Search code examples
rhighchartsr-highcharter

How to change x-axis type from category to datetime on drilldown with Highcharter in R


Thanks to this answer I was able to learn how to make as many drill-down plots as I want.

The issue I've faced thought is that my date is seen as a "category" and not as a time-series. I would like to modify this code to display the last level (or if possible, any level I want) of a drill-down plot as a "dateTime" graph instead of a "category" graph.

I have tried using the provided code, but it only displays the last level as a "category" graph, even when my data is in "dateTime" format. Can someone please advise on how to modify the code to achieve this?


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)
  )

Thanks is advance !

Edit: in this answer, it seems that you can do it (see this example i've made: https://jsfiddle.net/scrk20wa/). How can I implement this script in my R code?

PS: If I may, I would like to note that my actual data consists of thousands of rows.


Solution

  • After spending several hours learning about the highcharter package and thanks to this answer I managed to change the X-axis type. To achieve this, I added the id (Xaxis = 0 or Xaxis = 1) of the X-axis type to every level of the Drilldown as follows:

    
    library(highcharter)
    library(dplyr)
    
    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",
    xAxis = 0, # 0 is the first type you put in the hc_xaxis argument, 1 is for the second, etc...
    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",
    xAxis = 0, # 0 is the first type you put in the hc_xaxis argument, 1 is for the second, etc...
    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",
    xAxis = 1, # 0 is the first type you put in the hc_xaxis argument, 1 is for the second, etc...
    data = list_parse2(Lvl4dfStatus))
        })
      })%>% unlist(recursive = FALSE)
    }) %>% unlist(recursive = FALSE)
    

    After that, I modified the hc_xAxis argument to :

    highchart() %>%
      hc_xAxis(list(type = "category"), list(type = "datetime")) %>% # need to make a list for every type I wanted (first one is assigned 0, second 1, etc...
      hc_add_series(Lvl1dfStatus, "column", hcaes(x = name, y = y), color = "#E4551F", xAxis = 0) %>%
      hc_plotOptions(column = list(stacking = "normal")) %>%
      hc_drilldown(
        allowPointDrilldown = TRUE,
        series = c(Level_2_Drilldowns_Test, Level_3_Drilldowns_Test, Level_4_Drilldowns)
      )
    
    

    Note that I used a list for every type I wanted to assign to the X-axis. In this case, I assigned the value "category" to the first axis type (assigned 0 earlier) and "datetime" to the second axis type (assigned 1 earlier).

    I hope this helps other users looking for a solution to this problem.