在 R 中拼接图片

2020-04-25  本文已影响0人  奔跑的Forrest

这里推荐一个很好用的 R 包 ,基于 ggplot 的拼图包 patchwork 。说它好用不光是说拼图拼的好,而是操作特别方便。

install.packages("patchwork") # 安装包
library(patchwork) # 载入包
library(ggplot2)
p1 <- ggplot(diamonds,mapping = aes(x=price))+
  geom_histogram(binwidth = 1)
p2 <- ggplot(diamonds,mapping = aes(x=price))+
  geom_histogram(binwidth = 10)
p3 <- ggplot(diamonds,mapping = aes(x=price))+
  geom_histogram(binwidth = 100)
p4 <- ggplot(diamonds,mapping = aes(x=price))+
  geom_histogram(binwidth = 1000)  # 画出p1,p2,p3,p4
(p1|p2)/(p3|p4) # 拼图开始
(p1/p2|p3)/p4  # 这里“+”或者“|”是表示在一行,“/”表示换行。
(p1|p2)/(p3|p4)
(p1/p2|p3)/p4

还有一些其他的操作,比如自定义图片的长宽高

p1 + p2+ plot_layout(ncol = 1, heights = c(2, 1))
# 这里后面是定义为图片排成一列,高度是二比一
p1 + p2 + p3 + p4 + plot_layout(ncol = 1, heights = c(1,2,3,4))
p1 + p2+ plot_layout(ncol = 1, heights = c(2, 1))
p1 + p2 + p3 + p4 + plot_layout(ncol = 1, heights = c(1,2,3,4))

还有很多其他的用法就慢慢探索吧,这里贴几个学习参考的网站
https://www.jianshu.com/p/0536cce0acad
https://taoyan.netlify.app/post/2017-12-18

上一篇 下一篇

猜你喜欢

热点阅读