I’ve recently came across the hafen/geofacet function and was pondering to blog an example. Then, I came across a perfect example, thanks to **kanishkamisra** for working on the dataset & code and making it available via github here!

Skip to content
# Tag: layout

## A nice example of hafen/geofacet from Washington Post to ggplot2

## Layout matrix vs par(mfrow)

Demography, R, and Mac

I’ve recently came across the hafen/geofacet function and was pondering to blog an example. Then, I came across a perfect example, thanks to **kanishkamisra** for working on the dataset & code and making it available via github here!

Advertisements

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)

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)

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

layout.show(ex2)

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)])

This 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)

` `

`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)

%d bloggers like this: