当前位置:   article > 正文

ggplot2中显示坐标轴_R:ggplot2多子图对齐坐标轴

ggarrange 对其坐标轴

可用于 ggplot2 子图排版的 package 有:

  • gridExtra
  • patchwork
  • cowplot

分别用他们排版,观察子图的对齐方式

  1. library(ggplot2)
  2. library(gridExtra)
  3. library(patchwork)
  4. library(cowplot)

生成 4 幅子图,plot.background 显示出每个子图的边框

  1. p1 <- ggplot(mtcars, aes(x = mpg, y = wt)) +
  2. geom_point() +
  3. coord_cartesian(xlim = c(0, 36)) +
  4. theme_classic() +
  5. theme(plot.background = element_rect(color = "blue"))
  6. p2 <- ggplot(mtcars, aes(x = 10 * mpg, y = 10 * wt)) +
  7. geom_point() +
  8. coord_cartesian(xlim = c(0, 360)) +
  9. theme_classic() +
  10. theme(plot.background = element_rect(color = "blue"))
  11. p3 <- ggplot(mtcars, aes(x = 100 * mpg, y = 100 * wt)) +
  12. geom_point() +
  13. coord_cartesian(xlim = c(0, 3600)) +
  14. theme_classic() +
  15. theme(plot.background = element_rect(color = "blue"))
  16. p4 <- ggplot(mtcars, aes(x = 1000 * mpg, y = 1000 * wt)) +
  17. geom_point() +
  18. coord_cartesian(xlim = c(0, 36000)) +
  19. theme_classic() +
  20. theme(plot.background = element_rect(color = "blue"))

gridExtra::grid.arrange () 排版

边框对齐,坐标轴未垂直对齐

grid.arrange(p1, p2, p3, p4, ncol = 2)

646e583a6fd0acbb6549e8b23cd6f758.png

patchwork () 排版

边框未对其,坐标轴未垂直对齐

  1. (p1 | p2) /
  2. (p3 | p4)

c81dbef8ac0ff754e002c4dee2e27c7c.png

cowplot::plot_grid () 排版

align = "v":垂直对齐坐标轴

plot_grid(p1, p2, p3, p4, ncol = 2, align = "v")

98946b0241c74efb40a84e3df5c3d07b.png

align = "h":水平对齐坐标轴

plot_grid(p1, p2, p3, p4, ncol = 2, align = "h")

832d56fa14598e83649d5efb94e6da7e.png

align = "h":垂直水平对齐坐标轴

plot_grid(p1, p2, p3, p4, ncol = 2, align = "vh")

98946b0241c74efb40a84e3df5c3d07b.png

小结

cowplot::plot_grid(align = "vh") 排版可以使得各个子图的坐标轴对齐

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/花生_TL007/article/detail/65995
推荐阅读
相关标签
  

闽ICP备14008679号