条带负堆叠和正标签

时间:2019-04-05 11:06:46

标签: r highcharts shiny shinydashboard

我正在尝试在R(发光)中制作一个反向堆叠的条形图表。我已经找到了制作图形的方法,但是找不到如何使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轴上的标签都为正。欢迎提出任何想法。

1 个答案:

答案 0 :(得分:1)

使用您的代码:

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) + '%'; }")))

结果:

enter image description here