R-将页码添加到PDF

|| 我在将页码添加到PDF时遇到问题。这是我插入页面/图的方式:
pdf( file = pdfFilePath , width = 11 , height = 8.5  )
for ( ... ) {
    grid.newpage()
    pushViewport( viewport( layout = grid.layout( 2 , 2 ) ) )
    ... print 4 plots ....
}
onefile似乎用页码来命名文件,但是我希望页码出现在同一文件中。 编辑 我已经修改了@Gavin的代码示例,以生成混合图形类型以获取页码的工作版本:
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 <- ggplot(data.frame(X = 1:10, Y = runif(10)), aes(x = X, y = Y)) + 
        geom_point()
vplayout <- 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()
    
已邀请:
在对Q的第二点评论中,我提出了使用ѭ2a的基本图形解决方案。这似乎适用于OP,因此我在此处显示扩展版本: 基本图形:
op <- par(oma = c(2,0,0,0))
layout(matrix(1:4, ncol = 2))
plot(1:10)
plot(1:10)
plot(1:10)
plot(1:10)
mtext(side = 1, text = \"Page 1\", outer = TRUE)
layout(1)
par(op)
导致: @ SFun28报道了这个想法也适用于他的ggplot / grid图形,但是对我来说不行。运行下面的代码块的最后一行后,出现以下错误:
> mtext(side = 1, text = \"Page 1\")
Error in mtext(side = 1, text = \"Page 1\") : 
  plot.new has not been called yet
这表示警告不要混合基础图形和网格图形。
require(ggplot2)
p1 <- ggplot(data.frame(X = 1:10, Y = runif(10)), aes(x = X, y = Y)) + 
        geom_point()
vplayout <- function(x, y) {
    viewport(layout.pos.row = x, layout.pos.col = y)
}
grid.newpage()
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(side = 1, text = \"Page 1\")
    
我修改了Paul Murrell的R Graphics书中的一个示例,该示例使用网格绘制了整个图,然后在单独的视口的底部放置了一个标签。我不愿对OP进行微调,因为我不知道各个图在做什么,但是在绘制标签的设备底部创建一个额外的视口(?)的总体思路应该映射到@ SFun28已经使用的ѭ6个想法:
label <- textGrob(\"A page number! \",
                  x=0.5, y = 1.0, just=\"centre\")
x <- seq(0.1, 0.9, length=50)
y <- runif(50, 0.1, 0.9)
gplot <- 
  gTree(
    children=gList(rectGrob(gp=gpar(col=\"grey60\",
                                    fill=\"white\")),
                   linesGrob(x, y), 
                   pointsGrob(x, y, pch=16, 
                              size=unit(1.5, \"mm\"))),
    vp=viewport(width=unit(1, \"npc\") - unit(5, \"mm\"), 
                height=unit(1, \"npc\") - unit(10, \"mm\")))



layout <- grid.layout(2, 1,
                      widths=unit(c(1, 1), 
                                  c(\"null\", \"grobwidth\"),
                                  list(NULL, label)),
                      heights = unit(c(1, 1),
                                     c(\"null\", \"grobheight\"),
                                     list(NULL, label)))

grid.rect(gp=gpar(col=\"grey60\", fill=\"grey90\"))
pushViewport(viewport(layout=layout))
pushViewport(viewport(layout.pos.row=2))
grid.draw(label)
popViewport()
pushViewport(viewport(layout.pos.col=1))
grid.draw(gplot)
popViewport(2)
这使:     
library(ggplot2)
plots = replicate(8, qplot(1,1))
library(gridExtra)
p <- 
do.call(marrangeGrob, c(plots, list(ncol=2, nrow=2, top =NULL, 
        bottom = quote(quote(paste(g, \"/\",pages))))))

p

ggsave(\"multipage.pdf\", p)
(以某种方式保持懒惰的评估需要9英镑)     
如@Gavin所述,将ggplot与mtext混合时,我也遇到了错误。 这是使用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\")
    

要回复问题请先登录注册