Search code examples
rhighchartsrcharts

Formatting Highcharts plot via rCharts


I'm having some trouble getting a highchart plot from rCharts working. My data and the intended graph something like this:

set.seed(123134)
y <- rnorm(20, 35, 4)
y[7] <- NA
y[13] <- NA
y <- rbind(t(t(y)), t(t(rep(NA, 10))))
fc <- rnorm(10, 35, 1)
fc <- rbind(t(t(rep(NA,20))), t(t(fc)))
uci <- rnorm(10, 38, 1)
uci <- rbind(t(t(rep(NA,20))), t(t(uci)))
lci <- rnorm(10, 32, 1)
lci <- rbind(t(t(rep(NA,20))), t(t(lci)))
plotData <- data.frame(y,fc,uci,lci)

h1 <- Highcharts$new()
h1$chart(type="line")
h1$series(data=plotData$y)
h1$series(data=plotData$fc)
h1$series(data=plotData$uci)
h1$series(data=plotData$lci)
h1$series(data=rep(30,30))
h1

Mostly it is some observed data with missing values, a forecast and corresponding intervals and a certain limit displayed by a horizontal line. Now, there are some things I can't figure out:

  1. I'd like to have the forecasts and intervals the same style. How can I change the point style of these three series to the same style?
  2. The horizontal line doesn't have to be interactive. Is there a option to draw a simple horizontal line? I didn't get it to work with the reference from http://docs.highcharts.com
  3. How can I remove certain series from the legend? In particular I don't want the intervals to be included in the legend.
  4. Is there a way of interpolating the missing values in the observed data? Or do I have to do this manually in advance?

Solution

  • Hi @user2691669 welcome to SO. I will try to address your 4 questions.

    1. To set style use a marker with option symbol = your style
    2. To remove markers use a marker with option enabled = FALSE
    3. To not have a series show in legend use showInLegend = FALSE
    4. To interpolate missing values the best i can offer is connectNulls = TRUE

    Your code can be written to implement the above as:

    set.seed(123134)
    y <- rnorm(20, 35, 4)
    y[7] <- NA
    y[13] <- NA
    y <- rbind(t(t(y)), t(t(rep(NA, 10))))
    fc <- rnorm(10, 35, 1)
    fc <- rbind(t(t(rep(NA,20))), t(t(fc)))
    uci <- rnorm(10, 38, 1)
    uci <- rbind(t(t(rep(NA,20))), t(t(uci)))
    lci <- rnorm(10, 32, 1)
    lci <- rbind(t(t(rep(NA,20))), t(t(lci)))
    plotData <- data.frame(y,fc,uci,lci)
    
    h1 <- Highcharts$new()
    h1$chart(type="line")
    h1$series(data=plotData$y, marker = list(symbol = 'circle'), connectNulls = TRUE)
    h1$series(data=plotData$fc, marker = list(symbol = 'circle'), connectNulls = TRUE)
    h1$series(data=plotData$uci, showInLegend = FALSE, marker = list(symbol = 'square'), connectNulls = TRUE)
    h1$series(data=plotData$lci, showInLegend = FALSE, marker = list(symbol = 'square'), connectNulls = TRUE)
    h1$series(data=rep(30,30), marker= list(enabled = FALSE))
    h1
    

    The various options can be seen at the HighCharts api documentation. For example the marker options are found at this link.