MadiN
MadiN

Reputation: 195

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;

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

Old static version on the plot

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

Dygraph version displays rainfall as instantaneous points

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) %>%

Upvotes: 3

Views: 3634

Answers (1)

symbolrush
symbolrush

Reputation: 7457

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)

enter image description here

See also here for a deeper discussion about filling NAs.

Upvotes: 4

Related Questions