Ken Knoblauch
2007-Sep-03 10:38 UTC
[R] plotting predicted curves with log scale in lattice
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
Ken Knoblauch
2007-Sep-03 11:59 UTC
[R] plotting predicted curves with log scale in lattice
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
hadley wickham
2007-Sep-03 13:25 UTC
[R] plotting predicted curves with log scale in lattice
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/
Deepayan Sarkar
2007-Sep-03 23:27 UTC
[R] plotting predicted curves with log scale in lattice
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,More like a possibly desirable feature that's missing.> I haven't picked up an indication that would lead me to > expect this in the documentation.Yes, the documentation is a bit vague. I've changed it to the following, which is hopefully clearer. 'log' Controls whether the corresponding variable ('x' or 'y') will be log transformed before being passed to the panel function. Defaults to 'FALSE', in which case the data are not transformed. Other possible values are any number that works as a base for taking logarithm, 'TRUE' (which is equivalent to 10), and '"e"' (for the natural logarithm). As a side effect, the corresponding axis is labeled differently. Note that this is a transformation of the data, not the axes. Other than the axis labeling, using this feature is no different than transforming the data in the formula; e.g., 'scales=list(x = list(log = 2))' is equivalent to 'y ~ log2(x)'. -Deepayan