[R] plotting predicted curves with log scale in lattice

Ken Knoblauch knoblauch at lyon.inserm.fr
Mon Sep 3 13:59:52 CEST 2007


Hi,

I was taken off guard by the following behavior in a lattice plot.
I frequently want to add a predicted curve defined at more
points than in the formula expression of xyplot.  There have
been numerous examples of how to do this on r-help, but I
still often struggle to make this work.  I just realized that
specifying one of the axes on a log scale does not guarantee
that the added data for a curve will automatically take that
into account.  I don't know if this should be called a bug,
I haven't picked up an indication that would lead me to
expect this in the documentation.  I admit that if I had a
deeper understanding of lattice and/or grid, it might be
clearer why...  Here is a toy example illustrating the behavior
(there may be a more efficient way to do this),

ds1 <- data.frame( RR = rep(seq(0, 1, len = 5)^2, 2) +
						rnorm(10, sd = 0.1),
				   LL = rep(10^seq(1, 5), 2),
				   FF = factor(rep(letters[1:2], each = 5))
				   )
ds2 <- data.frame(RR = rep(seq(0, 1, len = 20)^2, 2),
				  LL = rep(10^seq(1, 5, len = 20), 2),
				   FF = factor(rep(letters[1:2], each = 20))
				   )
library(lattice)
xyplot(RR ~ LL | FF, ds1,
		scales = list(x = list(log = TRUE)),
		aspect = "xy",
		subscripts = TRUE,
		ID = ds2$FF,
		panel = function(x, y, subscripts, ID, ...) {
			w <- unique(ds1$FF[subscripts])
			llines(log10(ds2$LL[ID == w]), ds2$RR[ID == w], ...)
			panel.xyplot(x, y, ...)
			}
		)

Note that the x-variable of llines must be logged to plot the correct values
and so the scales argument seems to apply only to the x, y arguments
passed to the panel function.

Thank you.

best,

Ken


-- 
Ken Knoblauch
Inserm U846
Institut Cellule Souche et Cerveau
Département Neurosciences Intégratives
18 avenue du Doyen Lépine
69500 Bron
France
tel: +33 (0)4 72 91 34 77
fax: +33 (0)4 72 91 34 61
portable: +33 (0)6 84 10 64 10
http://www.lyon.inserm.fr/846/english.html



More information about the R-help mailing list