Olivier Fermeus Olivier Fermeus - 1 month ago 26
R Question

Two y Axis in Highcharter in R

I would like to make a line chart containing one x axis sharing two y axis, one on the left side and the second one on the right of the plot in R.

I found many examples on how to do this but I don't manage to reproduce it in R with the package "highcharter".

Here are the examples :

http://jsfiddle.net/gh/get/jquery/1.7.1/highslide-software/highcharts.com/tree/master/samples/highcharts/yaxis/opposite/

http://www.highcharts.com/demo/combo-dual-axes

Here what I have done so far :

g <- highchart()%>%
hc_xAxis(categories = c("2016-01-01","2016-02-01","2016-03-01","2016-04-01","2016-05-01","2016-06-01","2016-07-01","2016-08-01","2016-09-01","2016-10-01"))%>%
hc_yAxis(
list(title = list(text = "Yaxis1")),
list(title = list(text = "Yaxis2"), opposite = TRUE)
)%>%
hc_series(
list(yAxis = 0, data = c(7.0, 6.9, 9.5, 14.5, 18.2, 21.5, 25.2, 26.5, 23.3, 18.3), name = "Data1"),
list(yAxis = 1, data = c(8.0, 7.9, 10.5, 15.5, 19.2, 22.5, 28.2, 23.5, 21.3, 14.3), name = "Data2")
)


Does anyone have an idea ?

Thank you !

Answer

In the highcharter official website there is a demo with two axis:

http://jkunst.com/highcharter/highcharts.html#highcharts-home-page-demo

enter image description here

You need to use hc_yAxis_multiples in this case:

highchart() %>% 
  hc_yAxis_multiples(
    list(lineWidth = 3),
    list(showLastLabel = FALSE, opposite = TRUE)
  ) %>% 
  hc_add_series(data = rnorm(10)) %>% 
  hc_add_series(data = rexp(10), type = "spline", yAxis = 1)