【问题标题】:How to align legends included in the plots of the last column of an arrange of plots using `plot_grid` from `cowplot` package?如何使用 `cowplot` 包中的 `plot_grid` 对齐包含在最后一列图中的图例?
【发布时间】:2020-05-25 14:28:37
【问题描述】:

我使用来自cowplot 包的plot_grid 安排了16 个地块(4x4)。我想对齐排列的最后一列,以使所有图例垂直对称。我已经阅读了this,但我没有得到我想要的东西。我不知道为什么它不起作用。

arrange <- plot_grid(P.ADelay.5m,P.ADelay.15m,P.ADelay.25m,P.ADelay.35m + theme(legend.justification = c(0,1)),
                     P.SW.5m,P.SW.15m,P.SW.25m,P.SW.35m + theme(legend.justification = c(0,1)),
                     P.Method.5m,P.Method.15m,P.Method.25m,P.Method.35m + theme(legend.justification = c(0,1)),
                     P.Period.5m,P.Period.15m,P.Period.25m,P.Period.35m + theme(legend.justification = c(0,1)),
                     P.Statistic.5m,P.Statistic.15m,P.Statistic.25m,P.Statistic.35m + theme(legend.justification = c(0,1)),
                     ncol=4, nrow = 5, align = "v" )

arrange

我分享了这个假的例子,我在其中遵循了相同的程序:

library(ggplot2)
library(cowplot)
library(ggpubr)
theme_set(theme_cowplot())

df1 <- data.frame(x = 1:12, y = (1:12)^2)
df1$grp = c('A', 'B', 'C')

df2 <- data.frame(x = 1:12, y = (1:12)^2)
df2$Statistic = c('none', 'Mean')

df3 <- data.frame(x = 1:12, y = (1:12)^2)
df3$Methodology = c('FFFFFF', 'GGGGGGGGG', 'HH','IIIII')

df4 <- data.frame(x = 1:12, y = (1:12)^2)
df4$Depth = c('15m', '1000m')

p1.a <- ggplot(df1, aes(x, y, color=grp)) + geom_point()  + theme(legend.position = "none")
p1.b  <- ggplot(df1, aes(x, y, color=grp)) + geom_point()

p2.a <- ggplot(df2, aes(x, y, color=Statistic)) + geom_point()  + theme(legend.position = "none")
p2.b  <- ggplot(df2, aes(x, y, color=Statistic)) + geom_point()

p3.a <- ggplot(df3, aes(x, y, color=Methodology)) + geom_point()  + theme(legend.position = "none")
p3.b  <- ggplot(df3, aes(x, y, color=Methodology)) + geom_point()

p4.a <- ggplot(df4, aes(x, y, color=Depth)) + geom_point()  + theme(legend.position = "none")
p4.b  <- ggplot(df4, aes(x, y, color=Depth)) + geom_point()

plot_grid(
  p1.a, p1.a, p1.a, p1.b + theme(legend.justification = c(0,1)),
  p2.a, p2.a, p2.a, p2.b + theme(legend.justification = c(0,1)),
  p3.a, p3.a, p3.a, p3.b + theme(legend.justification = c(0,1)),
  p4.a, p4.a, p4.a, p4.b + theme(legend.justification = c(0,1)),
  ncol = 4,align = "hv"
)

我收到消息“警告消息:除非设置了轴参数,否则图形无法垂直对齐。放置未对齐的图形。”

如何对齐图例?

【问题讨论】:

  • 您的plot_grid() 电话中可能需要axis = 'lr'axis = 'tblr'
  • 你是对的,使用axis = 'lr'axis = 'tblr' 我得到了要对齐的图例。但是,列之间的空间急剧增加!如果不是一回事,那就是另一回事了!!如果我删除了align="hv" 中的v,那么图例就会失去对齐。如果我删除axis = 'lr'axis = 'tblr',我也会失去对齐。然而,正如我已经告诉过你的,如果我保持axis = 'lr'/axis = 'tblr'align="hv",我得到的对齐是很少见的。你知道为什么吗?这只是为了知道它。我现在要试试你的其他建议。

标签: r ggplot2 cowplot


【解决方案1】:

可能有多种方法可以处理此问题,但一种选择是将图例放置在自己的网格列中:

p1.a <- ggplot(df1, aes(x, y, color=grp)) + geom_point() + theme(legend.justification = c(0,0.8))
p1.leg  <- as_ggplot(get_legend(p1.a))
p1.a <- p1.a + theme(legend.position = "none")

p2.a <- ggplot(df2, aes(x, y, color=Statistic)) + geom_point() + theme(legend.justification = c(0,0.8))
p2.leg  <- as_ggplot(get_legend(p2.a))
p2.a <- p2.a + theme(legend.position = "none")

