how to plot NA gaps using ggplot2
In basic plotting in R if there is an NA in a data series ggplot2 a gap will be plotted:
As an example see:
df=data.frame(x=c(1:10),y=c(1:10)) df[5:7,]=NA plot(df,type="l")
However, ggplot2 removes the missing values and plots a straight line with no gap.
I'd like to keep the gaps. They are valuable info in the data I am working with. Is there a simple way to tell ggplot2 to stop ignoring gaps?
This happens because you set both x and y to NA. (And the ggplot2 behaviour is more sensible IMHO than the base plot behaviour.) If only the y values are NA, you get the desired behaviour. Here is a workaround:
df$g <- cumsum(apply(df, 1, anyNA)) ggplot(data=df,aes(x,y, group=g))+geom_line()
Replace geom_line with geom_path.
library(ggplot2) ggplot(data = df, aes(x, y)) + geom_path()