堆叠的条形图不正确的类别标签

时间:2018-08-07 21:11:02

标签: r highcharts r-highcharter

悬停标签可以正确显示标签,但是我无法在栏下获得正确的类别标签。

enter image description here

这是工作示例:

library(highcharter)
A <- data.frame(x=c(1,2,3,1,2,3),
                y=c(0.7,0.8,0.7,0.3,0.2,0.3),
                group=factor(c("A","A","A","B","B","B")),
                cluster=c("C1","C1","C2","C1","C1","C2"),
                name=c("John","Sally","Ed","John","Sally","Ed"),
                stringsAsFactors=F)

A %>%
hchart(.,"column",hcaes(x="x",y="y",group="group")) %>%
hc_xAxis(title=list(text=NULL),allowDecimals=FALSE,categories=A$name[1:3]) %>%
hc_plotOptions(column=list(animation=FALSE,stacking="normal"))

我尝试设置categories=A$name以防它需要六个值,但这是行不通的。我曾尝试更改某些x轴选项(startonTickminPaddingminmax),但似乎无法使其正常工作。

我在x轴上使用变量'x'而不是变量'names'来保持特定顺序,否则它将按字母顺序排序。

1 个答案:

答案 0 :(得分:0)

问题与x轴为数字有关。两种获取您想要的方法。

首先,我将x分配给name

A %>%
  hchart(.,"column",hcaes(x="name",y="y",group="group")) %>%
  hc_plotOptions(column=list(animation=FALSE,stacking="normal"))

其他方法是声明A$x一个因素

A %>%
  dplyr::mutate(x = as.factor(x)) %>%
  hchart(.,"column",hcaes(x="x",y="y",group="group")) %>%
  hc_xAxis(title=list(text=NULL),allowDecimals=FALSE,categories=A$name[1:3]) %>%
  hc_plotOptions(column=list(animation=FALSE,stacking="normal"))