I am trying to get a multi-page two-dimensional lattice panel plot to have the same conditioning layout on multiple pages. The vertical arrangement should be
CCC
BBB
AAA
on each page. I know how to do this manually, but the code is ugly, especially if the last page is not completely filled.
In a 2010 SO thread, I read that this was “on the list” for ggplot2.
library(lattice)
d = expand.grid(f1 = as.factor(letters[1:10]),
f2 = as.factor(LETTERS[1:3]),
x = 0:10)
d$y = rnorm(nrow(d))
xyplot(y~x|f1+f2,data=d,cex=0.5,pch=16,layout=c(5,3,2))
Note that all A are on the first page, followed by B, followed by B and C on the last page.
Edited following Gabor’s idea
library(lattice)
library(latticeExtra)
# Note: changed so that it does not fill the three pages
d <- expand.grid(f1 = as.factor(letters[1:8]),
f2 = as.factor(LETTERS[1:3]),
x = 0:10)
d$y <- rnorm(nrow(d))
page <- factor((as.numeric(d$f1) - 1) %/% 5 + 1)
# The second (=last) page has different panel sizes
# Using aspect does not help
for(pg in levels(page)) {
p <- xyplot(y ~ x|f1+f2, data = d[pg == page, ], cex = .5, pch = 16,
layout = c(5, 3))
print(useOuterStrips(p))
}
The placement of the panels can be achieved by writing a new
packet.panelfunction that automatically draws the panels in the desired places.useOuterStripswill change the layout, but it can be changed back afterwards. The desired result can be achieved like this: