[R] align two lattice plots using grid

baptiste auguie ba208 at exeter.ac.uk
Thu Nov 20 21:16:11 CET 2008

Dear list,

I'm trying to get two lattice plots aligned on a page. They should  
share a common x axis, hence the need for perfect alignment, but the  
data is taken from unrelated, separate sources (it is therefore  
inappropriate to combine them and use facetting to get an automatic  
layout: the legend and y axis need to be different). I can get the  
panels to be of the same size, but they have an horizontal offset due  
to a differing size of the legend / ylabel.

Here's an example of what I've tried,

x <- seq(0, 10, length=100)
y <- sin(x)
f <- rep(c("1", "2"), each=50)

p1 <- xyplot(y~x,groups=f, ylab="BIG LABEL",  
p2 <- xyplot(y~x, ylab="a")

pushViewport(viewport(layout=grid.layout(nrow = 2, ncol = 1 ,
						widths=unit(6, "inches"),
                           heights=unit(c(3, 2), "inches"))))
pushViewport(viewport(layout.pos.col=1, layout.pos.row=1, just="center",
name = "top"))

print(p1, newpage=F, panel.width=list(4, "inches"),  
panel.height=list(2.5, "inches"))
pushViewport(viewport(layout.pos.col=1, layout.pos.row=2, just="center",
name = "bottom"))

print(p2, newpage=F, panel.width=list(4, "inches"),  
panel.height=list(1.5, "inches"))

I'm sure there is a clever trick to get the panels to be aligned as  
opposed to the whole plots, but I can't see it.

Many thanks,



Baptiste Auguié

School of Physics
University of Exeter
Stocker Road,
Exeter, Devon,

Phone: +44 1392 264187


More information about the R-help mailing list