我有两个facet_wrap
要垂直组合的图,并与拼凑而成左对齐。我的问题是两个图之间的列数不相等(如下面的简单示例所示)。
有没有办法让这些图左对齐拼凑?
library(ggplot2)
library(dplyr)
library(purrr)
library(patchwork)
plot_ls <- data.frame(var_a = c(rep(1, 5), rep(2, 4)),
var_b = c(letters[1:5], letters[c(1:2, 4:5)]),
var_x = "a",
var_y = rnorm(9),
var_color = "b") %>%
split(.$var_a) %>%
imap(function(df.x, var_a.x) {
ggplot(data = df.x) +
geom_point(aes(x = var_x, y = var_y, color = var_color)) +
facet_wrap(var_b ~ ., scales = "free", nrow = 2) +
ylab(var_a.x) +
theme(aspect.ratio = 1,
axis.title.x = element_blank(),
plot.margin = margin(1,1,1,1))
})
# ...centered, not aligned to the left...
plot_ls[[1]] + plot_ls[[2]] +
plot_layout(ncol = 1,
guides = 'collect') &
theme(legend.position = 'bottom')