R 对齐刻面的情节和图例

R 对齐刻面的情节和图例,r,ggplot2,gridextra,cowplot,R,Ggplot2,Gridextra,Cowplot,我试着用每个方面都有自己的传说来描绘各个方面。然而,我有一些麻烦,让它所有正确对齐 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",

我试着用每个方面都有自己的传说来描绘各个方面。然而,我有一些麻烦,让它所有正确对齐

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

使用
cowplot
图例稍微居中:

library(cowplot)
plot_grid(p1, p2, p2, p1, ncol=2, align="hv")
我尝试添加
legend.justification
和/或
legend.position
,但没有效果

如何使两个绘图/图例对齐?

您可以尝试

注意GGOPTT2习惯于在每次更新时破坏这类代码,所以认为它是一个脆弱的解决方案。


我们可以添加
辅助线(col=guide\u legend(nrow=3))
以使图例占据相同的宽度大小。@zx8754我的真实数据有更多的因子级别,根据面从2到10不等。没有办法拥有多个列,但是列的数量不同。谢谢,我必须做什么才能访问函数
ggarrange
?@beetroot:
devtools::install_github('baptiste/egg')
要安装iThanks感谢您的帮助,我现在也可以使用我的示例。但是根据我的实际数据,我得到了一个错误:
error:ncol(x)==ncol(y)不是真的。非常感谢您的帮助,也感谢您在gridExtra等方面所做的出色工作:)
library(cowplot)
plot_grid(p1, p2, p2, p1, ncol=2, align="hv")
grid.draw(ggarrange(plots=list(p1,p2,p2,p1)))