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

前端 未结 3 1730
广开言路
广开言路 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:25

    The problem is that how does ggplot2 know you have missing values? I see two options:

    1. Pad out your time series with NA values
    2. Add an additional variable representing a "group". For example,

      dd = data.frame(Time, Value)
      ##type contains three distinct values
      dd$type = factor(cumsum(c(0, as.numeric(diff(dd$Time) - 1))))
      
      ##Plot, but use the group aesthetic
      ggplot(dd, aes(x=Time, y=Value)) +
            geom_line (aes(group=type))
      

      gives

      enter image description here

提交回复
热议问题