# R绘图基础（1）布局

• A+

Ｒ绘图所占的区域，被分成两大部分，一是外围边距，一是绘图区域。

比如mfcol(3,2)就是指将绘图区域分成3行2列，并按列的顺序依次绘图填充；

``` 1234567891011121314151617181920212223242526272829303132333435363738394041 SOUTH<-1; WEST<-2; NORTH<-3; EAST<-4; GenericFigure <- function(ID, size1, size2){  plot(0:10, 0:10, type="n", xlab="X", ylab="Y")  text(5,5, ID, col="red", cex=size1)  box("plot", col="red")  mtext(paste("cex",size2,sep=""), SOUTH, line=3, adj=1.0, cex=size2, col="blue")  title(paste("title",ID,sep=""))} MultipleFigures <- function(){  GenericFigure("1", 3, 0.5)  box("figure", lty="dotted", col="blue")   GenericFigure("2", 3, 1)  box("figure", lty="dotted", col="blue")   GenericFigure("3", 3, 1.5)  box("figure", lty="dotted", col="blue")   GenericFigure("4", 3, 2)  box("figure", lty="dotted", col="blue")} par(mfrow=c(2,2),mar=c(6,4,2,1),oma=c(4,3,2,1)) MultipleFigures() box("inner", lty="dotted", col="green")box("outer", lty="solid", col="green") mtext("Outer Margin Area (oma) of South: 6", SOUTH, line=1, cex=1, outer=TRUE) plotline<-function(n,direc){  for(i in 0:n){    mtext(paste("line",i,sep=""), direc, line=i, cex=1, col="black", adj=1, outer=TRUE)  }}plotline(4,SOUTH) ```

Ｒ绘图布局

layout(mat, widths = rep(1, ncol(mat)), heights = rep(1,nrow(mat)),respect = FALSE)

``` 12345 > attach(mtcars)> layout(matrix(c(1,1,2,3), 2, 2, byrow = TRUE))> hist(wt)> hist(mpg)> hist(disp) ```

layout不规则布局0

``` 12345678910 > attach(mtcars)> layout(matrix(c(1,1,1,1,2,2,2,3,2,2,2,3),3,4,byrow=TRUE))> hist(wt)> hist(mpg)> hist(disp)> layout(matrix(c(1,1,2,3), 2, 2, byrow = TRUE), +   widths=c(3,1), heights=c(1,2))> hist(wt)> hist(mpg)> hist(disp) ```

layout不规则布局1

``` 1234 > attach(mtcars)> layout(matrix(c(1,1,2,1,1,1),nrow=2,ncol=3,byrow=T))> hist(wt)> hist(mpg) ```

layout内嵌图布局

``` 12345678910 > par(fig=c(0,0.85,0,0.85), new=F)> plot(mtcars\$wt, mtcars\$mpg, xlab="Miles Per Gallon",+   ylab="Car Weight")> par(fig=c(0,0.8,0.55,1), new=TRUE)> boxplot(mtcars\$wt, horizontal=TRUE, axes=FALSE)> par(fig=c(0.65,1,0,0.8),new=TRUE)> boxplot(mtcars\$mpg, axes=FALSE)> mtext("Enhanced Scatterplot", side=3, outer=TRUE, line=-3)> par(fig=c(0.4,0.75,0.4,0.7),new=TRUE,mar=c(2,2,0,0),mgp=c(1,.4,0),cex=1,cex.lab=0.7,cex.axis=0.7)> hist(mtcars\$mpg, main="") ```

par实现内嵌图