Kevin
Kevin

Reputation: 2044

R Highcharter - X-axis names turning to index in graph

I have a couple column graphs in Highcharter that allow drill down. I have noticed that depending on how big the column drill down is, highcharter switches from showing the correct x-axis names to index. Is there a way I can make sure it will always show the x-axis name?

Below is an example code that shows a scenario where it displays correctly and where it doesn't. Only change is the size of the data set (from 400 to 50).

library (highcharter)

rm(list=ls())

#### CASE 1 - X AXIS NAMES NOT SHOWING IN DRILLDOWN ####

h <- 0
Case1Num <- c()
for(i in 1:400){
  h <- h + .02
  Case1Num[i] <- h
}

Case1Name <- c()
for(i in 1:400){
  Case1Name[i] <- paste0("test",i)
}

Case1Group <- c()
for(i in 1:400){
  Case1Group[i] <- paste0("group1")
}

Case1 <- data.frame(Case1Name,Case1Num,Case1Group)

Lvl1 <- aggregate(Case1$Case1Num, by = list(Case1$Case1Group),FUN=sum)
Lvl1df <- data_frame(name = Lvl1$Group.1, y = Lvl1$x, drilldown = tolower((paste(name))))

dfLvl2 <- arrange(data_frame(name = Case1$Case1Name,value = Case1$Case1Num),desc(value))

hc <- highchart() %>%
  hc_chart(type = "column", zoomType = "x") %>%
  hc_xAxis(type = "category", labels = list(style = list(fontSize = "1.1vh"))) %>%
  hc_yAxis(gridLineWidth = 0, labels = list(style = list(fontSize = "1.1vh"))) %>%
  hc_add_series(data=Lvl1df, color = "#D20000") %>%
  hc_legend(enabled = TRUE) %>%
  hc_plotOptions(column = list(stacking = "normal")) %>%

  hc_drilldown(
    allowPointDrilldown = TRUE,
    series = list(
      list(id = "group1", data = list_parse2(dfLvl2))
    )
  )

hc

#### CASE 2 - X AXIS NAMES SHOWING CORRECTLY IN DRILLDOWN ####

h <- 0
Case2Num <- c()
for(i in 1:50){
  h <- h + .02
  Case2Num[i] <- h
}

Case2Name <- c()
for(i in 1:50){
  Case2Name[i] <- paste0("test",i)
}

Case2Group <- c()
for(i in 1:50){
  Case2Group[i] <- paste0("group1")
}

Case2 <- data.frame(Case2Name,Case2Num,Case2Group)

Lvl1 <- aggregate(Case2$Case2Num, by = list(Case2$Case2Group),FUN=sum)
Lvl1df <- data_frame(name = Lvl1$Group.1, y = Lvl1$x, drilldown = tolower((paste(name))))

dfLvl2 <- arrange(data_frame(name = Case2$Case2Name,value = Case2$Case2Num),desc(value))

hc2 <- highchart() %>%
  hc_chart(type = "column", zoomType = "x") %>%
  hc_xAxis(type = "category", labels = list(style = list(fontSize = "1.1vh"))) %>%
  hc_yAxis(gridLineWidth = 0, labels = list(style = list(fontSize = "1.1vh"))) %>%
  hc_add_series(data=Lvl1df, color = "#D20000") %>%
  hc_legend(enabled = TRUE) %>%
  hc_plotOptions(column = list(stacking = "normal")) %>%

  hc_drilldown(
    allowPointDrilldown = TRUE,
    series = list(
      list(id = "group1", data = list_parse2(dfLvl2))
    )
  )

hc2

Upvotes: 1

Views: 1798

Answers (2)

Katia
Katia

Reputation: 3914

This is an open issue with Highcharts. Not just in R but in javascript as well. This issue was reported here:

https://github.com/highcharts/highcharts/issues/5857

Upvotes: 2

Kevin
Kevin

Reputation: 2044

Looks like the issue was solved in the github thread.

adding cropThreshold = 1000 to hc_plotOptions solves the trick. It is normally set at default to 50 which is why the above was seen for anything greater.

hc2 <- highchart() %>%
  hc_chart(type = "column", zoomType = "x") %>%
  hc_xAxis(type = "category", labels = list(style = list(fontSize = "1.1vh"))) %>%
  hc_yAxis(gridLineWidth = 0, labels = list(style = list(fontSize = "1.1vh"))) %>%
  hc_add_series(data=Lvl1df, color = "#D20000") %>%
  hc_legend(enabled = TRUE) %>%
  hc_plotOptions(column = list(
                 cropThreshold = 1000,
                 stacking = "normal")) %>%

  hc_drilldown(
    allowPointDrilldown = TRUE,
    series = list(
      list(id = "group1", data = list_parse2(dfLvl2))
    )
  )

Upvotes: 1

Related Questions