我在向 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 的第二个评论中,我建议了一个使用 mtext()
的基本图形解决方案。这似乎适用于 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")
关于R - 将页码添加到 PDF,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/5735541/