我正试图在R(Shiny)中制作一个反向堆叠条形图。我已经找到了如何制作图表,但我无法找到如何使x轴上的标签为正,如下所示:https://www.highcharts.com/demo/bar-negative-stack
我试图应用abs()
函数,但到目前为止它没有工作。有没有人有办法解决吗?
highchart() %>%
hc_chart(type = "bar") %>%
hc_title(text = "Example") %>%
hc_yAxis(title = list(text = ""), labels = list(format = "{value}")) %>%
hc_plotOptions(series=list(stacking='normal'),
column = list( dataLabels = list(enabled = FALSE),
enableMouseTracking = TRUE)) %>%
hc_legend(enabled = FALSE) %>%
hc_xAxis(reversed=FALSE, opposite=TRUE, reversed=FALSE) %>%
hc_add_series(name="neutral", id='neutral', color=c("#766A62"), data=list(2, 8)) %>%
hc_add_series(name="Neutral",linkedTo='neutral',color=c("#ffeeff"),data=list(-5, -3))
我希望条形图的值和x轴上的标签都是正的。欢迎任何想法。
使用你的代码:
highchart() %>%
hc_chart(type = "bar") %>%
hc_title(text = "Example") %>%
hc_yAxis(title = list(text = ""),labels = list(format = "{value}")) %>%
hc_plotOptions(series=list(stacking='normal'),column = list( dataLabels = list(enabled = FALSE),
enableMouseTracking = TRUE)) %>%
hc_legend(enabled = FALSE) %>%
hc_xAxis(list(categories = c("0-4", "5-9"),
reversed=FALSE ),
list(reversed=FALSE,opposite=TRUE,
reversed=FALSE,
categories =c("0-4", "5-9"),
linkedTo = 0)) %>%
hc_yAxis(
labels = list(
formatter = JS("function(){ return Math.abs(this.value) + '%'; }"))) %>%
hc_add_series(name="neutral",id='neutral',color=c("#766A62"),data=list(2, 8)) %>%
hc_add_series(name="Neutral",linkedTo='neutral',color=c("#ffeeff"),data=list(-5, -3))
以下行被修改:
hc_xAxis(list(categories = c("0-4", "5-9"),
reversed=FALSE ),
list(reversed=FALSE,opposite=TRUE,
reversed=FALSE,
categories =c("0-4", "5-9"),
linkedTo = 0)) %>%
hc_yAxis(
labels = list(
formatter = JS("function(){ return Math.abs(this.value) + '%'; }")))
结果: