JohnCoene / echarts4r

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

How to omit empty categorical group combinations #575

Closed jzadra closed 1 year ago

jzadra commented 1 year ago

How can I adjust an echart so that if there is no data for a combination of x-axis category and grouping variable, the empty space is removed?

ie in the chart below, the group2/A combo and the group1/C combo have no data, so I would like for the space for them to be removed.

Example:

library(tidyverse)
library(echarts4r)
x <- rep(c("A", "B", "C"), each = 100)
y <- rep(c("group1", "group2"), each = 150)
z <- runif(300, max = 10)

df <- tibble(x, y, z)

df %>% 
  group_by(y) %>% 
  e_charts(x) %>% 
  e_bar(z)

enter image description here

I have seen a potential solution using a custom serie, but my actual use case has many bars and I'd prefer to have an automatic solution rather than having to specify the width of each individual bar.

While I'm doing this in R, I can translate Javascript solutions.

rdatasculptor commented 1 year ago

I think this question was quite similar https://github.com/JohnCoene/echarts4r/issues/551 for me e_morph did the trick, but that won't be te solution for you. But maybe you can get some inspiration from it.

jzadra commented 1 year ago

Why closed?

jzadra commented 1 year ago

551 does not apply to my question.

munoztd0 commented 12 months ago

Well tell us why it doesn't ?

jzadra commented 12 months ago

1) Both of the charts in #551 still show empty space where there are no values for one of the A/B groups in each x-axis category. 2) I'm not doing a transition or timeline

rdatasculptor commented 12 months ago

@jzadra just curious, why is'nt removing empty bars using R before you render the echart a solution in your case? Also, I think I misunderstand your example code. Why do you make a tible with 300 records if you only want to show six bars?

if you you do this, it has the same chart as a result:

library(echarts4r)
x <- rep(c("A", "B", "C"), each = 100)
y <- rep(c("group1", "group2"), each = 150)
z <- runif(300, max = 10) 

df <- tibble(x, y, z) %>%
  group_by(x, y) %>%
  filter(z == max(z)) %>% # pick the maximum z value per x y combination
  ungroup()

df %>% 
  group_by(y) %>% 
  e_charts(x) %>% 
  e_bar(z)
jzadra commented 12 months ago

My example was created thinking I was going to do something differently, but then what I had worked for illustrative purposes so I just left it.

I"m not sure what you mean about removing the extra bars using R beforehand. Echarts is allocating space for two bars in each letter group, despite the fact that there is no combination of group1/A and group2/C. That isn't in the data, so there's nothign to remove.

JohnCoene commented 12 months ago

I don't think it's possible.

Also, I'm wondering if it is possible with other libraries (e.g.: ggplot2), this would make for an irregular grid which is supported in some libraries but is done to display areas more than bars.

Do you have an example in another library for us so that we can take a look? If it is supported I'm curious to see how this is handled.

rdatasculptor commented 11 months ago

@jzadra I think... that this is 90% of what you intended :). Entirely different approach, but maybe you can use it for some inspiration:

library(echarts4r)
x <- rep(c("A", "B", "C"), each = 100)
y <- rep(c("group1", "group2"), each = 150)
z <- runif(300, max = 10) 

df <- tibble(x, y, z) %>%
  group_by(x, y) %>%
  filter(z == max(z)) %>% # pick the maximum z value per x y combination
  ungroup() %>% 
  mutate(label = x,
         x = 1:n())
df1 <- df %>%
  filter(y == "group1") %>% rename(group1 = z)
df2 <- df %>%
  filter(y == "group2") %>% rename(group2 = z)

df1 %>% 
  e_charts(x) %>% 
  e_bar(group1, legend = TRUE,
        bind = label,
        label = list(show = TRUE,
                     formatter = "{b}",
                     position = "bottom",
                     offset = c(0, 10)),
        barWidth = "80%") %>%
  e_data(df2, x) %>%
  e_bar(group2, legend = TRUE,
        bind = label,
        label = list(show = TRUE,
                     formatter = "{b}",
                     position = "bottom",
                     offset = c(0, 10)),
        barGap = "-100%",
        barWidth = "80%") %>%
  e_x_axis(axisLabel = list(show = FALSE),
           axisTicks = list(show = FALSE))