【发布时间】:2021-11-07 07:17:58
【问题描述】:
我有两个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')
【问题讨论】:
-
如果您使用设计参数来进行绘图布局,您可以通过调整绘图的宽度,让所有内容都对齐。我尝试了` layout plot_layout(design = layout,...
标签: r ggplot2 facet-wrap patchwork