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
The problem is that how does ggplot2 know you have missing values? I see two options:
NA
valuesAdd 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
csgillespie mentioned padding by NA, but a simpler method is to add one NA after each block:
Value[seq(1,length(Value)-1,by=100)]=NA
where the -1 avoids a warning.
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")