在 HighcharteR - R 中按 class 组向下钻取

Drilldown by class group in HighcharteR - R

我有这个数据:

library(highcharter) 
library(dplyr)

the_dates <- as.Date(c(rep("2021-01-01",3),
                       rep("2021-02-01",3)))
the_values <- c(2,3,4,5,6,7)
the_group <- c("Group_A","Group_B","Group_B",
               "Group_A","Group_B","Group_B")
the_class <- c("X","Y","Z",
               "X","Y","Z")

the_data <- data.frame(the_dates,
                       the_group,
                       the_class,
                       the_values,
                       stringsAsFactors = FALSE)
> the_data
   the_dates the_group the_class the_values
1 2021-01-01   Group_A         X          2
2 2021-01-01   Group_B         Y          3
3 2021-01-01   Group_B         Z          4
4 2021-02-01   Group_A         X          5
5 2021-02-01   Group_B         Y          6
6 2021-02-01   Group_B         Z          7

我想创建一个向下钻取图。所以我希望看到这些组,如果我向下钻取,我希望看到 class。我试过的是:

the_data %>%
  hchart(
    type = "spline",
    hcaes(x = the_dates, y = the_values, drilldown = the_class),
    colorByPoint = TRUE)

但要深入了解的 link 是在日期中。任何帮助将不胜感激。

这是一个可能的解决方案,但有一些注意事项。

我在向下钻取 x 轴名称中遇到了 as.Date() 的一些问题,所以我将它们保留为字符。我还通过 the_datethe_values 上做了一个快速 mean(),所以实际上有一些东西可以深入研究。

library(highcharter) 
library(dplyr)
library(purrr) # for the map() function 

the_dates <- c(rep("2021-01-01",3),
               rep("2021-02-01",3))
the_values <- c(2,3,4,5,6,7)
the_group <- c("Group_A","Group_B","Group_B",
               "Group_A","Group_B","Group_B")
the_class <- c("X","Y","Z",
               "X","Y","Z")

the_data <- data.frame(the_dates,
                       the_group,
                       the_class,
                       the_values,
                       stringsAsFactors = FALSE)

mean_data <- the_data %>% 
  group_by(the_dates) %>% 
  summarise(mean_values = mean(the_values))

drill_data <- the_data %>%
  group_nest(the_dates) %>% 
  mutate(
    id   = the_dates,
    type = "column",
    data = map(data, ~ .x %>%
      mutate(
        name = the_class,
        y    = the_values
      ) %>%
      list_parse())
  )

现在让我们构建情节:

mean_data %>%
  hchart(
    type = "spline",
    hcaes(x = the_dates, y = mean_values, drilldown = the_dates, name = the_dates),
    name = "Dates",
    colorByPoint = TRUE) %>% 
  hc_drilldown(
    allowPointDrilldown = TRUE,
    series = list_parse(drill_data)
  )