Question

I'd like to arrange a 3x3 grid layout over 3 pages of a PDF file. I'd like to plot in arbitrary grid locations across the three pages. I know how to arrange multiple subplots using some option like layout=c(3,3). I can figure out how to arrange a 3x3 layout on a single plot using the grid package, and then decide to choose which plot to use. However, I can't figure out how to lay out a 3x3 grid over 3 pages, and then choose which grid to plot in.

I was hoping grid.newpage() would solve my problem, as in the following:

library(grid)
pdf(file="griddtest.pdf",paper="letter")
vp1 <- viewport(x = 0, y = 0.5, w = 0.5, h = 0.5, just = c("left", "bottom"), 
    name = "vp1")
vp2 <- viewport(x = 0, y = 0.5, w = 0.5, h = 0.5, just = c("left", "bottom"), 
    name = "vp2")
pushViewport(vp1)
grid.text("Some drawing in graphics region 1 on page 1",y = 0.8)
grid.newpage()
pushViewport(vp2)
grid.text("Some drawing in graphics region 2 on page 2",y = 0.8)
dev.off()

but this just generates the second page (I'm guessing 'newpage' overwrites the oldpage, rather than making a new page).

Any help would be greatly appreciated!

Was it helpful?

Solution

If you have nine panels, and specify 3 panels in your layout, e.g.,

xyplot(runif(9) ~ 1:9 | 1:9, layout = c(1,3))

then you get 3 graphs drawn. For plotting into the GUI window the plots get overwritten, but if you are saving to PDF they appear on consecutive pages.


EDIT: To make the plots only take up one third of the page, adjust the height of the plot in the call to pdf.

pdf(..., height = 3)
# ...
dev.off()

By default, this draws each plot in the centre of each page. By setting pagecentre = FALSE, the plots appear at the bottom of each page. I haven't found an option in pdf to make them appear at the top.

To get better control over the positioning of the plots, first save them to eps (with the postscript function) or to png.

You can then use sweave to create a pdf formatted however you like, including those files. Alternatively, create a document in the word processor of your choice and manually import the image files.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top