ggplot2 time series plotting: how to omit periods when there is no data points?

前端 未结 3 1733
广开言路
广开言路 2020-12-15 21:08

I have a time series with multiple days of data. In between each day there\'s one period with no data points. How can I omit these periods when plotting the time series usin

3条回答
  •  生来不讨喜
    2020-12-15 21:34

    First, create a grouping variable. Here, two groups are different if the time difference is larger than 1 minute:

    Group <- c(0, cumsum(diff(Time) > 1))
    

    Now three distinct panels could be created using facet_grid and the argument scales = "free_x":

    library(ggplot2)
    g <- ggplot(data.frame(Time, Value, Group)) + 
      geom_line (aes(x=Time, y=Value)) +
      facet_grid(~ Group, scales = "free_x")
    

    enter image description here

提交回复
热议问题