Dygraph with Multiple Series at Different Time Intervals

I have 2 sets of time series with different time intervals which I am attempting to show in a single dygraph plot;

  • Stage (river level) and Modelled Stage - 5 or 15 minute interval
  • Rainfall and Forecast Rainfall - 3 hourly interval

I would like the stage set to be a line chart and rainfall to appear as a step plot similar to below.

My issue is that, as far as I can see, you must cbind your timeseries together in order to create a multi-series dygraph. Cbind fills in 'missing' points with NA causing my graph to appear with isolated points of rainfall like so

Is there any way to overplot in dygraph without combining everything into 1 time series object? Alternatively does anybody have any clever methods for filling in NAs during a cbind? I have a rather inelegant bit of code to fill in NAs after the cbind at the moment...


Example code for second plot

stage <- zoo(sample(1:100, 154, replace=TRUE), seq(as.POSIXct("2018-08-23"), as.POSIXct("2018-08-31"), by = 4500))
rain <- zoo(sample(1:100, 154, replace=TRUE), seq(as.POSIXct("2018-08-23"), as.POSIXct("2018-08-31"), by = 54000))

eventData <- cbind(stage, rain)

dygraph(eventData, main = "Sitename") %>% 
  dyOptions(useDataTimezone = TRUE, colors = colour, drawGrid = F) %>%
  dyAxis("y", label = "Stage", valueRange = c(0, maxStage+maxStage*.2), independentTicks = TRUE) %>%
  dyAxis("y2", label = "Rainfall ", valueRange = c(0, maxRain+maxRain*.5), independentTicks = TRUE) %>%
  dySeries("Stage", axis=('y')) %>%
  dySeries("Rainfall", axis=('y2'), stepPlot = T, fillGraph = T) %>%

1 Answer

You can use zoo::na.locf function to fill the missing rows.

In your example:

stage <- zoo(sample(1:100, 154, replace=TRUE), seq(as.POSIXct("2018-08-23"), as.POSIXct("2018-08-31"), by = 4500))
rain <- zoo(sample(1:100, 154, replace=TRUE), seq(as.POSIXct("2018-08-23"), as.POSIXct("2018-08-31"), by = 54000))

eventData <- cbind(stage, rain)
head(eventData)

                    stage rain
2018-08-23 00:00:00    85   61
2018-08-23 01:15:00    71   NA
2018-08-23 02:30:00    10   NA
2018-08-23 03:45:00    16   NA
2018-08-23 05:00:00    31   NA
2018-08-23 06:15:00    92   NA

# fill NAs with na.locf

eventData <- na.locf(eventData)
head(eventData)

                    stage rain
2018-08-23 00:00:00    85   61
2018-08-23 01:15:00    71   61
2018-08-23 02:30:00    10   61
2018-08-23 03:45:00    16   61
2018-08-23 05:00:00    31   61
2018-08-23 06:15:00    92   61

This can be plotted the way you want it:

library(dygraphs)
dygraph(eventData, main = "Sitename") %>% 
  dyOptions(drawGrid = F) %>%
  dyAxis("y", label = "Stage", independentTicks = TRUE) %>%
  dyAxis("y2", label = "Rainfall ", independentTicks = TRUE) %>%
  dySeries("stage", axis=('y')) %>%
  dySeries("rain", axis=('y2'), stepPlot = T, fillGraph = T)

See also here for a deeper discussion about filling NAs.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Robert Thorne

Robert Thorne

Automotive & Future Transportation Editor

Robert Thorne covers electric vehicle innovations, autonomous driving systems, global mobility trends, and automotive engineering developments.

Share this article
Twitter Facebook Pinterest