5

有没有一种优雅的方法可以将 tableGrob 行与轴中断对齐?

我想在 R 中并列一个 tableGrob 和 ggplot 图表(我需要重现以前版本的公共报告中使用的一些 SAS 输出)。就像这个最小的可重现示例:

并列 tableGrob 和 ggplot 图表

这篇文章让我走得很远 --- tableGrob 与图表主体在同一 gtable 行中;但是,它需要大量手动操作才能使 tableGrob 中的行与轴标签对齐。

我也找到了这篇文章。由于我正在编写一份公开报告,因此我不希望使用 CRAN 包中不易获得的代码。话虽如此,tableGrob 的实验版本似乎接受高度作为参数。如果这段代码可以解决问题,并且我确实选择使用这个实验版本,我将如何计算适当的行高?

如果没有一种优雅的方式来做到这一点,我发现这些技巧很有帮助:

  • 在 tableGrob 中设置 fontsize 和 cex 以匹配 ggplot2
  • 将 padding.v 设置为 tableGrob 中的空间表行
  • 修改坐标限制以适应列标签并与最后一行的底部对齐

我的 MRE 代码:

library(ggplot2)
library(gridExtra)
library(gtable)

theme_set(theme_bw(base_size = 8))
df <- head(mtcars,10)
df$cars <- row.names(df)
df$cars <- factor(df$cars, levels=df$cars[order(df$disp, decreasing=TRUE)], ordered=TRUE)
p <- ggplot(data=df, aes(x=hp, y=cars)) +
  geom_point(aes(x=hp, y=cars)) +
  scale_y_discrete(limits=levels(df$cars))+
  theme(axis.title.y = element_blank()) +
  coord_cartesian(ylim=c(0.5,length(df$cars)+1.5))
t <- tableGrob(df[,c("mpg","cyl","disp","cars")],
               cols=c("mpg","cyl","disp","cars"),
               gpar.coretext = gpar(fontsize = 8, lineheight = 1, cex = 0.8),
               gpar.coltext = gpar(fontsize = 8, lineheight = 1, cex = 0.8),
               show.rownames = FALSE,
               show.colnames = TRUE,
               equal.height = TRUE,
               padding.v = unit(1.65, "mm"))
g <- NULL
g <- ggplotGrob(p)
g <- gtable_add_cols(g, unit(2,"in"), 0)
g <- gtable_add_grob(g, t, t=3, b=3, l=1, r=1)

png('./a.png', width = 5, height = 2, units = "in", res = 100)
grid.draw(g)
dev.off()

出于故障排除目的,我已将汽车名称留在 y 轴中断处,但最终我将删除它们。

4

2 回答 2

5

现在有这个gtable_table的实验版本

在此处输入图像描述

table <- gtable_table(df[,c("mpg","cyl","disp","cars")], 
                      heights = unit(rep(1,nrow(df)), "null"))

g <- ggplotGrob(p)
g <- gtable_add_cols(g, sum(table$widths), 0)
g <- gtable_add_grob(g, table, t=3, b=3, l=1, r=1)

grid.newpage()
grid.draw(g)
于 2014-06-21T19:57:42.597 回答
2

@Baptiste 的答案扩展为演示列标签和单元格参数:

library(ggplot2)
library(gridExtra)
## I manually changed the dependency on 
install.packages(".//gtable_0.2.tar.gz", repos = NULL, type="source")

## The forked version of gtable requires R version 3.2.0 
## which is currently in development (as of 9/17/2014) due to change in grid 
## (https://github.com/wch/r-source/commit/850a82c30e91feb47a0b6385adcbd82988d90413)
## I have not installed the development version.
## However, I was able, in limited testing, to get this to work with R 3.1.0
## and ggplot2_1.0.0
## YRMV
## The following code, commented out, may be more useful with release of R 3.2.0
## library(devtools)
## devtools::install_github("baptiste/gtable")
library(gtable)

theme_set(theme_bw(base_size = 10))

df <- mtcars
df$cars <- row.names(df)
df <- head(df[,c("mpg","cyl","disp","cars")],10)
df$cars <- factor(df$cars, levels=df$cars[order(df$disp, decreasing=TRUE)], ordered=TRUE)

p <- ggplot(data=df, aes(x=disp, y=cars)) +
     geom_point(aes(x=disp, y=cars)) +
     scale_y_discrete(limits=levels(df$cars))+
     theme(axis.title.y = element_blank()) +
     coord_cartesian(ylim = c(0.5,nrow(df)+1))

core <- gtable_table(df[order(df$disp, decreasing=FALSE),],
                   fg.par = list(fontsize=c(8)),
                   bg.par = list(fill=c(rep("lightblue",4),rep("white",4)), alpha=0.5),
                   heights = unit(rep(1,nrow(df)), "null"))

colHead <- gtable_table(t(colnames(df)),
                    fg.par = list(fontsize=c(8)),
                    bg.par = list(fill=c(1), alpha=0.2),
                    heights = unit(0.5, "null"))
table1 <- rbind(colHead, core)

g <- ggplotGrob(p)
g <- gtable_add_cols(g, sum(table1$widths), 0)
g <- gtable_add_grob(g, table1, t=3, b=3, l=1, r=1)

grid.newpage()
grid.draw(g)

在此处输入图像描述

于 2014-09-17T18:42:28.030 回答