Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

R: plot in a grid layout over multiple pages

Tags:

r

r-grid

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!

like image 409
Mike Dewar Avatar asked Nov 01 '10 20:11

Mike Dewar


People also ask

How do you make a grid plot in R?

Creating a Grid of Plots To do this, you use the parameter value mfrow=c(x,y) where x is the number of rows that you wish to have in your plot and y is the number of columns. When you plot, R will place each plot, in order by row within the grid that you define using mfrow .

How do I arrange multiple Ggplots?

To arrange multiple ggplot2 graphs on the same page, the standard R functions - par() and layout() - cannot be used. The basic solution is to use the gridExtra R package, which comes with the following functions: grid. arrange() and arrangeGrob() to arrange multiple ggplots on one page.

Can you combine two plots in R?

It is straightforward to combine plots in base R with mfrow and mfcol graphical parameters. You just need to specify a vector with the number of rows and the number of columns you want to create.


1 Answers

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.

like image 176
Richie Cotton Avatar answered Sep 21 '22 17:09

Richie Cotton