[R] plotting predicted curves with log scale in lattice

Ken Knoblauch knoblauch at lyon.inserm.fr
Mon Sep 3 12:38:57 CEST 2007


Excuse me for forgetting sessionInfo (below)
Ken Knoblauch <knoblauch <at> lyon.inserm.fr> writes:
> 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.
R version 2.5.1 Patched (2007-08-26 r42657) 
i386-apple-darwin8.10.1 

locale:
C

attached base packages:
[1] "stats"     "graphics"  "grDevices" "utils"     "datasets"  "methods"  
[7] "base"     

other attached packages:
 lattice 
"0.16-3" 

but this also occurs for
R version 2.6.0 Under development (unstable) (2007-08-26 r42657) 
i386-apple-darwin8.10.1 

locale:
C

attached base packages:
[1] stats     graphics  grDevices utils     datasets  methods   base     

other attached packages:
[1] lattice_0.16-3

loaded via a namespace (and not attached):
[1] grid_2.6.0



More information about the R-help mailing list