【问题标题】:Add reference line for each bar in bar chart using highcharter使用 highcharter 为条形图中的每个条形添加参考线
【发布时间】:2018-06-07 15:15:47
【问题描述】:

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

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

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】:

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

    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
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-03-04
      • 1970-01-01
      • 1970-01-01
      • 2020-05-28
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多