• The dygraphs package
  • A quick example
  • xts
  • Quarterly time series
  • Monthly time series
  • Options
    • Colors
    • Steps, fill (area), points, size, shape
    • Mix series
    • More parameters and settings

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"))
New Haven Temperatures
48
49
50
51
52
53
54
55
1920
1930
1940
1950


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"))
Number of Australian residents
13000
13500
14000
14500
15000
15500
16000
16500
17000
1980


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"))
Sunspots
0
10
20
30
40
50
60
70
80
90
100
1800
1810
1820


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'))
Deaths from Lung Disease (UK)
ldeaths
mdeaths
fdeaths
0
500
1000
1500
2000
2500
3000
3500
4000
Jan 1974
Jan 1975
Jan 1976
Jan 1977
Jan 1978
Jan 1979
dygraph(lungDeaths, main = "Deaths from Lung Disease (UK)") %>%
  dyOptions(colors = RColorBrewer::brewer.pal(3, "Set2"))
Deaths from Lung Disease (UK)
ldeaths
mdeaths
fdeaths
0
500
1000
1500
2000
2500
3000
3500
4000
Jan 1974
Jan 1975
Jan 1976
Jan 1977
Jan 1978
Jan 1979

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)
Deaths from Lung Disease (UK)
ldeaths
mdeaths
fdeaths
0
500
1000
1500
2000
2500
3000
3500
4000
Jan 1974
Jan 1975
Jan 1976
Jan 1977
Jan 1978
Jan 1979
# 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")
Deaths from Lung Disease (UK)
ldeaths
mdeaths
fdeaths
0
500
1000
1500
2000
2500
3000
3500
4000
Jan 1974
Jan 1975
Jan 1976
Jan 1977
Jan 1978
Jan 1979
# 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’).