使用 ggarrange 添加行和列标题

add row and column titles with ggarrange

我有一个 ggplots 列表,可能太复杂而无法使用 facet_wrap 进行排列。所有图必须共享相同的图例,并应排列在网格中。网格的每一列需要不同的标题,网格的每一行也需要不同的标题。
一个非常简单的例子:

library(ggplot2)
library(ggpubr)

plot1<- ggplot() + geom_point(aes(x=1, y=1, col="a"))
plot2<- ggplot() + geom_point(aes(x=1, y=1, col="a"))
plot3<- ggplot() + geom_point(aes(x=1, y=1, col="a"))
plot4<- ggplot() + geom_point(aes(x=1, y=1, col="a"))

plotlist<- list(plot1, plot2, plot3, plot4)

ggarrange(plotlist = plotlist, ncol = 2, nrow = 2, common.legend = TRUE, legend="bottom")

这会生成除列标题和行标题之外的所有所需内容,并且 annotate_figure 只会向图中添加全局标题。所需的输出应如下所示:

我更喜欢 patchwork 包。控制布局很容易。标题有点棘手。 Patchwork 使用每个图中的实验室 - 因此您可以将图标题添加到上图,并可能在左侧图中的 y 标签中添加一行。

可以说更容易的是将标题创建为情节并将它们添加到您的拼凑作品中。您可以轻松自定义您的布局,例如 explained in the patchwork vignette

library(ggplot2)
library(patchwork)

plot1<-plot2<-plot3<-plot4<- ggplot() + geom_point(aes(x=1, y=1, col="a"))

row1 <- ggplot() + annotate(geom = 'text', x=1, y=1, label="row 1 title", angle = 90) + theme_void() 

row2 <- ggplot() + annotate(geom = 'text', x=1, y=1, label="row 2 title", angle = 90) + theme_void() 

col1 <- ggplot() + annotate(geom = 'text', x=1, y=1, label="col 1 title") + theme_void() 

col2 <- ggplot() + annotate(geom = 'text', x=1, y=1, label="col 2 title") + theme_void() 

layoutplot <- "
#cccddd
aeeefff
aeeefff
bggghhh
bggghhh
"

plotlist <- list(a = row1, b = row2, c = col1, d = col2, e= plot1, f=plot2, g=plot3, h=plot4)

wrap_plots(plotlist, guides = 'collect', design = layoutplot)

reprex package (v0.3.0)

于 2020 年 2 月 22 日创建