简体   繁体   中英

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

在此处输入图像描述

Maybe a workaround, adding a third line to your plot:

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

在此处输入图像描述

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

在此处输入图像描述

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM