An embedded and charset-unspecified text was scrubbed... Name: not available URL: <https://stat.ethz.ch/pipermail/r-help/attachments/20110224/e5620aba/attachment.pl>
Gaps in plotting temporal data.
3 messages · Christos Delivorias, Duncan Murdoch, rex.dwyer at syngenta.com
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
If you're in a hurry, it's way easier than that: t <- c(1,2,3,7,8,9,11,12,13) x <- rnorm(length(t)) new.t <- min(t):max(t) new.x <- NULL new.x[t-min(t)+1] <- x plot(new.t, new.x, type='l') This is wastes max(t)-min(t)-length(t)+1 vector entries, but presumably you won't be wasting a lot of real estate along the horizontal axis of a plot. If you do, you're going to need to fix that anyway. -----Original Message----- From: r-help-bounces at r-project.org [mailto:r-help-bounces at r-project.org] On Behalf Of Duncan Murdoch Sent: Thursday, February 24, 2011 8:45 AM To: Christos Delivorias Cc: r-help at r-project.org Subject: Re: [R] Gaps in plotting temporal data.
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
______________________________________________
R-help at r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.
message may contain confidential information. If you are not the designated recipient, please notify the sender immediately, and delete the original and any copies. Any use of the message by you is prohibited.