R - Highcharter, как добавить горизонтальную пунктирную линию? - PullRequest
1 голос
/ 11 ноября 2019

У меня есть график с 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 ]

1 голос
/ 11 ноября 2019

Вы можете установить параметр 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

1 голос
/ 11 ноября 2019

Возможно, есть обходной путь, добавив третью строку к вашему графику:

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

...