[R] Multiple lattice plots on a page: aligning x-axes vertically

Daniel Myall daniel.lists at zeno.co.nz
Sun Jan 25 10:54:38 CET 2009


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/




More information about the R-help mailing list