R - adding page numbers to PDF

后端 未结 4 1001
天涯浪人
天涯浪人 2021-01-14 19:26

I\'m having trouble adding page numbers to PDFs. Here\'s how I\'m inserting pages / plots:

pdf( file = pdfFilePath , width = 11 , height = 8.5  )
for ( ...          


        
4条回答
  •  半阙折子戏
    2021-01-14 20:25

    As mentioned by @Gavin, I also encountered the error when mixing ggplot with mtext.

    Here is what works really nicely for me when using ggplot:

    require(ggplot2)
    require(grid)
    printWithFooter = function(gg_obj, bottom_left = NULL, bottom_right = NULL) 
    {
      print(gg_obj)
      if (!is.null(bottom_right))
      {
        label = textGrob(bottom_right,
                         x = 0.98,  # right side
                         y = 0.0,   # bottom
                         just="right", 
                         hjust = NULL,
                         vjust = -.5,
                         gp=gpar(fontsize=7, col="#333333"))
        grid.draw(label)
      }
      if (!is.null(bottom_left))
      {
        label = textGrob(bottom_left,
                         x = 0.02,  # left side
                         y = 0.0,   # bottom
                         just="left", 
                         hjust = NULL,
                         vjust = -.5,
                         gp=gpar(fontsize=7, col="#333333"))  
        grid.draw(label)
      }
    }
    
    ## example
    
    d = data.frame(x = runif(1:20), y = runif(1:20), facet = rep(1:4, each=5))
    p = ggplot(d) + geom_point(aes(x=x, y=y)) + facet_wrap(~facet)
    printWithFooter(p, "left", "right")
    

提交回复
热议问题