Interactive time series visualisation in R

Hey there,

today I’d like to introduce you to a great R package that focuses on interactive time series visualisation. The dygraphs package is an R interface to the Dygraphs Interactive Time Series Charting Library which is implemented in JavaScript. dygraphs provides really nice methods for plotting time-series data interactively in R. These methods include (credits to the RStudio Blog):

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:

Matthias

Matthias studied Environmental and Bio-Resources Management with a specialization in Environmental Information Management at the University of Natural Resources and Life Sciences (Vienna). He is currently a PhD student working at the Austrian Institute of Technology. Having written his master's thesis about extreme weather risk identification for the Austrian road network, he currently focuses on modeling of adverse weather events as a basis for risk assessment of road infrastructure networks.

6 Comments

You can post comments in this post.


  • Dear Matthias,
    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!
    My best,

    Alessio

    Alessio Bocco 2 months ago Reply


  • Hi Matthias,

    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 3 weeks ago Reply


    • Hi,

      I’m not 100% sure if I get what you intend to do. Are you looking for something in the lines of dyAnnotation?
      A minimal example (copied from its help page) would be:

      dygraph(presidents, main = "Presidential Approval") %>%
        dyAxis("y", valueRange = c(0, 100)) %>%
        dyAnnotation("1950-7-1", text = "A", tooltip = "Korea") %>%
        dyAnnotation("1965-1-1", text = "B", tooltip = "Vietnam")
      

      Best regards,
      Matthias

      Matthias 2 weeks ago Reply


Post A Reply

*