简体   繁体   English

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

[英]Stacked barplot incorrect category labels

The hover labels show the labels correctly, but I am not able to get the correct category labels under my bars. 悬停标签可以正确显示标签,但是我无法在栏下获得正确的类别标签。

在此处输入图片说明

Here is the working example: 这是工作示例:

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"))

I have tried setting categories=A$name in case it needs six values, but that doesn't work. 我尝试设置categories=A$name ,以防它需要六个值,但这是行不通的。 I have tried changing some of the x-axis options ( startonTick , minPadding , min , max ), but doesn't seem to make it work. 我尝试过更改某些x轴选项( startonTickminPaddingminmax ),但似乎没有使其工作。

I am using variable 'x' on the x-axis rather than variable 'names' to retain the specific order, else it sorts alphabetically. 我在x轴上使用变量“ x”,而不是变量“ names”来保留特定顺序,否则它将按字母顺序排序。

The issue has to do with the x axis being numeric. 问题与x轴是数字有关。 Two ways to get you what you want. 两种获取您想要的方法。

First, I'm assigning x to name 首先,我将x分配给name

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

Other way is to declare A$x a factor 另一种方法是声明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"))

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM