Thursday, April 18, 2013

RG#83: Multi-faceted (Trellis) plot of time series plot (weather data example)


st <- as.Date("2009-1-1")
en <- as.Date("2011-12-28")
date1 <- seq(st, en, "1 day")
avgtm <- round (rnorm (length(date1), 50,5), 1)
maxtm <- round (avgtm + 8 + abs(rnorm (length (avgtm), 0, 1)),1)
mintm <-  round (avgtm - 8 + abs(rnorm (length (avgtm), 0, 1)), 1)
myd <- data.frame(date1, maxtm, mintm, avgtm)

# extract month
month <- function(x)format(x, '%m')
year <- function(x)format(x, '%Y')

require(lattice)
require(latticeExtra)

xyplot(avgtm ~ date1 | year(date1), data=myd,
       type='l', layout=c(1, 3),
       scales=list(x=list(relation='free')),
       xlab='', ylab='',
       panel=function(x, y, ...){
           panel.xblocks(x, month,
                         col = c("lightblue", "lightgreen"),
                         border = "darkgray")
           panel.xyplot(x, y, lwd = 1, col='red', ...)
           })


No comments:

Post a Comment

Note: Only a member of this blog may post a comment.