我正在尝试通过拼凑组合几个 ggplot2 图来制作一个图。
我首先想要图 1 和 3 的共享 y 轴。然后是图 1 和 3,最后是图 2 和 4。这是我在 @Allan Cameron 的帮助下实现的 - 请参阅图。不幸的是,我无法控制情节 1、3、2 和 4 的宽度。我希望情节 1 和 3 比情节 2 和 4 更宽。此外,由于某种原因,图例最终出现在情节的中间。我怎样才能把它一直放在右边?
有任何想法吗?非常感谢所有帮助!
这是代码:
mtcars
library(ggplot2)
library(patchwork)
p1 <- ggplot(mtcars) +
geom_point(aes(disp, wt, colour = mpg)) +
ggtitle('Plot 1')
p2 <- ggplot(mtcars) +
geom_point(aes(carb, wt)) +
ggtitle('Plot 2')
p3 <- ggplot(mtcars) +
geom_point(aes(hp, wt, colour = mpg)) +
ggtitle('Plot 3')
p4 <- ggplot(mtcars) +
geom_area(aes(gear, carb)) +
ggtitle('Plot 4')
# Patchwork graph with shared y-axis
y_axis <- ggplot(data.frame(l = p1$labels$y, x = 1, y = 1)) +
geom_text(aes(x, y, label = l), angle = 90) +
theme_void() +
coord_cartesian(clip = "off")
p1$labels$y <- p2$labels$y <- " "
y_axis + (p1 + p2) / (p3 + p4) + plot_layout(widths = c(1, 15, 5), guides = "collect")