如何使用 R 中的 Highcharter 将 x 轴类型从类别更改为日期时间

问题描述 投票:0回答:0

感谢这个答案我能够学习如何制作尽可能多的钻取图。

我遇到的问题是我的约会被视为“类别”而不是时间序列。我想修改此代码以将向下钻取图的最后级别(或者如果可能的话,我想要的任何级别)显示为“日期时间”图而不是“类别”图。

我试过使用提供的代码,但它只将最后一级显示为“类别”图,即使我的数据是“dateTime”格式。有人可以建议如何修改代码以实现此目的吗?


Test <- data.frame(Group = c("A", "A", "A", "A", "A", "A", "A", "A", 
                             "B", "B", "B", "B", "B", "B", "B", "B"),
                   Group_Two = c("AA", "AAA", "AA", "AAA", "AAA", "AA", 
                                 "AA", "AAA", "BB", "BBB", "BB", "BBB", 
                                 "BB", "BBB", "BB", "BBB"),
                   Group_Three = c("AJX", "ABX", "AJX", "ABX", "APX", "ANX", 
                                   "ANX", "APX", "BJX", "BBX", "BJX", "BBX", 
                                   "BPX", "BNX", "BPX", "BNX"),
                   Group_Four = as.Date(c("2022-07-12", "2022-07-16", "2022-07-31", "2022-08-01", "2022-08-12", "2022-08-21", "2022-08-27", 
                                  "2022-09-14", "2022-09-16", "2022-09-21", "2022-11-12", "2022-11-23", "2022-12-14", "2022-12-17", 
                                  "2022-12-24", "2022-12-29")),
                   Value = c(5293, 78225, 33235, 56022, 13056, 6160, 44067, 75529, 
                             95679, 98172, 27159, 77475, 37838, 25897, 88400, 28484))

TestSum <- Test %>%
  group_by(Group) %>%
  summarize(Quantity = sum(Value)
  )

TestSum <- arrange(TestSum,desc(Quantity))

Lvl1dfStatus <- tibble(name = TestSum$Group, y = TestSum$Quantity, drilldown = tolower(name))

Level_2_Drilldowns_Test <- lapply(unique(Test$Group), function(x_level) {
  TestSum2 <- subset(Test, Test$Group %in% x_level)
  #TestSum2 <- Test[Test$Group == x_level,]
  TestSum2 <- TestSum2 %>%
    group_by(Group_Two) %>%
    summarize(Quantity = sum(Value)
    )
  TestSum2 <- arrange(TestSum2,desc(Quantity)) ###CHECK
  Lvl2dfStatus <- tibble(name = TestSum2$Group_Two, y = TestSum2$Quantity, drilldown = tolower(paste(x_level, name, sep = "_")))
  list(id = tolower(x_level), type = "column", data = list_parse(Lvl2dfStatus))
})

Level_3_Drilldowns_Test <- lapply(unique(Test$Group), function(x_level) {
  TestSum2 <- subset(Test, Test$Group %in% x_level)
  #TestSum2 <- Test[Test$Group == x_level,]
  lapply(unique(TestSum2$Group_Two), function(y_level) {
    TestSum3 <- subset(TestSum2, TestSum2$Group_Two %in% y_level)
    #TestSum3 <- TestSum2[TestSum2$Group_Two == y_level,]
    TestSum3 <- TestSum3 %>%
      group_by(Group_Three) %>%
      summarize(Quantity = sum(Value)
      )
    TestSum3 <- arrange(TestSum3,desc(Quantity))
    Lvl3dfStatus <- tibble(name = TestSum3$Group_Three,y = TestSum3$Quantity, drilldown = tolower(paste(x_level, y_level, name, sep = "_")))
    list(id = tolower(paste(x_level, y_level, sep = "_")), type = "column", data = list_parse(Lvl3dfStatus))
  })
})%>% unlist(recursive = FALSE)

Level_4_Drilldowns <- lapply(unique(Test$Group), function(x_level) {
  TestSum2 <- subset(Test, Test$Group %in% x_level)
  #TestSum2 <- Test[Test$Group == x_level,]
  lapply(unique(TestSum2$Group_Two), function(y_level) {
    TestSum3 <- subset(TestSum2, TestSum2$Group_Two %in% y_level)
    #TestSum3 <- TestSum2[TestSum2$Group_Two == y_level,]
    lapply(unique(TestSum3$Group_Three), function(z_level) {
      TestSum4 <- subset(TestSum3, TestSum3$Group_Three %in% z_level)
      #TestSum4 <- TestSum3[TestSum3$Group_Three == z_level,]
      TestSum4 <- TestSum4 %>%
        group_by(Group_Four) %>%
        summarize(Quantity = sum(Value)
        )
      TestSum4 <- arrange(TestSum4,desc(Quantity))
      Lvl4dfStatus <- tibble(name = TestSum4$Group_Four,y = TestSum4$Quantity)
      list(id = tolower(paste(x_level, y_level, z_level, sep = "_")), type = "column", data = list_parse2(Lvl4dfStatus))
    })
  })%>% unlist(recursive = FALSE)
}) %>% unlist(recursive = FALSE)

highchart() %>%
  hc_xAxis(type = "category") %>%
  hc_add_series(Lvl1dfStatus, "column", hcaes(x = name, y = y), color = "#E4551F") %>%
  hc_plotOptions(column = list(stacking = "normal")) %>%
  hc_drilldown(
    allowPointDrilldown = TRUE,
    series = c(Level_2_Drilldowns_Test, Level_3_Drilldowns_Test, Level_4_Drilldowns)
  )

谢谢提前!

编辑:在this answer中,似乎你可以做到。我怎样才能在我的 R 代码中实现这个脚本?

PS:如果可以的话,我想指出我的实际数据包含数千行

r highcharts r-highcharter
© www.soinside.com 2019 - 2024. All rights reserved.