# [R] plotting time series with zoo pckg

Gabor Grothendieck ggrothendieck at gmail.com
Wed Dec 27 18:08:41 CET 2006

```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:
> Dear Gabor,
> sorry for not posting the code. below I have a piece of code that generates
> a multivariate zoo data (3 columns) and graphs it using the axis
> commands to generate the labels. This does not work. However, if one
> extracts one column the labelling works using the same commands!
> I cannot figure out what I am missing here.
> Thanks for any suggestion.
> AA.
> I am using R version 2.4.0 (windows XP) and zoo package version 1.2-1(2006-09-20).
>
>
> #-----Begin------------------------------------------
> # Let's take the previous example to create a simple zoo 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")
>
> # generating 3 random vectors with the same length as z.
> 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))
> # now we just repete the previous example.
> plot(z1, xaxt = "n")
> axis(1, time(z1), lab = FALSE)
> jd <- time(z1)[seq(1, dim(z1)[], 3)]
> axis(1,jd, as.character(jd),cex.axis = 0.8, tcl = -0.7, las = 2)
> # now we extract one column of z1 and graph it with the same axis commands.
> z2 <- z1[,1, drop = F]
> windows()
> plot(z2, xaxt = "n")
> axis(1, time(z2), lab = FALSE)
> jd <- time(z2)[seq(1, dim(z2)[], 3)]
> axis(1,jd, as.character(jd),cex.axis = 0.8, tcl = -0.7, las = 2)
> #-------------------End-----------------------------------
>
> ----- 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: Tuesday, December 26, 2006 11:17:44 PM
> Subject: Re: [R] plotting time series with zoo pckg
>
>
> On 12/26/06, ahmad ajakh <aajakh at yahoo.com> wrote:
> > Dear Gabor,
> > This solution works for my univariate zoo class time series. I first tried
> > it for a timeseries with 4 columns of data, it did not plot the labels nor the
> > ticks, I tried it on a one dim timeseries (one column zoo class data as the example
> > in the question) and it worked!  is there something that I am missing?
> > Thanks again.
> > 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: Tuesday, December 26, 2006 8:31:07 PM
> > Subject: Re: [R] plotting time series with zoo pckg
> >
> > Try this:
> >
> >
> > # test data
> > library(zoo)
> > 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")
> > z
> >
> > # plot without X axis
> > plot(z, xaxt = "n")
> >
> > # unlabelled tick at each point
> > axis(1, time(z), lab = FALSE)
> >
> > # labelled tick every third point
> > dd <- time(z)[seq(1, length(z), 3)]
> > axis(1, dd, as.character(dd), cex.axis = 0.7, tcl = -0.7)
> >
> >
> >
> > On 12/26/06, ahmad ajakh <aajakh at yahoo.com> wrote:
> > > Hi all,
> > >
> > > I am using the zoo package to plot time series. I have a problem with formatting the axes.
> > > my zoo object (z) looks like the following.
> > >
> > >                c1
> > > 1992-01-10     21
> > > 1992-01-17     34
> > > 1992-01-24     33
> > > 1992-01-31     41
> > > 1992-02-07     39
> > > 1992-02-14     38
> > > 1992-02-21     37
> > > 1992-02-28     28
> > > 1992-03-06     33
> > > 1992-03-13     40
> > >
> > > plot.zoo(z) produces a plot with the labels on the x-axis that I cannot control.
> > > I want a an xtick every 10 data points with corresponding date labels.
> > > I have tried different combination of axis command without success
> > > any idea?
> > > Thanks
> > >
> > > ______________________________________________
> > > R-help at stat.math.ethz.ch mailing list
> > > https://stat.ethz.ch/mailman/listinfo/r-help
> > > and provide commented, minimal, self-contained, reproducible code.
> > >
> >
> >
> >
> >
> > __________________________________________________
> > Do You Yahoo!?
> > Tired of spam?  Yahoo! Mail has the best spam protection around
> > http://mail.yahoo.com
> >
>
>
>
>
> __________________________________________________
> Do You Yahoo!?
> Tired of spam?  Yahoo! Mail has the best spam protection around
> http://mail.yahoo.com
>

```