[R] xyplot: modify axis tick marks

Gabor Grothendieck ggrothendieck at gmail.com
Sun Jan 16 18:32:45 CET 2011


On Sun, Jan 16, 2011 at 8:01 AM, Kang Min <ngokangmin at gmail.com> wrote:
> Hi,
>
> I would like to plot time against rainfall data (data is at the end)
> using xyplot.
>
> The basic code looks like this: xyplot(rainfall~time, type="a")
> When I do this, the graph looks ok except that the x-axis has too many
> values. I would just like to display the years and not the months on
> the x-axis. I've been fiddling around with 'scales', and read previous
> posts about axis problems but I still can't find the solution.
>


First we read in the data using zoo's "yearmon" class for the index:
yearmon variables are stored as year plus a fraction to indicate the
month such that January's fraction is zero so picking out the January
indexes using cycle(z) == 1 gives the years.  With lattice we need to
convert that to numeric years but its unnecessary using classic
graphics.

library(zoo)
z <- read.zoo("myfile.dat", header = TRUE, FUN = as.yearmon, format = "%b%Y")

# using xyplot.zoo and lattice
library(lattice)
xyplot(z, scales = list(x = list(at = as.numeric(time(z))[cycle(z) == 1])))

# using plot.zoo and classic graphics
plot(z, xaxt = "n")
axis(1, time(z)[cycle(z) == 1])


-- 
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