Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Plotting temporal TS and omitting NA data

Tags:

r

time-series

I'm trying to plot some temporal data that have some gaps in them. You can see the plot here: http://www.tiikoni.com/tis/view/?id=da222e2.
The problem is that during the time gaps in the TS the line plot is interpolated over the gap and I don't want it to. I've tried interleaving the gaps with an NA flag, but there are around 10000 datapoints sorted from multiple files, that makes it difficult to add the NA flag manually. If it's not possible to define the behaviour of the plot(0function, is there another plot I can use, e.g. zoo, that will allow me to not have the lines drawn between the gaps?

like image 766
cmdel Avatar asked Feb 24 '11 12:02

cmdel


2 Answers

It's not difficult to interleave the gaps with NA using merge, once you know the sequence of your time series. A little demonstration :

X <- c(1:20,41:100)
Y <- rnorm(80)

plot(X,Y,type="l")    

Z <- seq(min(X),max(X),by=1)    # I take 1 is the period.
newData <- merge(data.frame(X,Y),data.frame(X=Z),all=T)
plot(newData,type="l")
like image 63
Joris Meys Avatar answered Sep 22 '22 02:09

Joris Meys


Create a zoo series with a gap. Then define g which includes the time points of z plus the missing points. Create a zero width zoo series to merge with z and plot.

library(zoo)
z <- zoo(rnorm(12), c(1:6, 11:16)) # test data

g <- seq(start(z), end(z), 1)
zz <- merge(z, zoo(, g))
plot(zz)
like image 31
G. Grothendieck Avatar answered Sep 26 '22 02:09

G. Grothendieck