1

我正在尝试使用R中的 gridExtra 包创建一个表,并且我希望在通用列名下有子列名。例如,有一个名为“Urbana-Champaign”的大列,它跨越两个较小的列名“元素”和“基因数”。我在 gridExtra 支持站点上到处查看,但似乎找不到创建包含子列的整体列名的方法。有谁知道怎么做?

4

1 回答 1

0

It's rather easy to get a basic gtable, and add new text to it, but you'd have to add all the formatting and styling of the cells. That's where I always give up -- way too many parameters and options to take care of.

library(gtable)
gtable_add_grobs <- gtable_add_grob #misleading name

d <- head(iris, 3)

extended_matrix <- cbind(c("", rownames(d)), rbind(colnames(d), as.matrix(d))) 

all_grobs <- matrix(lapply(extended_matrix, textGrob), ncol=ncol(d) + 1)

row_heights <- function(m){
  do.call(unit.c, apply(m, 1, function(l)
    max(do.call(unit.c, lapply(l, grobHeight)))))
}

col_widths <- function(m){
  do.call(unit.c, apply(m, 2, function(l)
    max(do.call(unit.c, lapply(l, grobWidth)))))
}

g <- gtable_matrix("table", grobs=all_grobs,
                   widths=col_widths(all_grobs) + unit(4,"mm"), 
                   heights=row_heights(all_grobs) + unit(4,"mm"))

g <- gtable_add_rows(g, unit(1, "line"), 0)
g <- gtable_add_grobs(g, list(textGrob("Sepal's main title"), 
                              textGrob("Petal's main title"))
                     t=1,b=1,l=c(2, 4), r=c(3, 5))

grid.newpage()
grid.draw(g)

enter image description here

于 2013-07-24T00:07:10.483 回答