R-Highcharter如何添加水平虚线?

时间:2019-11-11 10:09:16

标签: r highcharts r-highcharter

我有一个图,其中2个子图堆叠成1个较大的图。我想在第二张图中添加水平虚线(y值= 1)。正确的代码是什么?

我当前的代码如下:

library(xts)
library(highcharter)

dates = seq(as.Date("2012-01-01"), as.Date("2012-01-04"), by="day")
x1 = xts(c(2,3,1,5), dates)
x2 = xts(c(1,1.5,2,1), dates)

highchart(type = "stock") %>%
   hc_yAxis_multiples(
     list(top = "0%", height = "60%", title = list(text = "Var1")),
     list(top = "60%", height = "40%", title = list(text = "Var2"))) %>%
   hc_add_series(x1, yAxis=0, compare="percent", color="blue") %>%
   hc_add_series(x2, yAxis=1, color="black")

创建的图形为:

enter image description here

2 个答案:

答案 0 :(得分:1)

也许是一种解决方法,在绘图中添加第三行:

library(xts)
library(highcharter)

dates = seq(as.Date("2012-01-01"), as.Date("2012-01-04"), by="day")
x1 = xts(c(2,3,1,5), dates)
x2 = xts(c(1,1.5,2,1), dates)
x3 = xts(rep(1, length(dates)), dates) # here the third line

highchart(type = "stock") %>%
  hc_yAxis_multiples(
    list(top = "0%", height = "60%", title = list(text = "Var1")),
    list(top = "60%", height = "40%", title = list(text = "Var2"))) %>%
  hc_add_series(x1, yAxis=0, compare="percent", color="blue") %>%
  hc_add_series(x2, yAxis=1, color="black")%>%
  # here you add to your plot
  hc_add_series(x3, yAxis=1, color="red", dashStyle = "DashDot")

enter image description here

答案 1 :(得分:1)

您可以将plotLines参数设置为第二个y轴:

highchart(type = "stock") %>%
  hc_yAxis_multiples(
    list(top = "0%", height = "60%", title = list(text = "Var1")),
    list(top = "60%", height = "40%", title = list(text = "Var2"),
         plotLines = list(list(value = 1, color = "red", width = 2,
                               dashStyle = "shortdash")))
  ) %>%
  hc_add_series(x1, yAxis = 0, compare = "percent", color = "blue") %>%
  hc_add_series(x2, yAxis = 1, color = "black")

enter image description here