控制许多拼凑的 ggplots 的宽度
Controlling widths of many patchworked ggplots
我正在尝试通过拼凑组合多个 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")
关于宽度问题,您所做的嵌套 - 例如 (p1 + p1)
- 导致嵌套对象的响应不同。相反,您可以使用 plot_layout()
中的 design
参数来实现相同的效果,但会响应宽度。
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",
design = "
123
145
")
由 reprex package (v0.3.0)
于 2020-12-16 创建
请注意,您正在删除 p2
的 y 轴标签,而我认为您打算从 p3
.
中删除它
我正在尝试通过拼凑组合多个 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")
关于宽度问题,您所做的嵌套 - 例如 (p1 + p1)
- 导致嵌套对象的响应不同。相反,您可以使用 plot_layout()
中的 design
参数来实现相同的效果,但会响应宽度。
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",
design = "
123
145
")
由 reprex package (v0.3.0)
于 2020-12-16 创建请注意,您正在删除 p2
的 y 轴标签,而我认为您打算从 p3
.