
01
—
图形组合布局
par(mfrow=c(2,2))
par(mfrow=c(2,2)),可以理解将绘图区域分割为2x2的矩阵区域,另可参照《R语言实战》3.5图形的组合
##################par(mfrow=c(2,2))####################
attach(iris)
opar <- par(no.readonly = TRUE)
# data(iris)
# head(iris)
# > head(iris)
# Sepal.Length Sepal.Width Petal.Length Petal.Width Species
# 1          5.1         3.5          1.4         0.2  setosa
# 2          4.9         3.0          1.4         0.2  setosa
# 3          4.7         3.2          1.3         0.2  setosa
# 4          4.6         3.1          1.5         0.2  setosa
# 5          5.0         3.6          1.4         0.2  setosa
# 6          5.4         3.9          1.7         0.4  setosa
par(mfrow=c(2,2)) # 设置2x2的布局
plot(Sepal.Length,Sepal.Width, main = "第一张图")
boxplot(Sepal.Length, horizontal = TRUE, main = "第二张图") 
boxplot(Sepal.Width, main = "第三张图") 
hist(Petal.Length, main = "第四张图")
par(opar)
detach(iris)

02
—
图形组合布局
par(fig=c(x1, x2, y1, y2), new = TRUE)
par(fig=c(x1, x2, y1, y2), new = TRUE),取x1,x2,y1,y2四条线圈住的位置绘图图形,另可参照《R语言实战》3.5图形的组合

opar <- par(no.readonly = TRUE)
par(fig=c(0,0.8,0,0.8)) # 设置散点图的布局参数
plot(iris$Sepal.Length,iris$Sepal.Width) #绘制散点图
par(fig=c(0,0.8,0.65,1),new = TRUE) #设置上方箱型图的布局参数
boxplot(iris$Sepal.Length, horizontal = TRUE, axes=FALSE) # 绘制上方箱型图
par(fig=c(0.65,1,0,0.8),new = TRUE) #设置右侧箱型图的布局参数
boxplot(iris$Sepal.Width, axes=FALSE) # 绘制右侧箱型图
mtext("par(fig=c(x1, x2, y1, y2), new = TRUE)",side = 3, outer=TRUE, line=-3)
par(opar)

03
—
图形组合布局
grid.layout & vplayout
library(ggplot2)
#绘制基本ggplot图
base <- ggplot(mpg, aes(displ, hwy)) + geom_point()
p1 <- base + geom_smooth() + labs(title="图1") #如图1
#用%+%调整映射关系中的数据
base <- ggplot(mpg, aes(displ, hwy)) + geom_point()
p2 <- base %+% subset(mpg, fl == "p") + labs(title="图2") #图2#第二种调整数据的方法list
p3 <- base + list(subset(mpg, fl == "p"), geom_smooth(), labs(title="图3")) #图3
###########一页多图########
library(grid)
grid.newpage()  ##新建页面
pushViewport(viewport(layout = grid.layout(2,2))) #将页面分成2*2矩阵
vplayout <- function(x,y){ viewport(layout.pos.row = x, layout.pos.col = y)}
print(p1, vp = vplayout(1,1))   #(1,1)的位置画图1
print(p2, vp = vplayout(1,2))   #(1,2)的位置画图2
print(p3, vp = vplayout(2,1:2))  #(2,:)的位置画图

04
—
图形组合布局
plot_grid {cowplot} 
install.packages("cowplot") #安装cowplot包
library(cowplot) # 加载
?plot_grid  #帮助函数查看具体usage
示例
library(ggplot2)
df <- data.frame(  x = 1:10, y1 = 1:10, y2 = (1:10)^2, y3 = (1:10)^3, y4 = (1:10)^4)
p1 <- ggplot(df, aes(x, y1)) + geom_point()
p2 <- ggplot(df, aes(x, y2)) + geom_point()
p3 <- ggplot(df, aes(x, y3)) + geom_point()
p4 <- ggplot(df, aes(x, y4)) + geom_point()
gridplot_grid(p1, p2, p3, p4)

05
—
图形组合布局
multiplot{Rmisc}
############################# multiplot{Rmisc} ##################################
library(Rmisc)
library(ggplot2)
df <- data.frame(  x = 1:10, y1 = 1:10, y2 = (1:10)^2, y3 = (1:10)^3, y4 = (1:10)^4)
p1 <- ggplot(df, aes(x, y1)) + geom_point()
p2 <- ggplot(df, aes(x, y2)) + geom_point()
p3 <- ggplot(df, aes(x, y3)) + geom_point()
p5 <- ggplot(mpg, aes(as.factor(year), hwy)) +  geom_boxplot() +  facet_wrap(~class, scales = "free_y")
multiplot(p1, p2, p3, p5, cols=2)

“ 获取源代码请至“数据驱动实践”公众号后台回复:一页多图”