JohnCoene / echarts4r

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

Add lines to e_boxplot #509

Closed svenb78 closed 1 year ago

svenb78 commented 1 year ago

Hi,

given the following example:

mtcars |>
  dplyr::group_by(gear) |>
  echarts4r::e_charts() |>
  echarts4r::e_boxplot(serie = hp)

How can I add within each boxplot a horizontal line for -- say -- the average?

According to apache documentation, boxplot has an argument called markline, but I don't know how to use it correctly.

E.g., the following does not work in the sense of does not change the plot:

mtcars |>
  dplyr::group_by(gear) |>
  echarts4r::e_charts() |>
  echarts4r::e_boxplot(serie = hp, markline = list(type = "average"))

Thanks!

rdatasculptor commented 1 year ago

Have you tried e_mark_line()? A wrapper function, part of echarts4r. https://echarts4r.john-coene.com/reference/mark.html

munoztd0 commented 1 year ago

Something like that

library(tidyverse) # has both dplyr and purrrrrr (how many r's?)
library(echarts4r)

mean_boxplot <- mtcars %>%
  group_split(gear) %>%
  imap(function(j, k) {

    point <- j %>% 
      summarise(hp=round(mean(hp),0)) %>% 
      select(
        yAxis = hp,
        value = hp
    ) %>% 
    as.list()

    j  %>%
      e_charts(time, name = paste0("chart_", as.character(k))) %>%
              echarts4r::e_boxplot(hp) %>%
              e_mark_line(data = point) 

  }) %>% append(c(rows = 1, cols = 3)) %>%
  do.call(e_arrange, .)

mean_boxplot

image

Created on 2023-07-07 with reprex v2.0.2.9000