2011-04-20 90 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