Daniel Myall
2009-Jan-25 09:54 UTC
[R] Multiple lattice plots on a page: aligning x-axes vertically
Dear R-help, I am creating a two lattice plots (a densityplot() and xyplot()) that have the same x-axes and then 'printing' them onto the same page, one above the other (see end of email for an example to generate the graph). With different labels on the y-axis for each plot the left spacing is different, and the x-axes don't align vertically. Although I can manually modify the print region of the plots on the page to align the x-axes, this is not very robust; as can be expected changing the size of the plot device scales the x-axes differently in both plots. Additionally, changing devices also causes issues (i.e., the plots are aligned in quartz(width=12,height=6) but then slightly off with pdf(width=12,height=6)). As I'm using this inside Sweave to generate numerous plots of this form, I am after some robust way to vertically align the x-axes of the plots. With the approach I'm taking is there a way with lattice or grid to vertically align the x-axes? By somehow setting the internal plot width to be the same in both plots I think this would solve the issue (possibly by setting the right parameters in str(trellis.par.get("layout.widths")))? Alternatively, would another approach be better (i.e., is it possible to create a new panel function that does a panel.xyplot and panel.densityplot on slightly different data?). Thanks. Daniel ## BEGIN Example #OS: Mac OS X 10.5.5; R: 2.8.1; lattice 0.17-20 library(reshape) library(lattice) plotdensitymeans <- function(data,measure,factors=c('subject_group','task'), xlab="",xlim=NULL, ...) { # Create means by subject x.melted <- melt(data, id.var = append(factors,'subject_ID'), measure.var=measure,na.rm=T) formula.bysubject.cast <- paste(factors[1],"+",factors[2],"+ subject_ID ~ .") x.cast.subject <- data.frame(cast(x.melted,formula.bysubject.cast, mean)) # Plot means by subject text.formula <- paste(factors[1],":",factors[2],"~ X.all.") text.group <- paste(factors[1],":",factors[2]) formula.xyplot <- as.formula(text.formula) formula.group <- as.expression(formula.xyplot) environment(formula.xyplot) <- environment() plot.subject.means <- xyplot(formula.xyplot, group=eval(formula.group), xlim=xlim, pch=16, alpha=0.6,data = x.cast.subject,xlab=xlab,ylab="") # Plot distributions formula.densityplot <- as.formula(paste(" ~",measure[1])) environment(formula.densityplot) <- environment() plot.density <- densityplot(formula.densityplot,group=eval(formula.group),data=data, xlim=xlim,n=200,auto.key=list(columns = 4,line=TRUE),plot.points=F,xlab="",lty=1, scales=list(y=list(draw=FALSE),x=list(draw=FALSE))) # Plot both plots on a single page print(plot.subject.means, position = c(0,0,1,0.35)) print(plot.density, position = c(0.0988,0.22,0.98,1), newpage = FALSE) } example.data <- data.frame(subject_ID = c('A01','B01','A02','B02'), subject_group = c('pop1','pop2'), task= c(rep('task1',32),rep('task2',32)),dependent_measure=rnorm(64)) plotdensitymeans(example.data, measure=c('dependent_measure'), xlab="dependent measure (units)", xlim=c(-3,3)) ##END Example --- Daniel Myall PhD Student Department of Medicine University of Otago, Christchurch Van der Veer Institute for Parkinson's and Brain Research 66 Stewart St Christchurch New Zealand daniel.myall at vanderveer.org.nz http://www.vanderveer.org.nz/
baptiste auguie
2009-Jan-25 11:04 UTC
[R] Multiple lattice plots on a page: aligning x-axes vertically
Have you tried c() from the latticeExtra package? It worked for me (see below)> library(grid) > library(lattice) > x <- seq(0, 10, length=100) > y <- sin(x) > y2 <- 10*sin(x) > f <- rep(c("1", "2"), each=50) > > p1 <- xyplot(y~x,groups=f, ylab="BIG LABEL", > # auto.key=list(space="right"), > par.settings = list(layout.width = list(panel=1, ylab = 2, axis.left > =1.0, left.padding=1, > ylab.axis.padding=1, axis.panel=1))) > > p2 <- xyplot(y2~x, ylab="a", > par.settings = list(layout.width = list(panel=1, ylab = 2, axis.left > =1.0, left.padding=1, > ylab.axis.padding=1, axis.panel=1))) > > library(latticeExtra) > > update(c(p2, p1, x.same = TRUE), > layout = c(1, 2), > ylab = list(c("a", "BIG LABEL"), y = c(1/6, 2/3)), > par.settings = list(layout.heights = list(panel = c(1, 2))))Hope this helps, baptiste On 25 Jan 2009, at 09:54, Daniel Myall wrote:> Dear R-help, > > I am creating a two lattice plots (a densityplot() and xyplot()) that > have the same x-axes and then 'printing' them onto the same page, one > above the other (see end of email for an example to generate the > graph). > With different labels on the y-axis for each plot the left spacing is > different, and the x-axes don't align vertically. Although I can > manually modify the print region of the plots on the page to align the > x-axes, this is not very robust; as can be expected changing the > size of > the plot device scales the x-axes differently in both plots. > Additionally, changing devices also causes issues (i.e., the plots are > aligned in quartz(width=12,height=6) but then slightly off with > pdf(width=12,height=6)). As I'm using this inside Sweave to generate > numerous plots of this form, I am after some robust way to vertically > align the x-axes of the plots. > > With the approach I'm taking is there a way with lattice or grid to > vertically align the x-axes? By somehow setting the internal plot > width > to be the same in both plots I think this would solve the issue > (possibly by setting the right parameters in > str(trellis.par.get("layout.widths")))? Alternatively, would another > approach be better (i.e., is it possible to create a new panel > function > that does a panel.xyplot and panel.densityplot on slightly different > data?). > > Thanks. > > Daniel > > > ## BEGIN Example > #OS: Mac OS X 10.5.5; R: 2.8.1; lattice 0.17-20 > > library(reshape) > library(lattice) > > plotdensitymeans <- > function(data,measure,factors=c('subject_group','task'), > xlab="",xlim=NULL, ...) { > > # Create means by subject > x.melted <- melt(data, id.var = append(factors,'subject_ID'), > measure.var=measure,na.rm=T) > formula.bysubject.cast <- paste(factors[1],"+",factors[2],"+ > subject_ID ~ .") > x.cast.subject <- data.frame(cast(x.melted,formula.bysubject.cast, > mean)) > > # Plot means by subject > text.formula <- paste(factors[1],":",factors[2],"~ X.all.") > text.group <- paste(factors[1],":",factors[2]) > formula.xyplot <- as.formula(text.formula) > formula.group <- as.expression(formula.xyplot) > > environment(formula.xyplot) <- environment() > plot.subject.means <- xyplot(formula.xyplot, > group=eval(formula.group), xlim=xlim, pch=16, alpha=0.6,data > x.cast.subject,xlab=xlab,ylab="") > > # Plot distributions > formula.densityplot <- as.formula(paste(" ~",measure[1])) > > environment(formula.densityplot) <- environment() > plot.density <- > densityplot(formula.densityplot,group=eval(formula.group),data=data, > xlim=xlim,n=200,auto.key=list(columns > 4,line=TRUE),plot.points=F,xlab="",lty=1, > scales=list(y=list(draw=FALSE),x=list(draw=FALSE))) > > # Plot both plots on a single page > print(plot.subject.means, position = c(0,0,1,0.35)) > print(plot.density, position = c(0.0988,0.22,0.98,1), newpage = > FALSE) > > } > > example.data <- data.frame(subject_ID = c('A01','B01','A02','B02'), > subject_group = c('pop1','pop2'), > task> c(rep('task1',32),rep('task2',32)),dependent_measure=rnorm(64)) > plotdensitymeans(example.data, measure=c('dependent_measure'), > xlab="dependent measure (units)", xlim=c(-3,3)) > > ##END Example > > --- > Daniel Myall > PhD Student > Department of Medicine > University of Otago, Christchurch > Van der Veer Institute for Parkinson's and Brain Research > 66 Stewart St > Christchurch > New Zealand > > daniel.myall at vanderveer.org.nz > http://www.vanderveer.org.nz/ > > ______________________________________________ > R-help at r-project.org 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._____________________________ Baptiste Augui? School of Physics University of Exeter Stocker Road, Exeter, Devon, EX4 4QL, UK Phone: +44 1392 264187 http://newton.ex.ac.uk/research/emag
Possibly Parallel Threads
- call lattice function in a function passing "groups" argument
- Problems drawing a colored 'rug' in the Lattice 'densityplot'
- Several densityplots in single figure
- How to generate a figure using par( ) with some densityplot( )'s
- small issue with densityplot