0

我有一个 pdf 格式的表格输出,我想对其进行自定义以符合公司主题。但是,我是 R 中这个领域的新手,仍然发现在添加徽标时很难找到自己的脚。

我的原始数据集由 600 多行数据组成,并且是敏感的,所以我使用了一个示例数据集来演示。到目前为止,我已经使用 grid 和 gridExtra 包获得了以下代码:

library(grid)
library(gridExtra)

Data <- data.frame(Staff = c("Rod","Barry","Cheiny"),
               M1 = c(50,40,55),
               M2 = c(60,50,55),
               M3 = c(55,50,45))


maxrow <- c(35);
npages <- ceiling(nrow(Data)/maxrow);
pdf("Data.pdf", height = 11, width = 10)
idx <- seq(1, maxrow)
grid.table(Data, rows = NULL, theme = ttheme_minimal())
grid.text("data",gp = gpar(fontsize = 12,fontface = "bold",alpha = 0.5), 
           vjust = -40,
           hjust = -0.5)
for (i in 2:npages){
  grid.newpage();
  if(i*maxrow <= nrow(Data)) {
    idx <- seq(1+((i-1)*maxrow), i*maxrow)
  }else{
    idx <- seq(1+((i-1)*maxrow), nrow(Data))
  }
  grid.table(Data, rows =NULL, theme = ttheme_minimal())
}
dev.off()

目前我得到了合理的输出,但我想为生成的每个页面添加一个徽标。

任何人都知道如何添加将在所有页面上重复的徽标?

4

1 回答 1

2

使用 grid.draw() 添加元素很容易,但设计取决于您

library(grid)
library(gridExtra)

Data <- data.frame(Staff = c("Rod","Barry","Cheiny"),
                   M1 = c(50,40,55),
                   M2 = c(60,50,55),
                   M3 = c(55,50,45))

library(png)
img <- readPNG(system.file("img", "Rlogo.png", package="png"))

footer <- grobTree(rectGrob(y=0,vjust=0,gp=gpar(fill="grey97",col=NA), height=unit(1,"in")),
                   textGrob(y=unit(0.5,"in"), expression(Corporate^TM~line~(c))),
                   rasterGrob(img, x=1, hjust=1,y=unit(0.5,"in"),height=unit(1,"in")-unit(2,"mm")))

maxrow <- c(35);
npages <- ceiling(nrow(Data)/maxrow);
pdf("Data.pdf", height = 11, width = 10)
idx <- seq(1, maxrow)
grid.table(Data, rows = NULL, theme = ttheme_minimal())
grid.draw(footer)
grid.text("data",gp = gpar(fontsize = 12,fontface = "bold",alpha = 0.5), 
          vjust = -40,
          hjust = -0.5)
for (i in 2:npages){
  grid.newpage();
  if(i*maxrow <= nrow(Data)) {
    idx <- seq(1+((i-1)*maxrow), i*maxrow)
  }else{
    idx <- seq(1+((i-1)*maxrow), nrow(Data))
  }
  grid.table(Data, rows =NULL, theme = ttheme_minimal())
  grid.draw(footer)
}
dev.off()
于 2018-11-14T18:41:12.897 回答