Dear list, I need to align two plots on top of each other for comparison (they only have the x-axis in common). When the y-labels have a different extent, I cannot find a way to align the x-axes, as illustrated 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))) > > 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")) > upViewport() > 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")) > upViewport()Any help would be greatly appreciated! baptiste _____________________________ 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
On Mon, Dec 1, 2008 at 8:13 AM, baptiste auguie <ba208 at exeter.ac.uk> wrote:> Dear list, > > I need to align two plots on top of each other for comparison (they only > have the x-axis in common). When the y-labels have a different extent, I > cannot find a way to align the x-axes, as illustrated 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))) >> >> 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")) >> upViewport() >> 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")) >> upViewport()The most transparent approach is to manipulate your data into a suitable format; here's a crude first attempt: xyplot(c(y2, y) ~ c(x, x) | gl(2, 100), groups = rep(c("1", "2"), c(150, 50)), scales = list(y = "free"), layout = c(1, 2), strip = FALSE) In general, the latticeExtra package has some tools to combine arbitrary trellis objects (thanks to Felix Andrews): 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)))) -Deepayan
Many thanks, this tool from latticeExtra does exactly what I was trying to achieve! Best wishes, Baptiste On 1 Dec 2008, at 20:06, Deepayan Sarkar wrote:> > In general, the latticeExtra package has some tools to combine > arbitrary trellis objects (thanks to Felix Andrews): > > 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)))) > > -Deepayan