The dygraphs package

The package and the JavaScript library website.

A quick example

library(dygraphs)

dygraph(nhtemp, main = "New Haven Temperatures") %>% 
  dyRangeSelector(dateWindow = c("1920-01-01", "1960-01-01"))


xts

Datasets must either be time series data or numeric data. For time series, this must be an xts object or an object which is convertible to xts (such as zoo). For numeric data, this must be a named list or data frame, where the first element/column provides x-axis values and all subsequent elements/columns provide one or more series of y-values.

Quarterly time series

We use the quarterly time series of the number of Australian residents.

head(austres)
## [1] 13067.3 13130.5 13198.4 13254.2 13303.7 13353.9
library(xts)
## Le chargement a nécessité le package : zoo
## 
## Attachement du package : 'zoo'
## Les objets suivants sont masqués depuis 'package:base':
## 
##     as.Date, as.Date.numeric
# Convert austres into an xts object
au <- as.xts(austres)
head(au, 5)
##            [,1]
## 1971 Q2 13067.3
## 1971 Q3 13130.5
## 1971 Q4 13198.4
## 1972 Q1 13254.2
## 1972 Q2 13303.7
library(dygraphs)

dygraph(au, main = "Number of Australian residents") %>% 
  dyRangeSelector(dateWindow = c("1972-01-01", "1990-01-01"))


Monthly time series

head(sunspots)
## [1] 58.0 62.6 70.0 55.7 85.0 83.5
library(xts)

# Convert sunspots to xts
sunspots_xts <- as.xts(sunspots)
head(sunspots_xts, 5)
##          [,1]
## jan 1749 58.0
## fév 1749 62.6
## mar 1749 70.0
## avr 1749 55.7
## mai 1749 85.0
library(dygraphs)

dygraph(sunspots_xts, main = "Sunspots") %>% 
  dyRangeSelector(dateWindow = c("1800-01-01", "1830-01-01"))


Options

lungDeaths <- cbind(ldeaths, mdeaths, fdeaths)

head(lungDeaths, 3)
##      ldeaths mdeaths fdeaths
## [1,]    3035    2134     901
## [2,]    2552    1863     689
## [3,]    2704    1877     827

Colors

library(dygraphs)

dygraph(lungDeaths, main = "Deaths from Lung Disease (UK)") %>%
  dyOptions(colors = c('red', 'blue', 'orange'))
dygraph(lungDeaths, main = "Deaths from Lung Disease (UK)") %>%
  dyOptions(colors = RColorBrewer::brewer.pal(3, "Set2"))

Steps, fill (area), points, size, shape

library(dygraphs)

# Step plots
dygraph(lungDeaths, main = "Deaths from Lung Disease (UK)") %>%
  dyOptions(colors = RColorBrewer::brewer.pal(3, "Set2")) %>%
  dyOptions(stepPlot = TRUE)
# dyOptions(fillGraph = TRUE, fillAlpha = 0.4)
# dyOptions(drawPoints = TRUE, pointSize = 2)
# dyOptions(drawPoints = TRUE, pointSize = 5, pointShape = "triangle")
# triangle, square, diamond, pentagon, hexagon, circle, star, plus, ex

Mix series

library(dygraphs)

dygraph(lungDeaths, main = "Deaths from Lung Disease (UK)") %>%
  dySeries("ldeaths", drawPoints = FALSE, color = "red") %>%
  dySeries("mdeaths", drawPoints = TRUE, color = "blue") %>%
  dySeries("fdeaths", stepPlot = TRUE, fillGraph = TRUE, color = "orange")
# dySeries("ldeaths", strokeWidth = 2, strokePattern = "dashed")
# dyGroup(c("mdeaths", "ldeaths"), drawPoints = TRUE, color = c("blue", "green"))

More parameters and settings

  • Series Highlighting.
  • Axis Options.
  • Labels & legends.
  • Time Zones.
  • Range Selector.
  • Candlestick Charts (Stock Exchanges).
  • Synchronization.
  • Straw Broom Charts (Stock Exchanges).
  • Roll Periods (moving average and smoothing).
  • Annotation/Shading.
  • Events and Limits.
  • Upper and Lower Bars (confidence intervals).
  • Plugins (for interactivity).
  • Custom Plotters.
  • Colored ribbons (for displaying ‘regimes’).