highcharter:在仅包含一列的柱形图中固定x轴标签

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

我有以下代码来创建简单的柱状图,并且可以正常工作:

library(highcharter)
d1 <- iris %>% group_by(Species) %>% 
  summarize(mean_sepal_width = mean(Sepal.Width))

highchart() %>% 
  hc_chart(type = 'column') %>% 
  hc_xAxis(categories = d1$Species) %>% 
  hc_add_series(data = d1$mean_sepal_width)

但是,当我对输入数据进行子集以便仅存在一个x变量时,x轴标签将被破坏:

d2 <- d %>% filter(Species == 'virginica')
highchart() %>% 
  hc_chart(type = 'column') %>% 
  hc_xAxis(categories = d2$Species) %>% 
  hc_add_series(data = d2$mean_sepal_width)

enter image description here

这里提供了一种潜在的解决方案(Highcharter bar chart cut off x axis label,但我不希望使用hchart()函数,因为我的实际绘图要复杂得多。

是否有固定这些x轴标签的方法?

r highcharts r-highcharter
1个回答
0
投票
d2$Species放入列表中(或使用as.list)。这是已知的bug

highchart() %>% hc_chart(type = 'column') %>% hc_xAxis(categories = as.list(d2$Species)) %>% hc_add_series(data = d2$mean_sepal_width)

highcharter with xaxis category
© www.soinside.com 2019 - 2024. All rights reserved.