![13e2ef09b4b5177a0ab536c199d5f78f.png](https://i-blog.csdnimg.cn/blog_migrate/a58fc95a7e0b8a0766a7e6f418205814.jpeg)
前言
在同一页面上混合多个图形是一种常见的做法。它可以在同一数字上 总结大量信息,例如,它被广泛用于科学出版物。
par()
一页多图用mfrow参数或mfcol参数规定,这也是我几年前经常用的一种方法。
x <- rnorm(50)
y <- rnorm(50,2,2)
随便模拟产生数据,并对数据绘制一些简单的图,用该函数将一页中对他们进行全部展示。
par(mfrow=c(2,2))
plot(x, y, xlab = "", ylab = "")
hist(x,main='')
qqnorm(x,main = '');qqline(x)
barplot(x, axes = FALSE, space = 0,col='white')
![ad30e843d3c0b9cf10488bee73a42912.png](https://i-blog.csdnimg.cn/blog_migrate/03faba84bc527f137ba38f848ed34545.jpeg)
layout()
layout(mat, widths = rep.int(1, ncol(mat)),
heights = rep.int(1, nrow(mat)), respect = FALSE)
- mat 参数为一个矩阵,提供了作图的顺序以及图形版面的安排。0代表空缺,不绘制图形,大于0 的数代表绘图顺序,相同数字代表占位符。
- widths 和 heights 参数提供了各个矩形作图区域的长和宽的比例。
- respect 参数控制着各图形内的横纵轴刻度长度的比例尺是否一样。
- n 参数为欲显示的区域的序号。
生成2行2列的版面,并设置宽度和高度。par()中oma参数指四个外边空的行数
par(oma = c(2,2,2,2))
nf <- layout(matrix(c(1,2,1,3),2,2),widths = c(1, 3), heights = c(1, 2))
layout.show(nf)
![9cd5655e417f0b5a6b3d34a6a128422f.png](https://i-blog.csdnimg.cn/blog_migrate/f64d33fabbd738489e87c410421afc83.png)
再将各个图进行填充
plot(x, y, xlim = xrange, ylim = yrange, xlab = "", ylab = "")
barplot(xhist$counts, axes = FALSE, ylim = c(0, top), space = 0)
barplot(yhist$counts, axes = FALSE, xlim = c(0, top), space = 0, horiz = TRUE)
![07a7ce706be896c402b949d16a1063f5.png](https://i-blog.csdnimg.cn/blog_migrate/bc6797e2014f07a3df7fdff99f3977f2.png)
前面两种方法,说实话可以实现,但是比较费劲。那么下面看看gridExtra包。
gridExtra包—grid.arrange()
gridExtra包让混合多个图片变得轻而易举。它提供了grid.arrange() 函数来完成 这个任务。它的nrow参数允许指定如何安排布局。
对于更复杂的布局,arrangeGrob() 函数允许做一些嵌套。这里有 4 个 例子来说明 gridExtra 是如何工作的:
library(ggplot2)
library(gridExtra)
这里我们用ggplot绘图,并存在变量名称(g1,g2,g3)中,然后用grid.arrange()将各个变量名称展现出来。
# Make 3 simple graphics:
g1 <- ggplot(mtcars, aes(x=qsec)) + geom_density(fill="slateblue")
g2 <- ggplot(mtcars, aes(x=drat, y=qsec, color=cyl)) + geom_point(size=5) + theme(legend.position="none")
g3 <- ggplot(mtcars, aes(x=factor(cyl), y=qsec, fill=cyl)) + geom_boxplot() + theme(legend.position="none")
g4 <- ggplot(mtcars , aes(x=factor(cyl), fill=factor(cyl))) + geom_bar()
# Plots
grid.arrange(g1, g2, g3, nrow = 3)
![5088c3bf3d89ffb787b0e8ee6ff0aa9e.png](https://i-blog.csdnimg.cn/blog_migrate/4e90df49058f93ba246f453fe43ab0e0.jpeg)
当然可以使用参数arrangeGrob(),下面绘制了两行,第一行是g2,第二行是g3,g4.
grid.arrange(g2, arrangeGrob(g3, g4, ncol=2), nrow = 2)
![2e97df29e268f5556c071a3e2a0a6998.png](https://i-blog.csdnimg.cn/blog_migrate/e2f5922e272797d07cc032c0dadb6a20.jpeg)
下面绘制了两行,第一行是g2,第二行是g3,g4,g1.
grid.arrange(g2, arrangeGrob(g3, g4, g1,ncol=3), nrow = 2)
![8723cb01e108eb4a3ded9535ec7b51e2.png](https://i-blog.csdnimg.cn/blog_migrate/197c2794ff137a10afd947682b1a1698.jpeg)