[R] summary - controling x-labels in xyplot (lattice) when x is POSIX object

john.gavin@ubs.com john.gavin at ubs.com
Tue Oct 21 17:44:35 CEST 2003


Hi,

The solution to my problem is to use 
lattice:::calculateAxisComponents to calculate appropriate labels
for the time axis in trellis plots.

# For example, given

x <- seq.POSIXt(strptime("2003/01/01", format = "%Y/%m/%d"),
                strptime("2003/10/01", format = "%Y/%m/%d"), by = "month")
y <- rnorm(length(x))
dat <- data.frame(x= x, y = y)

# the code 

xyplot(y ~ x, data = dat, type = "b")

# could be replaced with

labels <- lattice:::calculateAxisComponents(x)
labels$at <- ISOdate(1970,01,01) + as.numeric(x)
xyplot(y ~ x, data = dat, type = "b",
       scales = list(x = list(at = labels$at, labels = labels$labels)))

# to get the effect that I want.

This is essentially what I used to do (< 1.8.0) 
but the ':::' operator is now required.
Also, the 'at' component must be of class "POSIXt" rather than numeric,
as was the case before.

Thanks to Deepayan Sarkar <deepayan at stat.wisc.edu> and
Martin Maechler <maechler at stat.math.ethz.ch>.

Regards,

John.

John Gavin <john.gavin at ubs.com>,
Quantitative Risk Models and Statistics,
UBS Investment Bank, 6th floor, 
100 Liverpool St., London EC2M 2RH, UK.
Phone +44 (0) 207 567 4289
Fax   +44 (0) 207 568 5352

Date: Mon, 20 Oct 2003 18:35:43 +0100
From: <john.gavin at ubs.com>
Subject: [R] controling x-labels in xyplot (lattice) when x is POSIX object
To: <r-help at stat.math.ethz.ch>

Hi,

V1.8.0 seems to allow DateTimeClasses as the x argument in xyplots (lattice).
For example:

x <- seq.POSIXt(strptime("2003/01/01", format = "%Y/%m/%d"),
                strptime("2003/10/01", format = "%Y/%m/%d"), by = "month")
y <- rnorm(length(x))
dat <- data.frame(x= x, y = y)
xyplot(y ~ x, data = dat, type = "b")

However, the labelling for the x-axis is not what I want.
(I see only one tick mark and one label ('Oct').)
What is the recommended way to relabel the x-axis?
Ideally, I want to see several months (3-6) labelled along the x-axis.

Previously, I used 'calculateAxisComponents' to massage the labels manually
but that function (which I realise was internal to lattice) is no longer available.

I am on Windows XP, R 1.8.0.

Regards,

John.


Visit our website at http://www.ubs.com

This message contains confidential information and is intend...{{dropped}}




More information about the R-help mailing list