简体   繁体   English

如何使用`cowplot` package中的`plot_grid`对齐包含在最后一列图中的图例?

[英]How to align legends included in the plots of the last column of an arrange of plots using `plot_grid` from `cowplot` package?

I have arranged 16 plots (4x4) using plot_grid from cowplot package.我使用来自plot_grid cowplot的 plot_grid 安排了 16 个地块(4x4)。 I would like to align last column of my arrange to get all the legends to be symmetrical vertically.我想对齐排列的最后一列,以使所有图例垂直对称。 I have read this but I don't get what I want.我读过这个,但我没有得到我想要的。 I don't know why it doesn't work.我不知道为什么它不起作用。

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

在此处输入图像描述

I share this fake example in which I followed the same procedure:我分享了这个假的例子,在这个例子中我遵循了相同的程序:

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"
)

的 I get the message "Warning message: Graphs cannot be vertically aligned unless the axis parameter is set. Placing graphs unaligned."我收到消息“警告消息:除非设置了轴参数,否则图形无法垂直对齐。放置未对齐的图形。”

How should I do to align legends?我应该如何对齐图例?

You can use the patchwork package.您可以使用patchwork而成的 package。 I find it a lot faster than cowplot .我发现它比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)

Created on 2020-05-25 by the reprex package (v0.3.0)代表 package (v0.3.0) 于 2020 年 5 月 25 日创建

There are probably multiple ways to handle this, but one option is to place the legends in their own column of the grid:可能有多种方法可以处理此问题,但一种选择是将图例放置在它们自己的网格列中:

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"
)

在此处输入图像描述

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Cowplot程序包:在R中使用plot_grid()将许多图安排成一个图后,如何垂直向下调整图例 - Cowplot Package: How to align legends vertically downwards after arranging many plots into one plot using plot_grid() in R 将 hist() 和 boxplot() object 转换为 ggplot() object 以使用 R 中的 Cowplot Package 中的 plot_grid() 对齐我的绘图 - Converting a hist() and boxplot() object into a ggplot() object to align my plots using plot_grid() in the Cowplot Package in R 如何使用 R 中的“plot_grid”减少一列图的 plot 边距? - How to reduce plot margins of one column of plots of an arrange of plots using `plot_grid` in R? 使用 coord_equal() 时使用 cowplot::plot_grid() 垂直对齐不同高度的图 - Vertically align of plots of different heights using cowplot::plot_grid() when using coord_equal() 如何使用 plot_grid() 将两个图组合在一起在“cowplot”中有一个单一的网格背景? 在 R 语言 - How to have a single grid background in "cowplot" using plot_grid() combining two plots together? in R language cowplot plot_grid自动缩小图的大小 - cowplot plot_grid scales down size of plots automatically 使用 cowplot::plot_grid 组装图时出错 - Error when assembling plots with cowplot::plot_grid 从Cowplot用plot_grid布置地块时,较长的地块标签向右移动 - Longer plot labels move to the right when arranging plots with plot_grid from cowplot 如何在 R 中使用 `ggdraw` 和 `plot_grid()` 在 X 和 Y 轴上创建一个公共标题? - How to create a common title in X and Y axis in an arrange of plots using `ggdraw` and `plot_grid()` in R? 如何与牛图的 plot_grid 一起使用 - How to use with plot_grid from cowplot
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM