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:

About This Author

Matthias studied Environmental Information Management at the University of Natural Resources and Life Sciences Vienna and holds a PhD in environmental statistics. The focus of his thesis was on the statistical modelling of rare (extreme) events as a basis for vulnerability assessment of critical infrastructure. He is working at the Austrian national weather and geophysical service (ZAMG) and at the Institute of Mountain Risk Engineering at BOKU University. He currently focuses the (statistical) assessment of adverse weather events and natural hazards, and disaster risk reduction. His main interests are statistical modelling of environmental phenomena as well as open source tools for data science, geoinformation and remote sensing.

7 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 6 years 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 6 years 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 6 years ago Reply


    • Hi, i know its been a long time, but do you remember how you solved your issue by any chance ?

      cgross 4 years ago Reply


Post A Reply

*