Search code examples
rr-highcharter

Axis Label Truncated in highcharter Barplot


This looks as expected:

df <- structure(list(surveillance_diag = c("Meningitis", "Sepsis"), 
                     y = c(1239L, 7850L), color = c("#1f78b4", "#e31a1c"), 
                     freq = c(14, 86)), row.names = c(NA, -2L), class = c("tbl_df", "tbl", "data.frame"))


library(highcharter)
library(magrittr)

highchart() %>% 
  hc_yAxis(title = "") %>%
  hc_xAxis(categories = df$surveillance_diag) %>%
  hc_add_series(data = df, type = "bar", hcaes(x = surveillance_diag, y = y, color = color))

enter image description here

But the same code with a data frame of only one row/category will cut the category label.

df <- df[1, ]

highchart() %>% 
  hc_yAxis(title = "") %>%
  hc_xAxis(categories = df$surveillance_diag) %>%
  hc_add_series(data = df, type = "bar", hcaes(x = surveillance_diag, y = y, color = color))

enter image description here

How can I ensure that the label is properly displayed regardless of the number of categories?


Solution

  • Passing categories as a list helps here.

    highchart() %>% 
      hc_yAxis(title = "") %>%
      hc_xAxis(categories = as.list(df$surveillance_diag)) %>%
      hc_add_series(data = df, type = "bar", hcaes(x = surveillance_diag, y = y, color = color))
    

    enter image description here