How to Plot With Dygraphs

[This article was first published on R-exercises, and kindly contributed to R-bloggers]. (You can report issue about the content on this page here)
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.

The dygraphs package is an R interface to the dygraphs JavaScript charting library. It provides rich facilities for charting time-series data in R, including:

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.

Installation
You can install the dygraphs package from CRAN, as follows:

install.packages("dygraphs")
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:

library(dygraphs) 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:

dygraph(lungDeaths) %>% 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)
predicted % 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!

To leave a comment for the author, please follow the link and comment on their blog: R-exercises.

R-bloggers.com offers daily e-mail updates about R news and tutorials about learning R and many other topics. Click here if you're looking to post or find an R/data-science job.
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.

Never miss an update!
Subscribe to R-bloggers to receive
e-mails with the latest R posts.
(You will not see this message again.)

Click here to close (This popup will not appear again)