gpt4 book ai didi

r - 对齐多面图和图例

转载 作者:行者123 更新时间:2023-12-04 23:41:27 26 4
gpt4 key购买 nike

我试图用每个方面都有自己的图例来绘制方面。但是,我在使其全部正确对齐时遇到了一些麻烦。

dat <- structure(list(group1 = structure(c(1L, 1L, 2L, 2L, 2L), .Label = c("A", 
"B"), class = "factor"), group2 = structure(c(1L, 2L, 1L, 3L,
2L), .Label = c("a", "b", "c"), class = "factor"), x = c("1",
"2", "3", "4", "2"), y = c("1", "2", "3", "4", "3")), .Names = c("group1",
"group2", "x", "y"), row.names = c(NA, 5L), class = "data.frame")

dat <- split(dat, f = dat$group1)

library(ggplot2)
p1 <- ggplot(dat$A) +
geom_point(aes(x=x, y=y, colour=group2)) +
facet_wrap(~group1) +
guides(colour=guide_legend(nrow=2)) +
scale_colour_manual(values=c(a = "green", b = "red", c = "blue"),
labels=c(a = "green", b = "red", c = "blue"))

p2 <- p1 %+% dat$B

使用 gridExtra问题是地块不对齐:
library(gridExtra)
grid.arrange(p1, p2, p2, p1, ncol=2)

enter image description here

并使用 cowplot传说有点集中:
library(cowplot)
plot_grid(p1, p2, p2, p1, ncol=2, align="hv")

我尝试添加 legend.justification和/或 legend.position但没有效果。

enter image description here

我怎样才能让两个图/图例对齐?

最佳答案

你可以试试 the experimental egg package .

grid.draw(ggarrange(plots=list(p1,p2,p2,p1)))

请注意,ggplot2 有在每次更新时破坏此类代码的习惯,因此将其视为一种脆弱的解决方法。

enter image description here

关于r - 对齐多面图和图例,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36419094/

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