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

R - Highcharter how to add horizontal dashed line?

我有一个图,其中 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")

创建的图表是:

可能是一种解决方法,在您的绘图中添加第三行:

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")

您可以将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")