today I’d like to introduce you to a great R package that focuses on interactive time series visualisation. The
dygraphs provides really nice methods for plotting time-series data interactively in R. These methods include (credits to the RStudio Blog):
- Automatic plotting of xts time-series objects (or other time series objects that are convertible to xts),
- Range selector interface for interactive interactive panning and zooming,
- Interactive series/point highlighting with different visual options,
- Highly configurable axis and series display (including optional 2nd Y-axis),
- Display upper/lower bars (e.g. error bars, prediction intervals) around series,
- Various graph overlays including shaded regions, event lines, and annotations,
- Use at the R console just like conventional R plots (via RStudio Viewer),
- Embeddable within R Markdown documents and Shiny web applications.
I have prepared a short application example using dygraphs to chart discharge data from the river Danube in Austria below. Please note that function
read_ehyd() for importing the data set can be found at Reading data from eHYD using R.
library(dygraphs) library(xts) # fetch data for three Austrian gauges: Wildungsmauer, Melk and Aschach url <- c(wm = "http://ehyd.gv.at/eHYD/MessstellenExtraData/owf?id=207373&file=4", me = "http://ehyd.gv.at/eHYD/MessstellenExtraData/owf?id=207134&file=4", ah = "http://ehyd.gv.at/eHYD/MessstellenExtraData/owf?id=207035&file=4") discharge <- do.call(cbind, lapply(url, read.ehyd)) # plot w/ dygraph dygraph(discharge, main = "Discharge of River Danube") %>% dyRangeSelector() %>% dySeries("wm", label = "Wildungsmauer") %>% dySeries("me", label = "Melk") %>% dySeries("ah", label = "Aschach") %>% dyHighlight(highlightCircleSize = 5, highlightSeriesOpts = list(strokeWidth = 3), highlightSeriesBackgroundAlpha = 0.2, hideOnMouseOut = FALSE) # time series with data coverage for all gauges common.start <- index(head(na.omit(discharge), 1)) dygraph(discharge[index(discharge) >= common.start, ], main = "Discharge of River Danube") %>% dyRangeSelector() %>% dySeries("wm", label = "Wildungsmauer") %>% dySeries("me", label = "Melk") %>% dySeries("ah", label = "Aschach") %>% dyHighlight(highlightCircleSize = 5, highlightSeriesOpts = list(strokeWidth = 3), highlightSeriesBackgroundAlpha = 0.2, hideOnMouseOut = FALSE)
This is how the resulting plot looks like:
You can post comments in this post.
Thank you very much for your great post. Is there a way to embed the graph into a presentation either Keynote, power point or prezi? Thank you again for your work!
Alessio Bocco 6 years ago
since I hardly use interactive plots in presentations, I have never considered this question.
I am not aware of simple methods to include dygraph plots into any of the mentioned presentation software.
However, from within R Studio, you can save dygraph plots as standalone HTML documents.
Probably, the simplest way is to use RStudio directly, knit a document, or export a HTML file and use your browser to show it.
Matthias 6 years ago
As Matthias already pointed out, you should have a look into Rmarkdown, especially ioslides. Here are some links:
or shiny R:
Martin 6 years ago
I am searching a way to embed a dygraph in a power point presentation as well. Did you find a solution?
And thank you Matthias for the great post!
Lina 5 years ago
Thank you for the informative page.
I have a question in regards to highlighting. I would like to pass on more information (not only the time and y-value of the data point highlighted) to the tooltip (or legend in this case) .
What do you suggest?
I think that I can use the dyCallBack function in the dygraph package to get the extra information, but not sure I should pass it on into legend area. If you can show me with a simple example it would be very appreciated. Thanks.
Ehson 5 years ago
I’m not 100% sure if I get what you intend to do. Are you looking for something in the lines of
A minimal example (copied from its help page) would be:
Matthias 5 years ago
Hi, i know its been a long time, but do you remember how you solved your issue by any chance ?
cgross 3 years ago
Post A Reply