高位图表条形图中截断的轴标签

时间:2019-11-11 19:17:02

标签: r r-highcharter

这看起来像预期的那样:

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

但是,只有一个数据行只有一个行/类别的相同代码将剪切类别标签。

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

无论类别数如何,如何确保标签正确显示?

1 个答案:

答案 0 :(得分:1)

在列表中传递categories很有帮助。

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