使用highcharter为条形图中的每个条添加参考线

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

我想为每个栏添加一个特定的行,如下所示:

enter image description here

但是,我不能。为此,我尝试了以下代码至少放置一个特定的文本,但它不再起作用:

mydata <- data.frame(A=runif(1:10),
                   B=runif(1:10),
                   C=runif(1:10))

highchart() %>% 
hc_chart(type = "column", inverted = TRUE) %>% 
hc_title(text = "MyGraph") %>% 
hc_yAxis(title = list(text = "Weights")) %>% 
hc_plotOptions(column = list(
  dataLabels = list(enabled = FALSE),
  stacking = "normal",
  enableMouseTracking = FALSE)
) %>% 
hc_legend(layout="vertical") %>%
hc_tooltip(formatter = function(){ return("<b> test</b><br/>")},
           useHtml = TRUE) %>%
hc_series(list(name="A",data=mydata$A),
          list(name="B",data=mydata$B),
          list(name="C",data=mydata$C))

我的问题是如何在每个条形线的条形图中添加红线?

r plot r-highcharter
1个回答
2
投票

这是一个可能的解决方案:

set.seed(1)
mydata <- data.frame(A=runif(1:10), B=runif(1:10), C=runif(1:10))

library(highcharter)
hc <- highchart() %>% 
hc_chart(type = "column", inverted = TRUE) %>% 
hc_title(text = "MyGraph") %>% 
hc_yAxis(title = list(text = "Weights")) %>% 
hc_plotOptions(column = list(
  dataLabels = list(enabled = FALSE),
  stacking = "normal", groupPadding=0, 
  enableMouseTracking = FALSE)
) %>% 
hc_legend(layout="vertical") %>%
hc_tooltip(formatter = function(){ return("<b> test</b><br/>")},
           useHtml = TRUE) %>%
hc_series(list(name="A",data=mydata$A), 
          list(name="B",data=mydata$B),
          list(name="C",data=mydata$C)) 

# x position of red lines
linepos <- c(1.3, 0.7, 1.8, 1.2, 1.0, 1.6, 0.7, 1.7, 0.8, 1.1)
# height of red lines
lw <- 0.35
for (k in 1:length(linepos)) {
   df <- data.frame(x=c(k-1-lw,k-1+lw),y=rep(linepos[k],2))
   hc <- hc %>%
      hc_add_series(data = df, type = 'line', marker=list(enabled=FALSE),
          x = ~x, y= ~y, color='red', lineWidth=5, showInLegend=FALSE,
          enableMouseTracking = FALSE)
}
hc

enter image description here

© www.soinside.com 2019 - 2024. All rights reserved.