R作图-多图布局

mfrow 基于par布局

  • 注意设置par时,先保存一下原始图像参数。
attach(mtcars)
opar=par(no.readonly = T)
par(mfrow=c(3,2)) #按行填充,行数为3,列数为2,mfcol按列填充
hist(wt)
hist(mpg,ann=F)
hist(disp,ann=F)
par(opar)
detach(mtcars)

layout 基于矩阵布局

attach(mtcars)
layout(matrix(c(1,1,2,3),2,2,byrow=T),widths=c(3,1),heights=c(1,2))
hist(wt)
hist(mpg)
hist(disp)
detach(mtcars)

fig 自定义布局

  • 主要是按照图层比例,自定义放置子图位置
attach(mtcars)
opar=par(no.readonly = T)
par(fig=c(0,0.8,0,0.8))  #fig=c(x1,x2,y1,y2),4个数值分别为左下角到左,右,下、上边界的距离与对应边的百分比数
plot(wt,mpg,xlab="Miles Per Gallon",ylab="Car Weight")
par(fig=c(0,0.8,0.55,1),new=T) #占据横向范围0~0.8,纵向范围0.55~1,new=T设定添加图到现有图上
boxplot(wt,horizontal=T,axes=F) #horizontal=T旋转90度
par(fig=c(0.65,1,0,0.8),new=T) #占据横向范围0.65~1,纵向范围0~0.8
boxplot(mpg,axes=F)
mtext("Enhanced Scatterplot",side=3,outer=T,line=-3)
par(opar)
detach(mtcars)

posted on 2020-03-20 10:10  萧飞IDO  阅读(197)  评论(0编辑  收藏  举报

导航