A simple patchwork
of ggplot
has left me uncertain how to locate the legend in the overall figure.
Here’s the data and the two plots
library(tidyverse)
library(patchwork)
library(magrittr)
set.seed(123)
t1 <- expand_grid(
rs = 1:2,
fac = 1:5,
grp = 1:2
) %>%
mutate(
y = runif(20),
row = fac %>%
is_in(1:3) %>%
ifelse(
1, 2
)
)
p1 <- t1 %>%
filter(
fac %>%
is_in(1:3)
) %>%
ggplot() +
geom_point(
aes(
rs, y, color = factor(grp)
),
size = 3,
position = position_dodge(width = .4)
) +
facet_wrap(~fac, nrow = 1) +
theme(legend.position = "none")
p2 <- t1 %>%
filter(
fac %>%
is_in(1:3) %>%
not
) %>%
ggplot() +
geom_point(
aes(
rs, y, color = factor(grp)
),
size = 3,
position = position_dodge(width = .4)
) +
facet_wrap(~fac, nrow = 1)
But I want the legend in the blank panel on the bottom row (next to the 5 facet and underneath the 3 facet)
I tried
p1 / p2 +
plot_layout(
design = "
AAA
BB#",
# CC#
guides = "collect"
) &
theme(
legend.position = "inside",
legend.position.inside = c(5/6, 1/4)
)
To no avail.