6

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!

Richie Cotton
  • 118,240
  • 47
  • 247
  • 360
Mike Dewar
  • 10,945
  • 14
  • 49
  • 65
  • You cannot operate on graphics on previous page. You will probably just have to rethink the way you are plotting the things. – VitoshKa Nov 02 '10 at 06:34
  • Your example works for me: I get both pages. (Tested in R2.12.0 and R2.11.1 under WinXP) – Richie Cotton Nov 02 '10 at 11:14
  • @Richie Argh! How frustrating... – Mike Dewar Nov 02 '10 at 13:32
  • @Vitoshka I'm not sure I'm operating on graphics on a previous page in the example... If I am can you tell me how? I think I draw something on page 1 (vp1) then make a new page, then draw something on the new page... – Mike Dewar Nov 02 '10 at 13:34
  • Well, I understood from "I can't figure out how to lay out a 3x3 grid over 3 pages, and then choose which grid to plot in." that you want to plot on previous page. Now I see you don't want that. Your example works perfectly for me. I get 2 pages as expected. – VitoshKa Nov 02 '10 at 14:32
  • 2
    There is onefile argument in pdf. Try to set it explicitly. – VitoshKa Nov 02 '10 at 14:36
  • @Vitoshka thanks! My brain and my programming seem to operate independently a lot of the time... – Mike Dewar Nov 05 '10 at 14:31

1 Answers1

1

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.

Richie Cotton
  • 118,240
  • 47
  • 247
  • 360
  • Can I control the layout of these panels so they only appear in the top third of the page? This was my brief... – Mike Dewar Nov 02 '10 at 13:34
  • @Mike, if you use latex (sweave) you can position your floats wherever you want (http://en.wikibooks.org/wiki/LaTeX/Floats,_Figures_and_Captions) – VitoshKa Nov 02 '10 at 14:37
  • @VitoshKa this, were it up to me, is exactly what I'd do. My brief, though, is to use R. Thanks for the help! – Mike Dewar Nov 05 '10 at 14:33