更改 table 中所有现有 grob 的字体大小

Change font size of all existing grobs in table

以下是 baptiste 提供的一些示例代码,展示了如何修改单个 grob 元素的字体大小。

g <- tableGrob(iris[1:4, 1:3])
find_cell <- function(table, row, col, name="core-fg"){
  l <- table$layout
  which(l$t==row & l$l==col & l$name==name)
}

ind <- find_cell(g, 3, 2, "core-fg")
ind2 <- find_cell(g, 2, 3, "core-bg")
g$grobs[ind][[1]][["gp"]] <- gpar(fontsize=15, fontface="bold")
g$grobs[ind2][[1]][["gp"]] <- gpar(fill="darkolivegreen1", col = "darkolivegreen4", lwd=5)
grid.draw(g)

我该怎么做才能把我的 table 中的所有内容都弄乱?

我尝试使用

g$grobs["gp"] <- gpar(fontsize=15, fontface="bold")

但这不是正确的语法。

你可以这样做:

lapply(1:length(g$grobs),function(i){g$grobs[[i]]$gp$fontsize <<- 35})
grid.draw(g)