Search code examples
rhighchartsr-highcharter

R - Highcharter how to add horizontal dashed line?


I have a graph with 2 subgraphs stacked into 1 larger graph. I would like to add a horizontal dashed line to second graph (at a y value =1). What is the correct code to do this?

My current code is as follows:

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

The graph created is:

enter image description here


Solution

  • You can set plotLines parameter to the second y-axis:

    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