我遇到了使用patchwork
when组合图的问题theme(aspect.ratio = 1)
。提供了几个例子:
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。