add row and column titles with ggarrange

Dar*_*cha 5 ggplot2 ggpubr

I have a list of ggplots that may be too complex to arrange using facet_wrap. All plots must share the same legend and should be arranged in a grid. Each column of the grid needs a different title, also each row of the grid needs a differen title.
An absurdly simple example:

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")
Run Code Online (Sandbox Code Playgroud)

This produces everything needed except the column and row titles, and annotate_figure only adds a global title to the figure. The desired output should look like:

带有行和列标题的 ggarrange

小智 6

我知道现在回复已经晚了,但对于未来的搜索者(包括未来的我):该ggpubr::annotate_figure功能可能是减少跨包问题的最佳选择。

再现:

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)
Run Code Online (Sandbox Code Playgroud)

这是您将其添加到的位置:

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

annotate_figure(commonplot, 
                top = "Column 1 Title                    Column 2 Title",
                left = "Row 2 Title                   Row 1 Title")
Run Code Online (Sandbox Code Playgroud)

您可以将ggarrange对象指定为对象,也可以通过管道将其传递到annotate_figure.

文档还显示了更多功能,例如添加右侧和底部标签以及通用标题。

上述代码的显示。


Tje*_*ebo 5

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

可以说更简单的方法是将标题创建为情节并将它们添加到您的拼凑作品中。您可以轻松自定义布局,如拼凑小插图中所述

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)
Run Code Online (Sandbox Code Playgroud)

由reprex 包(v0.3.0)于 2020-02-22 创建