Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/r/74.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/variables/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何使用tableGrob显示表格的标题?_R_Gridextra_Grob - Fatal编程技术网

如何使用tableGrob显示表格的标题?

如何使用tableGrob显示表格的标题?,r,gridextra,grob,R,Gridextra,Grob,以这个例子为例 library(gridExtra) library(grid) d1 <- head(iris[,1:3]) %>% as_tibble() d2 <- head(iris[,2:5]) %>% as_tibble() grid.arrange(tableGrob(d1), tableGrob(d2)) 库(gridExtra) 图书馆(网格) d1%为不可抗力() d2%作为不可抗力() 网格排列(表格B(d1), 表B

以这个例子为例

library(gridExtra)
library(grid)

d1 <- head(iris[,1:3]) %>% as_tibble()
d2 <- head(iris[,2:5]) %>% as_tibble()

grid.arrange(tableGrob(d1),
             tableGrob(d2))
库(gridExtra)
图书馆(网格)
d1%为不可抗力()
d2%作为不可抗力()
网格排列(表格B(d1),
表B(d2))

这真的很好,但我错过了数据帧的标题!如何将它们自动添加到图片中


谢谢

您可以使用
ggplot

library(ggplot2)

dd1 <- ggplot() + annotation_custom(tableGrob(d1)) + labs(title = 'd1')
dd2 <- ggplot() + annotation_custom(tableGrob(d2)) + labs(title = 'd2')
grid.arrange(dd1, dd2, nrow = 2)
库(ggplot2)

dd1我建议在gtable中添加一个textGrob

library(magrittr)

add_title <- function(g, title, padding = unit(2,"mm"), lpos=1, ...){
  tg <- textGrob(title, ...)
  g %>%
    gtable::gtable_add_rows(heights = grobHeight(tg) + padding, pos = 0L) %>% 
    gtable::gtable_add_grob(tg, t=1,b=1,l=lpos,r=ncol(g))

}


grid.arrange(tableGrob(d1) %>% add_title('a) First title', gp=gpar(fontsize=14, fontface=4), hjust=0, x=0, lpos=2),
             tableGrob(d2) %>% add_title('b) Second title', gp=gpar(fontsize=14, fontface=4), hjust=0, x=0, lpos=2))
库(magrittr)
添加标题%
gtable::gtable_add_grob(tg,t=1,b=1,l=LPO,r=ncol(g))
}
grid.arrange(表格GROB(d1)%>%添加标题(“a”)第一个标题,gp=gpar(fontsize=14,fontface=4),hjust=0,x=0,lpos=2),
tableGrob(d2)%%>%add_title('b)Second title',gp=gpar(fontsize=14,fontface=4),hjust=0,x=0,lpos=2))

相当不错!!!只是跟进一下。假设我有几十个这样的小数据帧。有没有办法将
marrangeGrob
tableGrob
一起使用,这样我就可以得到一份每页有4x4个图表的pdf?我不确定。昨天我真的遇到了
annotation\u custom
tableGrob
好的,谢谢!你知道如何去除灰色背景吗?看起来有点奇怪!>)。如果遇到任何问题,请告诉我