有没有一种优雅的方法来将tableGrob行与轴断开对齐?
我想在R中并置一个tableGrob和ggplot图表(我需要重现以前版本的公共报告中使用的一些SAS输出).像这个最小的可重复的例子:
这篇文章让我相当远 - tableGrob与图表的主体在同一个gtable行中; 但是,它需要大量的手动调整才能使tableGrob中的行与轴标签对齐.
我也找到了这篇文章.由于我正在发布一份公开报告,我宁愿不使用CRAN软件包中不易获得的代码.话虽如此,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, …
Run Code Online (Sandbox Code Playgroud)