Layout matrix vs par(mfrow)

There are a multitude of ways in which you can divide the plotting area to get multiple plots.

par(mfrow=c()) is one of the most popular: it’s symmetric and very easy to use, you just need to specify the number of rows and columns.

data(iris)
attach(iris)

op <- par(no.readonly = TRUE)
par(mfrow=c(2,3))
plot(Sepal.Length,Sepal.Width)
plot(Sepal.Length,Petal.Length)
plot(Sepal.Length,Petal.Width)
hist(Sepal.Width)
hist(Petal.Length)
hist(Petal.Width)
par(op)ex0

If what you’re looking for is a bit less regular and symmetrical, layout matrix is your friend, and you can control the plotting order of each plotting space through byrow (=T by default)

ex1 <- layout(matrix(c(1:2),1,2),widths=c(0.9,0.1),heights=1)
layout.show(ex1)

layex1

ex2 <- layout(matrix(c(1,1,2,3),2,2,byrow=T),widths=c(0.9,0.1),heights=1)
layout.show(ex2)

layex2
layout(matrix(c(2,0,1,3),2,2,byrow=TRUE), widths=c(1,0.3), heights=c(0.3,1), TRUE)
par(mar=c(0,0,0,0),oma=c(0,0,0,0))
plot(Sepal.Length,Sepal.Width, xlab="Sepal Length", ylab="Sepal Width")
hist(Sepal.Length,axes=FALSE, main="")
plot(NULL, type = "n", axes=F,xlim = c(0, max(x$counts)), ylim = c(range(x$breaks)))
rect(0, x$breaks[1:(length(x$breaks) - 1)], x$counts, x$breaks[2:length(x$breaks)])

ex3 copyThis kind of graph is actually much simpler to do in ggplot2…

 

 

 

 

 

 

Layout matrix also offers much more complicated combinations…

a <- layout(matrix(c(1,1,2,1,1,3,4,5,6),3,3), widths=c(0.5,0.5,0.4), heights=1)
layout.show(a)

ex4TFR 2011 fixed        

ex4 <- layout(matrix(c(1,2,2,3,3,3,4,4,4,4,5,5,6,6,6,7,7,7,7,8),10,2, byrow=F))
layout.show(ex4)

ex5

Advertisements

Author: acarioli

is a PostDoc at the Geography and Environment department of the University of Southampton, WorldPop project team. She is also affiliated researcher at CED, UAB and Dondena Centre. Her interests include spatial econometrics and modeling, bayesian methods, machine learning processes, forecasting, micro-data simulation, and data visualization. Demo-traveler, Mac enthusiast, R zealot and Rladies member.