R-向 PDF 添加页码

发布于 2024-11-02 17:13:59 字数 1142 浏览 5 评论 0原文

我在向 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()

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 seems to name a file by the page number, but I want the page numbers to appear in the same file.

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

要走就滚别墨迹 2024-11-09 17:13:59

从我对 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")

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:

Base Graphics:

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)

Resulting in:

base graphics page number example

@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")
演出会有结束 2024-11-09 17:13:59

我修改了 Paul Murrell 的 示例 < a href="http://www.stat.auckland.ac.nz/~paul/RGraphics/rgraphics.html" rel="nofollow noreferrer">R 图形 绘制了整个使用网格进行绘图,然后将标签放置在单独视口的底部。我将微调留给OP,因为我不知道各个图在做什么,但是在绘制标签的设备底部创建一个额外的视口(?)的总体思路应该映射到 < code>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

I have modified an example from Paul Murrell's R Graphics book that draws an entire plot using grid and then places a label at the bottom in a separate viewport. I leave the fine tuning to the OP as I don't know what the individual plots are doing, but the general idea of creating an extra viewport (?) across the bottom of the device into which the label is drawn should map onto the grid.layout() ideas already used by @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)

Which gives:

A grid page number example

猫弦 2024-11-09 17:13:59
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()) 来维持惰性求值,以某种方式)

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)

(the quote(quote()) is needed to maintain lazy evaluation, somehow)

脱离于你 2024-11-09 17:13:59

正如@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")

结果示例

As mentioned by @Gavin, I also encountered the error when mixing ggplot with mtext.

Here is what works really nicely for me when using 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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文