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:

library(dygraphs)
library(xts)

read.ehyd <- function(url) {
    # separate the header, open the connection with correct encoding
    con <- url(url, encoding = "latin1")
    header <- readLines(con, n=50)
    lines.header <- grep("Werte:", header, fixed = T)
    infile <- read.csv2(con, header = F, skip = lines.header,
        col.names = c("time", "value"),
        colClasses = c("character", "numeric"),
        na.strings = "Lücke",
        strip.white = TRUE, as.is = TRUE, fileEncoding = "latin1")
    infile$time <- as.POSIXct(infile$time, format = "%d.%m.%Y %H:%M:%S")
    return(xts(infile$value, order.by = infile$time))
}

# 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 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.

Post A Reply

*