Как выровнять легенды, включенные в графики последнего столбца аранжировки графиков, используя `plot_grid` из пакета` cowplot`? - PullRequest
0 голосов
/ 25 мая 2020

Я оформил 16 участков (4х4), используя plot_grid из cowplot пакета. Я хочу выровнять последний столбец моей аранжировки, чтобы все легенды были симметричными по вертикали. Я прочитал это , но не получаю того, что хочу. Я не знаю, почему это не работает.

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

enter image description here

Поделюсь этим фальшивым примером, в котором я выполнил ту же процедуру:

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

of Я получаю сообщение «Предупреждение: графики не могут быть выровнены по вертикали, если не задан параметр оси. Размещение графиков без выравнивания».

Как следует Что делать для сопоставления легенд?

Ответы [ 2 ]

1 голос
/ 26 мая 2020

Вероятно, есть несколько способов справиться с этим, но один из вариантов - разместить легенды в их собственном столбце сетки:

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

enter image description here

1 голос
/ 25 мая 2020

Вы можете использовать пакет 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)

Создано 25.05.2020 пакетом REPEX ( v0.3.0)

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...