[R] times series trellis plot

Gabor Grothendieck ggrothendieck at gmail.com
Wed Mar 7 01:21:59 CET 2012


On Tue, Mar 6, 2012 at 11:14 AM, Stefan Luedtke <sluedtke at gfz-potsdam.de> wrote:
> I spent some more time to understand the examples .. it helped a bit.
>
> But there is still one issue.
>
> Taking example data (my first on, hope it is not confusing ;-)  ) like:
>
>
> library(latticeExtra)
> library(zoo)
>
>
> #  example Data
>
> timeVect1=seq(as.Date("1990-01-01"), as.Date("2010-12-31"), by="day")
> timeVect2=seq(as.Date("1993-01-01"), as.Date("2010-12-31"), by="day")
>
> df1=data.frame(a=runif(length(timeVect1), max=200),
> b=runif(length(timeVect1), max=250))
> df2=data.frame(a=runif(length(timeVect2), max=2),
> b=runif(length(timeVect2), max=1.5))
>
> ts1=zoo(df1, timeVect1)
> ts2=zoo(df2, timeVect2)
>
>
> # Adding some NA values
>
> ts1$a[sample(1:trunc(length(timeVect1)/2), 2000)]=NA
> ts1$b[sample(trunc(length(timeVect1)/2):length(timeVect1), 2000)]=NA
>
> ts2$b[sample(1:trunc(length(timeVect2)/2), 2000)]=NA
> ts2$a[sample(trunc(length(timeVect2)/2):length(timeVect2), 2000)]=NA
>
>
>
>
> xyplot(ts1, panel = function(x, y, ...) {
>        panel.grid(h = 10, v = 10, col = "grey", lty = 3)
>        panel.xblocks(x, is.na(y), ..., col="RED")
> panel.xyplot(x, y, ...)
> })
>
> So I managed to plot the missing data of ts1 to the corresponding panel,
> but, how do I add the missing data from ts2 to the plot that show the
> data from ts1 for the columns a and b??

Assuming you want to add indications of missing data from ts2$a to the
a panel and missing data from ts2$b to the b panel then add this line
to your panel function:

	panel.xblocks(time(ts2), is.na(ts2[, panel.number()]), ..., col="RED")


-- 
Statistics & Software Consulting
GKX Group, GKX Associates Inc.
tel: 1-877-GKX-GROUP
email: ggrothendieck at gmail.com



More information about the R-help mailing list