layout, par(mfrow)

Create a multi-paneled plotting window. The par(mfrow) function is handy for creating a simple multi-paneled plot, while layout should be used for customized panel plots of varying sizes.
par(mfrow)
  • mfrow – A vector of length 2, where the first argument specifies the number of rows and the second the number of columns of plots.
layout(mat, widths=rep(1, ncol(mat)), heights=rep(1, ncol(mat)))
  • mat – A matrix describing the panel layout, where the numbers describe the order in which to add the plots. A zero entry is interpreted as don’t plot anything here.
  • widths – The widths of the panel columns.
  • heights – The heights of the panel rows.

Example.
> set.seed(5)
> x <- rnorm(200)
> y <- 25 - 22*x + 5*x^2 + rnorm(200)
> 
> 
> png("layout1.png")
> par(mfrow=c(2,2))
> par(mar=c(0.5, 4.5, 0.5, 0.5))
> boxplot(x, horizontal=TRUE, axes=FALSE)
> plot(0, type="n", xlab="", ylab="", axes=FALSE)
> par(mar=c(4.5, 4.5, 0.5, 0.5))
> plot(x, y)
> text(0.5, 85, "par(mfrow)", cex=2)
> par(mar=c(4.5, 0.5, 0.5, 0.5))
> boxplot(y, axes=FALSE)
> dev.off()
null device 
          1 
> 
> png("layout2.png")
> mat <- matrix(c(1,2,0,3), 2)
> mat
     [,1] [,2]
[1,]    1    0
[2,]    2    3
> layout(mat, c(3.5,1), c(1,3))
> par(mar=c(0.5, 4.5, 0.5, 0.5))
> boxplot(x, horizontal=TRUE, axes=FALSE)
> par(mar=c(4.5, 4.5, 0.5, 0.5))
> plot(x, y)
> text(0.5, 85, "layout", cex=2)
> par(mar=c(4.5, 0.5, 0.5, 0.5))
> boxplot(y, axes=FALSE)
> dev.off()
null device 
          1 
The set.seed makes the code reproducible, and the png function was used to save each of the plots to a file.
Tip. There are plenty of other plotting parameters that may be of interest. A few others were introduced in the par(mar, mgp, las) post.

Leave a Reply