gpt4 book ai didi

r - 如何在使用 ggpubr 包中的 `ggarrange` 排列多个图后添加图例?

转载 作者:行者123 更新时间:2023-12-04 10:07:03 29 4
gpt4 key购买 nike

我必须使用 ggarrange 创建这种不同图的排列:

arrange <- ggarrange(P1,P2,P3,P4,P5, ncol=3,nrow = 2,common.legend = F, align = c("hv"))
arrange

enter image description here

我想添加一个常见的图例,所以我这样做了:
arrange <- ggarrange(P1,P2,P3,P4,P5, ncol=3,nrow = 2,common.legend = T, align = c("hv"),legend="top")
arrange

enter image description here

但是,我希望图例位于图表的右下方,在“空白”空间中。

你知道我怎么能这样做吗?

最佳答案

也许它存在更简单和更容易的解决方案,但一个快速的方法是创建一个空图,只显示图例并用于填充 ggarrange 中的最后一个炮台。 .

这里使用 iris数据集,您可以首先通过指定 legend.position = "none" 生成五个图在 theme删除图例:

library(ggplot2)

p2 <- ggplot(iris, aes(x = Sepal.Length, y = Sepal.Width, color = Species))+
geom_point()+
theme(legend.position = "none")

然后,您绘制一个空图,仅在图区域中间显示图例。您可以增加 ggplot 的所有元素的大小,以使其在最终图形面板上可见:

p3 <- ggplot(iris, aes(x = Sepal.Length, y = Sepal.Width, color = Species))+
geom_point()+
lims(x = c(0,0), y = c(0,0))+
theme_void()+
theme(legend.position = c(0.5,0.5),
legend.key.size = unit(1, "cm"),
legend.text = element_text(size = 12),
legend.title = element_text(size = 15, face = "bold"))+
guides(colour = guide_legend(override.aes = list(size=8)))

现在,您可以使用 ggarrange并指定 p3 作为你的最后一个情节:

library(ggpubr)
ggarrange(p2,p2,p2,p2,p2, p3)

enter image description here

它回答你的问题吗?

关于r - 如何在使用 ggpubr 包中的 `ggarrange` 排列多个图后添加图例?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61534072/

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