Juan Pablo Lewinger
2007-Jun-19 07:35 UTC
[R] Controlling text and strip arrangement in xyplot
I've searched the archives and read the xyplot help but can't figure out the 2 lattice questions below? Consider: library(lattice) DF <- data.frame(x=rnorm(20), y=rnorm(20), g1=rep(letters[1:2], 10), g2=rep(LETTERS[1:2], each=10), g3=rep(rep(letters[3:4],each=5),2)) xyplot(y ~ x | g1 + g2, groups=g3, data=DF) 1) Is there a way to get one strip per row and column of panels as below instead of the default? _|__a__|__b__| | B | -- | A | 2) How do I control the text of the strips so that for instance instead of "a" and "b" it reads"g1=alpha", "g1=beta" where "alpha" and "beta" stand for the corresponding greek symbols? (my difficulty here is not with the plotmath symbols but with controlling the text of the strips directly from the call to xyplot and not by renaming the levels of g1) I'd appreciate any help! Juan Pablo Lewinger Department of Preventive Medicine Keck School of Medicine University of Southern California
On 6/19/07, Juan Pablo Lewinger <lewinger at usc.edu> wrote:> I've searched the archives and read the xyplot help but can't figure > out the 2 lattice questions below? > > Consider: > > library(lattice) > DF <- data.frame(x=rnorm(20), y=rnorm(20), g1=rep(letters[1:2], 10), > g2=rep(LETTERS[1:2], each=10), > g3=rep(rep(letters[3:4],each=5),2)) > > xyplot(y ~ x | g1 + g2, groups=g3, data=DF) > > 1) Is there a way to get one strip per row and column of panels as > below instead of the default? > > > _|__a__|__b__| > | > B > | > -- > | > A > |Instead of using lattice, you could use ggplot2 (http://had.co.nz/ggplot2), where this is the default: (p <- qplot(x, y, data=DF, facets = g1 ~ g2))> 2) How do I control the text of the strips so that for instance > instead of "a" and "b" it reads"g1=alpha", "g1=beta" where "alpha" > and "beta" stand for the corresponding greek symbols? (my difficulty > here is not with the plotmath symbols but with controlling the text > of the strips directly from the call to xyplot and not by renaming > the levels of g1)It's also possible to do this in ggplot, but some bugs currently stop it from working. It will work in the next version to be released next week: p$strip.text <- function(variable, value) { greek <- c("A" = "alpha", "B" = "beta")[value] makelabel <- function(g) substitute(variable == greek, list(variable=as.name(variable), greek=as.name(g))) lapply(greek, makelabel) } p Hadley
Hi Pablo,> DF <- data.frame(x=rnorm(20), y=rnorm(20), g1=rep(letters[1:2], 10), > g2=rep(LETTERS[1:2], each=10), > g3=rep(rep(letters[3:4],each=5),2)) > > xyplot(y ~ x | g1 + g2, groups=g3, data=DF)... I remember findling with this some time ago and getting most of the way there. If you have to use lattice, then the following may help you. It's close, but not quite what you want; you almost certainly need to write a custom panel function. Hopefully Deepayan will step in (as he usually does) to help you. 1) Look at ?strip.default, as well as ?xyplot (search for "strip", "strip.left") 2) Example: xyplot(y ~ x | interaction(g1,g2, drop=TRUE), groups=g3, data=DF, strip.left=strip.custom(factor.levels=c("A","A","B","B")), strip=strip.custom(factor.levels=rep(c("g1=a","g1=b"),2))) Hope that helps, Regards, Mark. Pablo Lewinger wrote:> > I've searched the archives and read the xyplot help but can't figure > out the 2 lattice questions below? > > Consider: > > library(lattice) > DF <- data.frame(x=rnorm(20), y=rnorm(20), g1=rep(letters[1:2], 10), > g2=rep(LETTERS[1:2], each=10), > g3=rep(rep(letters[3:4],each=5),2)) > > xyplot(y ~ x | g1 + g2, groups=g3, data=DF) > > 1) Is there a way to get one strip per row and column of panels as > below instead of the default? > > > _|__a__|__b__| > | > B > | > -- > | > A > | > > 2) How do I control the text of the strips so that for instance > instead of "a" and "b" it reads"g1=alpha", "g1=beta" where "alpha" > and "beta" stand for the corresponding greek symbols? (my difficulty > here is not with the plotmath symbols but with controlling the text > of the strips directly from the call to xyplot and not by renaming > the levels of g1) > > I'd appreciate any help! > > > Juan Pablo Lewinger > Department of Preventive Medicine > Keck School of Medicine > University of Southern California > > ______________________________________________ > 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. > >-- View this message in context: http://www.nabble.com/Controlling-text-and-strip-arrangement-in-xyplot-tf3944756.html#a11193990 Sent from the R help mailing list archive at Nabble.com.
Deepayan Sarkar
2007-Jun-19 18:12 UTC
[R] Controlling text and strip arrangement in xyplot
On 6/19/07, Juan Pablo Lewinger <lewinger at usc.edu> wrote:> I've searched the archives and read the xyplot help but can't figure > out the 2 lattice questions below? > > Consider: > > library(lattice) > DF <- data.frame(x=rnorm(20), y=rnorm(20), g1=rep(letters[1:2], 10), > g2=rep(LETTERS[1:2], each=10), > g3=rep(rep(letters[3:4],each=5),2)) > > xyplot(y ~ x | g1 + g2, groups=g3, data=DF) > > 1) Is there a way to get one strip per row and column of panels as > below instead of the default? > > > _|__a__|__b__| > | > B > | > -- > | > A > |This has been discussed on the list before (if I remember correctly), and I have been meaning to add something to the latticeExtra package. An implementation would look something like this (beware of line wrapping): useOuterStrips <- function(x, strip = strip.default, strip.left = strip.custom(horizontal = FALSE)) { dimx <- dim(x) stopifnot(inherits(x, "trellis")) stopifnot(length(dimx) == 2) opar <- if (is.null(x$par.settings)) list() else x$par.settings par.settings <- modifyList(opar, list(layout.heights list(strip = c(rep(0, dimx[2]-1), 1)), layout.widths list(strip.left = c(1, rep(0, dimx[1]-1))))) update(x, par.settings = par.settings, strip = function(which.given, which.panel, ...) { if (which.given == 1) strip(which.given = 1, which.panel = which.panel[1], ...) }, strip.left = function(which.given, which.panel, ...) { if (which.given == 2) strip.left(which.given = 1, which.panel = which.panel[2], ...) }, par.strip.text = list(lines = 0.5), layout = dimx) } The function acts on a "trellis" object and returns an updated one, so for your example, it would work like: useOuterStrips(xyplot(y ~ x | g1 + g2, groups=g3, data=DF))> 2) How do I control the text of the strips so that for instance > instead of "a" and "b" it reads"g1=alpha", "g1=beta" where "alpha" > and "beta" stand for the corresponding greek symbols? (my difficulty > here is not with the plotmath symbols but with controlling the text > of the strips directly from the call to xyplot and not by renaming > the levels of g1)Generally speaking, you need to write your own strip function. The default (strip.default) has some useful arguments that modify its behaviour, and in particular 'factor.levels' might do what you want. If you are going to do this in conjunction with (1), life will actually be simpler and you can get away with using strip.custom(): useOuterStrips(xyplot(y ~ x | g1 + g2, groups=g3, data=DF), strip strip.custom(factor.levels expression(g[1]==alpha, g[1]==beta)), strip.left strip.custom(horizontal = FALSE, factor.levels expression(g[2]==gamma, g[2]==delta))) Otherwise, you will really have to write a proper strip function that calls strip.default() with different values of 'factor.levels' depending on the value of 'which.given'. Note also the 'strip.names' and 'sep' argument of strip.default(), which might be more in line with what you want to do. -Deepayan