JohnCoene / echarts4r

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

free scale with e_facet() #470

Open carlosyanez opened 2 years ago

carlosyanez commented 2 years ago

Hi, Thank you for creating and maintaining this great package.

I'm trying to use e_facet() to create parallel charts, each showing data for one category. Basically, I'm trying to mimic ggplot2::facet_wrap(..., scales="free"), i.e

data <- tribble(~Cat,~Value,
                "AAAAA", 1,
                "AAAAA",   2,
                "AAAAA",   5,
                "BBBBB",   3,
                "BBBBB",    3,
                "BBBBB",    4,
                "CCCC",    1,
                "CCCC",    9,
                "CCCC",    5)

data |>
  ggplot(aes(y=Cat,x=Value)) +
  geom_point() +
  facet_wrap(.~Cat, scale="free_y",ncol=1)

Which will produce (plus theming):

ggplot

Using e_facet() I'm getting every category in each facet:

p <-data |>
  group_by(Cat)|>
  e_charts(Cat) |>
  e_scatter(Value,
            symbol_size = 10,
            jitter_factor =1,
            itemStyle=list(color= "green")) |>
  e_y_axis(min=0,max=10) |>
  e_flip_coords() |>
  e_tooltip(trigger = "item") |>
  e_legend(show = FALSE) |>
  e_facet(cols=1,rows=3) 

sample1

As a workaround, I'm editing the echarts4r object in R directly, but the result decomposes each string into 1-character labels:

for(i in 1:length(cats))
  p$x$opts$yAxis[[i]]$data <- cats[i]

sample2

When looking at the element I'm modifying I'm getting the following before and after:

Before

p$x$opts$yAxis[[1]]$data
#> [1] "AAAAA" "BBBBB" "CCCC" 

After modifying

p$x$opts$yAxis[[1]]$data
#> [1] "AAAAA"

Please note the change works well when each category is already a 1-character string.

Is there a work around for this from R?

Thanks, Carlos