title: R - adding page numbers to PDF
body: <p>I'm having trouble adding page numbers to PDFs.  Here's how I'm inserting pages / plots:</p>
  
  <pre><code>pdf( file = pdfFilePath , width = 11 , height = 8.5  )
  for ( ... ) {
      grid.newpage()
      pushViewport( viewport( layout = grid.layout( 2 , 2 ) ) )
      ... print 4 plots ....
  }
  </code></pre>
  
  <p>onefile seems to name a file by the page number, but I want the page numbers to appear in the same file.</p>
  
  <p><b>Edit</b><br>
  I've modified @Gavin's code sample to produce a working version of mixing graphic types to get page numbers:</p>
  
  <pre><code>require(ggplot2)
  pdf( file = "FILE_PATH_TO_SAVE_PDF_HERE" , width = 11 , height = 8.5  )
  par( oma = c ( 4 , 4 , 4 , 4 ) , mar=c( 4 , 0 , 2 , 0 )  )
  plot( 0:11 , type = "n", xaxt="n", yaxt="n", bty="n", xlab = "", ylab = ""  )
  mtext( side = 3 , line = 0 , outer = TRUE  , cex = 1.5 , family="mono" , "Title" )
  grid.newpage()
  p1 &lt;- ggplot(data.frame(X = 1:10, Y = runif(10)), aes(x = X, y = Y)) + 
          geom_point()
  vplayout &lt;- function(x, y) {
      viewport(layout.pos.row = x, layout.pos.col = y)
  }
  pushViewport(viewport(layout = grid.layout(2, 2)))
  print(p1, vp = vplayout(1,1))
  print(p1, vp = vplayout(1,2))
  print(p1, vp = vplayout(2,1))
  print(p1, vp = vplayout(2,2))
  mtext( "1" , side = 1 , line = 3 , outer = TRUE , cex = .8 , family="mono"  )
  dev.off()
  </code></pre>