如何使用`cowplot`软件包中的`plot_grid`对齐地块最后一列中的图例?
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了如何使用`cowplot`软件包中的`plot_grid`对齐地块最后一列中的图例?相关的知识,希望对你有一定的参考价值。
我已经使用plot_grid
包中的cowplot
布置了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"
)
我收到消息“警告消息:除非设置axis参数,否则图形无法垂直对齐。将图形放置为未对齐。”
如何对齐图例?
使用Tung推荐的使用patchwork
软件包后的图解
您可以使用patchwork
包。我发现它比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)
(v0.3.0)在2020-05-25创建
以上是关于如何使用`cowplot`软件包中的`plot_grid`对齐地块最后一列中的图例?的主要内容,如果未能解决你的问题,请参考以下文章
在cowplot中使用axis_canvas的边际图:如何在主面板和边际图之间插入间隙
我在 R 中使用 cowplot 和 ggplot2 的 plot_grids 缺少网格中的四个图之一?