1. Automatically plots xts time-series objects (or any object convertible to xts.)
2. Highly configurable axis and series display (including optional second Y-axis.)
3. Rich interactive features, including zoom/pan and series/point highlighting.
4. Display upper/lower bars (ex. prediction intervals) around the series.
5. Various graph overlays, including shaded regions, event lines, and point annotations.
6. Use at the R console just like conventional R plots (via RStudio Viewer.)
7. Seamless embedding within R Markdown documents and Shiny web applications.
You can install the dygraphs package from CRAN, as follows:
You can use dygraphs at the R console, within R Markdown documents, and within Shiny applications. See the usage documentation linked to from the sidebar for more details. There are a few demos of dygraphs below, as well as quite a few others in the gallery of examples.
Here’s a simple dygraph created from a multiple time series object:
lungDeaths <- cbind(mdeaths, fdeaths) dygraph(lungDeaths)
Note that this graph is fully interactive. As your mouse moves over the series, individual values are displayed. You can also select regions of the graph to zoom into (double-click zooms out.)
You can customize dygraphs by piping additional commands onto the original dygraph object. Here we pipe a dyRangeSelector onto our original graph:
dygraph(lungDeaths) %>% dyRangeSelector()
Note that this example uses the %>% (or “pipe”) operator from the magrittr package to compose the dygraph with the range selector. You can use a similar syntax to customize axes, series, and other options. For example:
dySeries("mdeaths", label = "Male") %>%
dySeries("fdeaths", label = "Female") %>%
dyOptions(stackedGraph = TRUE) %>%
dyRangeSelector(height = 20)
Many options for customizing series and axis display are available. It’s even possible to combine multiple lower/value/upper style series into a single display with shaded bars. Here’s an example that illustrates shaded bars, specifying a plot title, suppressing the drawing of the grid for the x axis, and the use of a custom palette for series colors:
hw <- HoltWinters(ldeaths)
dyAxis("x", drawGrid = FALSE) %>%
dySeries(c("lwr", "fit", "upr"), label = "Deaths") %>%
dyOptions(colors = RColorBrewer::brewer.pal(3, "Set1"))
Now, let’s move on to the first set of real exercises on the dygraphs package!