Skip to content

plotting time series with zoo pckg

1 message · AA

AA
#
Thank you Gabor. This is very helpful.
AA.

----- Original Message ----
From: Gabor Grothendieck <ggrothendieck at gmail.com>
To: ahmad ajakh <aajakh at yahoo.com>
Cc: r-help at stat.math.ethz.ch
Sent: Wednesday, December 27, 2006 12:08:41 PM
Subject: Re: [R] plotting time series with zoo pckg

To do it with plot.zoo one has to create a custom panel.

Another approach is to use xyplot.zoo since that supports custom scales
directly.   Note that the ?xyplot.zoo examples contain code that is along the
lines of the xyplot.zoo solution.

Here are examples of both approaches:

library(zoo)
# test data
z <- structure(c(21,34,33,41,39,38,37,28,33,40),
                index = structure(c(8044,8051,8058,8065,8072,
                8079,8086,8093,8100,8107), class="Date"), class = "zoo")
set.seed(1) # needed to make it reproducible
jx1 <- rnorm(10); jx2 <- rnorm(10); jx3 <- rnorm(10)
# create a zoo class data using the random vectors.
jx  <- cbind(jx1,jx2,jx3)
z1  <- zoo(jx, index(z))


# 1. plot.zoo solution using custom panel function, my.panel
my.panel <- function(...) {
   lines(...)
   if (parent.frame()$j == ncol(z1)) {
      # following line only if non-labelled ticks wanted for each point
      axis(1, at = time(z1), lab = FALSE)
      ix <- seq(1, length(z1), 3)
      labs <- format(time(z1)[ix], "%b-%d")
      axis(1, at = time(z1)[ix], lab = labs, tcl = -0.7, cex.axis = 0.7)
   }
}
plot(z1, panel = my.panel, xaxt = "n")

# 2. xyplot.zoo solution
# z1 is from above

library(lattice)
ix <- seq(1, length(z1), 3)
labs <- format(time(z1)[ix], "%b-%d")
xyplot(z1, scales = list(x = list(at = time(z1)[ix], labels = labs)))

# only need following if non-labelled ticks are to be added for each point
trellis.focus("panel", 1, 1, clip.off = TRUE)
panel.axis("bottom", check.overlap = TRUE, outside = TRUE, labels = FALSE,
   tck = .7, at = time(z1))
trellis.unfocus()
On 12/27/06, ahmad ajakh <aajakh at yahoo.com> wrote: