gpt4 book ai didi

ggplot2 - 使用 ggarrange 添加行和列标题

转载 作者:行者123 更新时间:2023-12-03 19:16:20 26 4
gpt4 key购买 nike

我有一个 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 仅向图形添加全局标题。所需的输出应如下所示:

ggarrange with row and column titles

最佳答案

我更喜欢 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)



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

关于ggplot2 - 使用 ggarrange 添加行和列标题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60347583/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com