Skip to content

You can plot error bars (or any other high/low oriented data) along with a series by simply specifying the lower, value, and upper series together in the dySeries() name parameter. For example:

hw <- HoltWinters(ldeaths)
p <- predict(hw, n.ahead = 72, prediction.interval = TRUE)

dygraph(p, main = "Predicted Lung Deaths (UK)") |>
  dySeries(c("lwr", "fit", "upr"), label = "Deaths")

Here’s how you would plot the actual and predicted series on the same graph:

hw <- HoltWinters(ldeaths)
p <- predict(hw, n.ahead = 36, prediction.interval = TRUE)
all <- cbind(ldeaths, p)

dygraph(all, "Deaths from Lung Disease (UK)") |>
  dySeries("ldeaths", label = "Actual") |>
  dySeries(c("p.lwr", "p.fit", "p.upr"), label = "Predicted")

Finally, here’s an example that plots multiple series each of which have upper and lower bars:

## Loading required package: xts
## Loading required package: zoo
## 
## Attaching package: 'zoo'
## The following objects are masked from 'package:base':
## 
##     as.Date, as.Date.numeric
## Loading required package: TTR
## Registered S3 method overwritten by 'quantmod':
##   method            from
##   as.zoo.data.frame zoo
getSymbols(c("MSFT", "HPQ"), src = "rda")
## [1] "MSFT" "HPQ"
stocks <- cbind(MSFT[, 2:4], HPQ[, 2:4])
dygraph(stocks, main = "Microsoft and HP Share Prices") |>
  dySeries(c("MSFT.Low", "MSFT.Close", "MSFT.High"), label = "MSFT") |>
  dySeries(c("HPQ.Low", "HPQ.Close", "HPQ.High"), label = "HPQ")