JohnCoene / echarts4r

🐳 ECharts 5 for R
http://echarts4r.john-coene.com/
Other
595 stars 81 forks source link

Conflict e_bar & e_legend #514

Closed Clementthomass closed 1 year ago

Clementthomass commented 1 year ago

I work on a shiny app and i use the package echarts4r. However after some research i realised that you can't modify and reorder a legend with e_legend when you use e_bar. I would like to keep the original order and not have it sorted alphabetically. Do you know if there is a way to modify my legend by an other way.

rdatasculptor commented 1 year ago

Can you give an example code?

JohnCoene commented 1 year ago

Can you try the reorder argument in e_charts?

Clementthomass commented 1 year ago

Hi, thanks for your return, well i tried reorder argument in e_charts nothing happen.

Here this a exemple code :

series_names <- c("Série 2", "Série 3", "Série 1")

data_series1 <- c(10, 20, 30) data_series2 <- c(5, 15, 25) data_series3 <- c(2, 4, 6)

df <- data.frame(Categories = c("Categorie 1", "Categorie 2", "Categorie 3"), Serie_1 = data_series1, Serie_2 = data_series2, Serie_3 = data_series3)

e_charts(df) %>% e_bar(Série_1, stack = "stack_1", ) %>% e_bar(Série_2, stack = "stack_1", ) %>% e_bar(Série_3, stack = "stack_1", ) %>% e_title("Exemple of a graph stacked") %>% e_x_axis(name = "Categories") %>% e_y_axis(name = "Value") %>% e_tooltip(formatter = "{a}: {c}")

Well the issue it's about the legend he kept Serie 1, Serie 2, Serie 3 in this order. I just want to modify this order. I find a way when i switched the different e_bar that changed the order. There is a other way because i want to automatize it. Thanks in advance

JohnCoene commented 1 year ago

I don't understand.

You can just the order in which you pass the series

series_names <- c("Série 2", "Série 3", "Série 1")

data_series1 <- c(10, 20, 30)
data_series2 <- c(5, 15, 25)
data_series3 <- c(2, 4, 6)

df <- data.frame(
  Categories = c("Categorie 1", "Categorie 2", "Categorie 3"),
  Serie_1 = data_series1,
  Serie_2 = data_series2,
  Serie_3 = data_series3
)

e <- e_charts(df) %>%
  e_bar(Serie_2, stack = "stack_1", ) %>%
  e_bar(Serie_1, stack = "stack_1", ) %>%
  e_bar(Serie_3, stack = "stack_1", ) %>%
  e_title("Exemple of a graph stacked") %>%
  e_x_axis(name = "Categories") %>%
  e_y_axis(name = "Value") %>%
  e_tooltip(formatter = "{a}: {c}")
Clementthomass commented 1 year ago

Sorry to be not clear,

This is just an example df, in reality, I am receiving the data from another application and it is very dynamic. I am looking for a way to keep the order of the original data I am receiving, whatever that data may be.

rdatasculptor commented 1 year ago

For me it is still not very clear what you mean :) If you always want the order of the series to be in the same order like the columns in the data frame you receive, you could do something like this:

data_series1 <- c(10, 20, 30)
data_series2 <- c(5, 15, 25)
data_series3 <- c(2, 4, 6)

df <- data.frame(
  Categories = c("Categorie 1", "Categorie 2", "Categorie 3"),
  Serie_1 = data_series1,
  Serie_2 = data_series2,
  Serie_3 = data_series3
)

# get the order of the columns by taking the column names without the "Categories" column
serie_order <- subset(names(df), !names(df) %in% "Categories") 

# initiate the chart
e <- e_charts(df)

# add the bars in the same order of the columns
for (i in 1:length(serie_order)) e <- e %>% e_bar_(serie_order[i], stack = "stack_1")

# add the rest of the chart
e <- e %>% 
  e_title("Exemple of a graph stacked") %>%
  e_x_axis(name = "Categories") %>%
  e_y_axis(name = "Value") %>%
  e_tooltip(formatter = "{a}: {c}")

Does this solve your issue?

Clementthomass commented 1 year ago
Capture

Hello, no this doesn't solve the issue. What i'm trying to do here on the picture is to order the legend according to the bar chart i.e Orange first,in the legend. Right now the legend is being arranged in alphabetical order. Thanks in advance

rdatasculptor commented 1 year ago

maybe you misunderstood my solution. You can give the legend any order you like.

Take a look at this part:

# add the bars in the same order of the columns
for (i in 1:length(serie_order)) e <- e %>% e_bar_(rerie_order[i], stack = "stack_1")

Then, e.g. reverse it

# add the bars in the reversed order of the columns (and take a look at the legend)
for (i in 1:length(serie_order)) e <- e %>% e_bar_(rev(serie_order)[i], stack = "stack_1")

or give it a completely other order:

# add the bars in another order
serie_order  <- c("Serie_1", "Serie_3", "Serie_1")
for (i in 1:length(serie_order)) e <- e %>% e_bar_(serie_order[i], stack = "stack_1")

Do you notice the order of the legend items changing?

atocharnaud commented 1 year ago

Yes order is changing, thank you, but in our case the dataframe we are using has a variable number of stacked variables (columns) making your suggestion difficult to implement while maybe an option to respect the order from the provided dataframe rather than forcing an alphabetical order which may be coming from some groupby you may have in the package.

image

Arnaud Atoch

rdatasculptor commented 1 year ago

Still hard to understand what you mean exactly 🙂. Do the the names in LH1 resemble the different bars you wan to show? If so, I guess you can use pivot_wider() of the tidyr package to make for each bar its own column in the data frame. After that my solution can be used to modify the order.

munoztd0 commented 1 year ago

Closing because of inactivity