12

I'm using the following code to create three sets of plots in the amazing package ggplot2:

w<-rnorm(100)
x<-rnorm(100)
y<-rnorm(100)
z<-rnorm(100)
g<-rep(factor(LETTERS[1:4]), 25)
d<-data.frame(g,w,x,y,z)

library(ggplot2)

pw<-ggplot(d, aes(w, y))
px<-ggplot(d, aes(x, y))
pz<-ggplot(d, aes(z, y))

pw+geom_point()+facet_grid(.~g, scales='fixed')+coord_equal()+stat_smooth(method='lm')
px+geom_point()+facet_grid(.~g, scales='fixed')+coord_equal()+stat_smooth(method='lm')
pz+geom_point()+facet_grid(.~g, scales='fixed')+coord_equal()+stat_smooth(method='lm')

I would make a PDF file that has each of these three sets of plots printed on the same page. My understanding is thatsplit.screen(c(3,1))andpar(mfrow=c(3,1))won't work with ggplot2 graphics, but thatgrid.layout()from the grid package would work so I tried:

pdf(file="test.pdf")
pushViewport(viewport(layout=grid.layout(3,1)))
print(pw+geom_point()+facet_grid(.~g, scales='fixed')+coord_equal()+stat_smooth(method='lm'))
print(px+geom_point()+facet_grid(.~g, scales='fixed')+coord_equal()+stat_smooth(method='lm'))
print(pz+geom_point()+facet_grid(.~g, scales='fixed')+coord_equal()+stat_smooth(method='lm'))
dev.off()

but this ends up being a four page PDF file with the first page being blank and each set of figures following one per page and the x-axis label way down at the bottom. Is there a way to make a PDF file with all the sets of figures on the same page (and without a blank page leading!)?

smillig
  • 5,073
  • 6
  • 36
  • 46
  • 4
    `ggplot2:::print.ggplot` has a `vp` argument to specify the optional viewport; if left `NULL` ggplot2 will call `grid.newpage()` (unless you tell it not to with `newpage=FALSE`) and use the full page (default viewport). You need to use `print(p, vp=viewport(layout.pos.row = 1, layout.pos.col = 1))` to place your plot in the layout. – baptiste Jan 17 '12 at 19:17
  • Perfect! This is exactly what I wanted (obviously, changing `layout.pos.row` to 2 and 3 for the subsequent plots. Thank you! – smillig Jan 17 '12 at 19:35

3 Answers3

20

You'll probably have a better time using grid.arrange(), from the gridExtra package:

p1 <- pw + geom_point() + facet_grid(.~g, scales='fixed') + coord_equal() +   
      stat_smooth(method='lm')
p2 <- px + geom_point() + facet_grid(.~g, scales='fixed') + coord_equal() + 
      stat_smooth(method='lm')
p3 <- pz + geom_point() + facet_grid(.~g, scales='fixed') + coord_equal() +   
      stat_smooth(method='lm')

grid.arrange(p1, p2, p3, ncol=1)

enter image description here

Josh O'Brien
  • 159,210
  • 26
  • 366
  • 455
2

You can also use the multiplot() function, which could be customized to suite your needs: http://www.cookbook-r.com/Graphs/Multiple_graphs_on_one_page_(ggplot2)/

Alex Popov
  • 3,726
  • 1
  • 17
  • 20
1

If you use markdown, use fig.height in each code chunk for each plot:

```{r pw, fig.height = 2.66, echo = F}
pw
```
```{r px, fig.height = 2.66, echo = F}
px
```
```{r pz, fig.height = 2.66, echo = F}
pz
```
Tunn
  • 1,506
  • 16
  • 25