如何使用`cowplot`包中的`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?

查看:168
本文介绍了如何使用`cowplot`包中的`plot_grid`来对齐地块最后一列中的图例?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经使用 cowplot 包中的 plot_grid 布置了16个地块(4x4).我想对齐我安排的最后一列,以使所有图例在垂直方向上对称.我已阅读

我分享了这个伪造的示例,其中我遵循了相同的步骤:

 库(ggplot2)图书馆(cowplot)图书馆(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 +主题(legend.justification = c(0,1)),p2.a,p2.a,p2.a,p2.b +主题(legend.justification = c(0,1)),p3.a,p3.a,p3.a,p3.b +主题(legend.justification = c(0,1)),p4.a,p4.a,p4.a,p4.b +主题(legend.justification = c(0,1)),ncol = 4,align ="hv") 

我收到消息警告消息:除非设置axis参数,否则图形无法垂直对齐.将图形放置为未对齐."

我应该如何对齐图例?

解决方案

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

  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 +主题(legend.position =无")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 +主题(legend.position =无")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 +主题(legend.position =无")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 +主题(legend.position =无")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") 

I have arranged 16 plots (4x4) using plot_grid from cowplot package. 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?

解决方案

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

这篇关于如何使用`cowplot`包中的`plot_grid`来对齐地块最后一列中的图例?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