p3.a <- ggplot(df3, aes(x, y, color=Methodology)) + geom_point() + theme(legend.justification = c(0,0.8))
p3.leg  <- as_ggplot(get_legend(p3.a))
p3.a <- p3.a + theme(legend.position = "none")

p4.a <- ggplot(df4, aes(x, y, color=Depth)) + geom_point() + theme(legend.justification = c(0,0.8))
p4.leg  <- as_ggplot(get_legend(p4.a))
p4.a <- p4.a + theme(legend.position = "none")

plot_grid(
  p1.a, p1.a, p1.a, p1.a, p1.leg,
  p2.a, p2.a, p2.a, p2.a, p2.leg,
  p3.a, p3.a, p3.a, p3.a, p3.leg,
  p4.a, p4.a, p4.a, p4.a, p4.leg,
  ncol = 5, align = "hv"
)

【讨论】:

  • 感谢@dww 看到这个。对此,我真的非常感激。您知道是否有任何方法可以在 X 轴上只留下一个 X 标签,在 Y 轴上只留下一个 Y 标签并且都居中,并且还可以为每一列添加一个名称?在这个例子中,就像在我的真实数据中一样,所有图的 x 轴和 y 轴都使用相同的单位。不同的是每行使用不同的图例,每列用于不同的变量。
  • 在这篇文章 (stackoverflow.com/questions/61997737/…) 中,我询问了如何获取它。最后我自己找到了解决方案,但似乎很棘手。我问你,以防万一有更简单的方法。
【解决方案2】:

您可以使用patchwork 包。我发现它比cowplot 快很多。

library(ggplot2)
library(cowplot)
#> 
#> ********************************************************
#> Note: As of version 1.0.0, cowplot does not change the
#>   default ggplot2 theme anymore. To recover the previous
#>   behavior, execute:
#>   theme_set(theme_cowplot())
#> ********************************************************
theme_set(theme_cowplot())

df1 <- data.frame(x = 1:12, y = (1:12)^2)
df1$grp = c('A', 'B', 'C')

df2 <- data.frame(x = 1:12, y = (1:12)^2)
df2$Statistic = c('none', 'Mean')

df3 <- data.frame(x = 1:12, y = (1:12)^2)
df3$Methodology = c('FFFFFF', 'GGGGGGGGG', 'HH','IIIII')

df4 <- data.frame(x = 1:12, y = (1:12)^2)
df4$Depth = c('15m', '1000m')

p1.a <- ggplot(df1, aes(x, y, color=grp)) + geom_point()  + theme(legend.position = "none")
p1.b  <- ggplot(df1, aes(x, y, color=grp)) + geom_point() + theme(legend.justification = c(0,1)) 

p2.a <- ggplot(df2, aes(x, y, color=Statistic)) + geom_point()  + theme(legend.position = "none")
p2.b  <- ggplot(df2, aes(x, y, color=Statistic)) + geom_point() + theme(legend.justification = c(0,1))

p3.a <- ggplot(df3, aes(x, y, color=Methodology)) + geom_point()  + theme(legend.position = "none")
p3.b  <- ggplot(df3, aes(x, y, color=Methodology)) + geom_point() + theme(legend.justification = c(0,1))

p4.a <- ggplot(df4, aes(x, y, color=Depth)) + geom_point()  + theme(legend.position = "none")
p4.b  <- ggplot(df4, aes(x, y, color=Depth)) + geom_point() + theme(legend.justification = c(0,1))


library(patchwork)
#> 
#> Attaching package: 'patchwork'
#> The following object is masked from 'package:cowplot':
#> 
#>     align_plots

# define plot layout 4 x 4
layout <- "
ABCD
EFGH
IJKL
MNOP
"

p1.a + p1.a + p1.a + p1.b +
p2.a + p2.a + p2.a + p2.b +
p3.a + p3.a + p3.a + p3.b +
p4.a + p4.a + p4.a + p4.b +
  plot_layout(design = layout)

reprex package (v0.3.0) 于 2020 年 5 月 25 日创建

【讨论】:

  • 谢谢@Tung,你能告诉我patchwork 是如何工作的吗?我的意思是,我看到你写了 `layout
  • 是的,它告诉将每个单独的图放在该矩阵中的哪个位置。您可以将ABCD 替换为AABBAAAA 以查看情节将如何变化
  • 这看起来比我用过的其他替代品更好。然而,对齐并不完美。我已经更新了帖子,并使用您的建议 (patchwork package) 添加了一张我实际安排的情节的图片。你知道为什么对齐不完美吗?
  • 您必须尝试设置情节图例的方式。我不认为patchwork 可以做得更好
猜你喜欢
  • 2019-07-31
  • 2020-11-14
  • 2018-08-02
  • 1970-01-01
  • 2021-09-06
  • 2018-03-20
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多