【问题标题】:Drilldown by class group in HighcharteR - R在 HighcharteR - R 中按类组向下钻取
【发布时间】:2021-04-12 23:44:26
【问题描述】:

我有这些数据:

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

我想创建一个向下钻取图。所以我想查看这些组,如果我向下钻取,我想查看课程。我试过的是:

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

但向下钻取的链接在日期中。任何帮助将不胜感激。

【问题讨论】:

    标签: r r-highcharter


    【解决方案1】:

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

    我在向下钻取 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)
      )
    

    【讨论】:

    • 谢谢@tomasu,我想知道是否可以再次按日期向下钻取,但我想应该点击组颜色。
    猜你喜欢
    • 2019-08-18
    • 2018-02-17
    • 1970-01-01
    • 1970-01-01
    • 2017-09-01
    • 1970-01-01
    • 2020-04-13
    • 2018-07-31
    • 1970-01-01
    相关资源
    最近更新 更多