R-将页码添加到PDF [英] R - adding page numbers to PDF
问题描述
我无法在PDF中添加页码.这是我插入页面/图的方式:
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 ( ... ) {
grid.newpage()
pushViewport( viewport( layout = grid.layout( 2 , 2 ) ) )
... print 4 plots ....
}
onefile似乎用页码命名文件,但我希望页码出现在同一文件中.
onefile seems to name a file by the page number, but I want the page numbers to appear in the same file.
编辑
我已经修改了@Gavin的代码示例,以生成混合图形类型以获取页码的工作版本:
Edit
I've modified @Gavin's code sample to produce a working version of mixing graphic types to get page numbers:
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,因此我在此处显示扩展版本:
From my second comment on the Q, I suggested a base graphics solution using mtext()
. This appears to work for the OP so I show an expanded version here:
基本图形:
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图形,但对我而言不行.运行下面的代码块的最后一行后,出现以下错误:
@SFun28 reports this idea works for his ggplot/grid graphics too, but it does not for me. After running the last line of the code chunk below I get the following error:
> mtext(side = 1, text = "Page 1")
Error in mtext(side = 1, text = "Page 1") :
plot.new has not been called yet
这表示警告,请注意不要混合基本图形和网格图形.
which is indicative of the warning not to mix base and grid graphics.
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的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!