【发布时间】:2021-09-23 08:20:00
【问题描述】:
当theme(aspect.ratio = 1) 时,我遇到了使用patchwork 组合图的问题。
提供几个例子:
library(tidyverse)
library(patchwork)
# Create the base plots
plotlist = list(
fig1 = iris %>%
filter(Species == "setosa") %>%
ggplot(aes(x = Sepal.Length, y = Sepal.Width)) +
geom_point(),
fig2 = iris %>%
filter(Species == "versicolor") %>%
ggplot(aes(x = Sepal.Length, y = Sepal.Width)) +
geom_point(),
fig3 = iris %>%
filter(Species == "virginica") %>%
ggplot(aes(x = Sepal.Length, y = Sepal.Width)) +
geom_point()
)
# Here patchwork combines the plots nicely
plotlist$fig1 / (plotlist$fig2 + plotlist$fig3)
# However, if we change the aspect.ratio to 1 things don't look so nice anymore
plotlist = lapply(plotlist, function(x) {
x + theme(aspect.ratio = 1)
})
# Notice the large gap between plots. Instead, I would like the plots in the second row to be almost directly under the first row.
plotlist$fig1 / (plotlist$fig2 + plotlist$fig3)
# I tried setting the margins to zero, but that doesn't change anything
plotlist = lapply(plotlist, function(x) {
x + theme(plot.margin = margin(0, 0, 0, 0, unit = "pt"))
})
plotlist$fig1 / (plotlist$fig2 + plotlist$fig3)
如何使用拼凑改进布局?第二行的图的宽度应该是第一行的一半,与第一个图一样,但所有图的纵横比都应为 1。
【问题讨论】:
-
plotlist$fig1 / (plotlist$fig2 + plotlist$fig3) + plot_layout(widths = 1)怎么样 -
这是个好建议!虽然这确实解决了间距问题,但它并没有产生我想要的布局(第二行中的图应限制为第一行的宽度)。使用
widths = c(1,2)也没有解决这个问题。 -
很难正确。这使它更接近。
design <- "#1# #23" plotlist$fig1 / (plotlist$fig2 + plotlist$fig3) + plot_layout(design = design, widths = c(1,2))。顺便说一句,设计应该在 2 条代码线上。从评论中复制粘贴效果不太好。 -
不幸的是,这并没有按预期工作,
design <- c(area(1, 1), area(2, 3))也没有。如果没有纵横比,您的建议有效,但当aspect.ratio = 1无法生成所需的布局时。