[R] plotting predicted curves with log scale in lattice

hadley wickham h.wickham at gmail.com
Mon Sep 3 15:25:59 CEST 2007


Hi Ken,

Alternatively, you could use ggplot2:

install.packages("ggplot2")
library(ggplot2)

qplot(LL, RR, data=ds1, facets = . ~ FF) + geom_line(data=ds2) + scale_x_log10()

It is very hard to get transformed scales working correctly, and it's
something I had to spend a lot of time on in between ggplot 1 and 2.

Hadley


On 9/3/07, Ken Knoblauch <knoblauch at lyon.inserm.fr> wrote:
> 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
>
> ______________________________________________
> R-help at stat.math.ethz.ch mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.
>


-- 
http://had.co.nz/



More information about the R-help mailing list