赞
踩
可用于 ggplot2 子图排版的 package 有:
分别用他们排版,观察子图的对齐方式
- library(ggplot2)
- library(gridExtra)
- library(patchwork)
- library(cowplot)
生成 4 幅子图,plot.background
显示出每个子图的边框
- p1 <- ggplot(mtcars, aes(x = mpg, y = wt)) +
- geom_point() +
- coord_cartesian(xlim = c(0, 36)) +
- theme_classic() +
- theme(plot.background = element_rect(color = "blue"))
-
- p2 <- ggplot(mtcars, aes(x = 10 * mpg, y = 10 * wt)) +
- geom_point() +
- coord_cartesian(xlim = c(0, 360)) +
- theme_classic() +
- theme(plot.background = element_rect(color = "blue"))
-
- p3 <- ggplot(mtcars, aes(x = 100 * mpg, y = 100 * wt)) +
- geom_point() +
- coord_cartesian(xlim = c(0, 3600)) +
- theme_classic() +
- theme(plot.background = element_rect(color = "blue"))
-
- p4 <- ggplot(mtcars, aes(x = 1000 * mpg, y = 1000 * wt)) +
- geom_point() +
- coord_cartesian(xlim = c(0, 36000)) +
- theme_classic() +
- theme(plot.background = element_rect(color = "blue"))
边框对齐,坐标轴未垂直对齐
grid.arrange(p1, p2, p3, p4, ncol = 2)
边框未对其,坐标轴未垂直对齐
- (p1 | p2) /
- (p3 | p4)
align = "v"
:垂直对齐坐标轴
plot_grid(p1, p2, p3, p4, ncol = 2, align = "v")
align = "h"
:水平对齐坐标轴
plot_grid(p1, p2, p3, p4, ncol = 2, align = "h")
align = "h"
:垂直水平对齐坐标轴
plot_grid(p1, p2, p3, p4, ncol = 2, align = "vh")
cowplot::plot_grid(align = "vh")
排版可以使得各个子图的坐标轴对齐
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。