2011-04-20 94 views
3

我无法将页码添加到PDF中。下面是我如何插入页/图:R - 将页码添加到PDF

pdf(file = pdfFilePath , width = 11 , height = 8.5 ) 
for (...) { 
    grid.newpage() 
    pushViewport(viewport(layout = grid.layout(2 , 2))) 
    ... print 4 plots .... 
} 

onefile似乎被命名页码文件,但我想要的页码出现在同一个文件。

编辑
我修改@加文的代码示例产生混合图形类型,以获得页码的工作版本:

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() 
+2

这不求求你为什么不想使用Sweave或Sweave-like系统的一个问题生成你的PDF。你甚至不需要使用LaTeX - 如果LaTeX是进入的障碍,你可以使用odfWeave并将其转换为PDF。 – 2011-04-20 19:42:50

+1

...如果这是基础图形,我只需添加一个外边距('par(oma = c(3,0,0,0))')并通过'title(xlab =“pagenum”, outer = TRUE)'调用或'mtext(“pagenum”,side = 1,outer = TRUE)'调用。但是,我不知道有足够的网格图形来了解与之等效的步骤。 – 2011-04-20 19:51:59

+0

@Gavin - 我需要再看一下Sweave,但说实话,这似乎是一个非常基本的场景。如果R让我能够将多个页面打印到PDF,那么我希望能够立即添加页码。 – SFun28 2011-04-20 19:52:23

回答

2

从我在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) 

结果造成:

base graphics page number example

@ SFun28报告这个想法工程他的ggplot /网格图形,但它不适合我。在运行下面的代码块的最后一行后,我得到以下错误:

> 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") 
+0

看看编辑后的代码。想法?顺便说一句...... SO说我的编辑只会在经过同行评审后才会出现。 – SFun28 2011-04-20 23:02:49

+0

@ SFun28我无法在任何地方看到您的编辑,即使在mod/10K +代表可以批准来自代表不足的用户的编辑区域。你做过编辑了吗?也许再试一次? – 2011-04-21 07:58:02

+0

我重新发布了编辑(这次是原始问题 - 请看上面)。没有提交选项......显然,当人们编辑其他人的帖子时,SO不喜欢它。这就说得通了。 – SFun28 2011-04-21 16:27:58

2

我已经修改了保罗的Murrell的R Graphicsbook绘制的example整个绘图使用网格,然后在单独的视口底部放置一个标签。由于我不知道单个绘图是在做什么,因此我将其微调为OP,但在绘制标签的设备底部创建额外视口(?)的一般想法应该映射到grid.layout()已使用的@ SFun28思路:

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) 

其中给出:

A grid page number example

+0

对不起,这有点复杂,但正如我所说,我的网格功能很弱。如果有一些可重现的ggplot代码可以绘制出实际情节,我可能已经确信尝试一个完整的解决方案... – 2011-04-20 20:43:20

+0

@Gavin - 实际上,我认为您在问题评论中提出的解决方案是可行的。在绘制完所有绘图后,我只是添加了mtext(...),并将页码放入!仍然需要做更多的测试.... – SFun28 2011-04-20 20:50:40

+0

@ SFun28要小心 - 你不应该混合网格和基础图形。但如果它适合你,太好了! – 2011-04-20 20:53:36

1
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) 

(该quote(quote())是需要保持惰性计算,以某种方式)

0

正如@Gavin提到的,我还与多行文字混合ggplot时遇到的错误。

下面是使用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") 

Example result