向R中的grid.table PDF输出添加徽标

时间:2018-11-13 10:50:51

标签: r pdf-generation gridextra grob

我有一个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()

此刻我得到了一个合理的输出,但是我想在生成的每个页面上添加一个徽标。

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

1 个答案:

答案 0 :(得分: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()