[R] Gaps in plotting temporal data.

Duncan Murdoch murdoch.duncan at gmail.com
Thu Feb 24 14:45:19 CET 2011


On 24/02/2011 7:38 AM, Christos Delivorias wrote:
> 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
> data-points 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?

Any software is going to have the same problem you had:  how do you 
define a gap?  If the definition is something simple like "time 
difference greater than X", then it will be fairly easy:  use diff() to 
find all the time differences in the sorted times, and wherever those 
exceed X, insert a new data point with an NA value.  For example,

t <- c(1,2,3,7,8,9,11,12,13)
x <- rnorm(length(T))
d <- diff(t)
gap <- which(d > 1.5)
if (length(gap)) {
   newT <- (t[gap] + t[gap+1])/2
   t <- c(t, newT)
   x <- c(x, rep(NA, length(newT)))
   o <- order(t)
   t <- t[o]
   x <- x[o]
}
plot(t, x, type='l')

Duncan Murdoch



More information about the R-help mailing list