Search code examples
rtime-seriesplotlyr-plotly

Make a graph line break even if there are no records for that gap


I'm facing an issue plotting a time series with plotly in R. Basically I have my data in a data frame structured like this:

datetime            | measure
2018-04-09 00:40:05 | 3.4
2018-04-09 00:41:00 | 3.9
2018-04-09 00:42:07 | 3.8
2018-04-09 00:43:00 | 3.7
2018-04-09 00:44:00 | 4.2
2018-04-09 00:45:00 | 5.8
2018-04-09 01:15:00 | 5.8

Observations are unevenly spaced, I have one observation at each minute but just roughly, there may be a difference of few seconds. Then I have larger gaps between observations, in the example I've put here there is a 30’ min gap between the last observation and the previous one.

Plotly is obviously connecting these two last observations in the line chart, what I would like to have is actually a blank gap between them instead.


Solution

  • You can achieve that by dividing your data into "chunks" and plotting each one as a separate trace. Reproducible example:

    df <- data.frame(
      time = Sys.time() - c(1:10, 51:60),
      value = runif(20),
      chunk = rep(c("A", "B"), each = 10)
    )
    
    p <- plot_ly(data = df[which(df$chunk == "A"),], x = ~time, y = ~value, name = "chunk A", type = "scatter", mode = "lines") %>%
      add_trace(data = df[which(df$chunk == "B"),], x = ~time, y = ~value, name = "chunk B", type = "scatter", mode = "lines")
    
    # or if there are more 'chunks'
    p2 <- plot_ly(data = df, x = ~time, y = ~value, color = ~chunk, type = "scatter", mode = "lines")
    

    Naturally, you need to find a meaningful way to group your data, i.e. if time difference between observations is bigger than [specify your cutoff], move to new chunk.