简体   繁体   English

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

[英]adding a logo to a grid.table PDF output in R

I have a table output in pdf format and I want to customise it to bring in line with a corporate theme. 我有一个pdf格式的表格输出,我想对其进行自定义以使其符合公司主题。 However, I'm new to this area in R and still finding it difficult to find my feet in adding logos. 但是,我是R语言领域的新手,但仍然很难在添加徽标中脱颖而出。

My original dataset is composed of over 600 rows of data and is sensitive, so I've used a sample dataset to demonstrate. 我的原始数据集包含600多行数据,并且非常敏感,因此我使用了一个示例数据集进行了演示。 So far, I've got the following code using the grid and gridExtra packages: 到目前为止,我已经使用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()

I'm getting a reasonable output at the moment, but I want to add a logo to each of the pages generated. 目前,我得到的输出是合理的,但我想在生成的每个页面上添加徽标。

Anyone know how to add a logo that will repeat across all the pages? 有人知道如何添加将在所有页面上重复的徽标吗?

It's easy to add elements with grid.draw(), but the design is up to you 使用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()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM