R Graphical Representation – Multiple Plots in One Graph

I find that the R layout() function is a lot easier and more flexible to work with when combing multiple plots into one graph. I have in the past also played around with the par() function, but layout() is my favourite 🙂

The R code below pretty much prints out four barplots onto one graph, as shown in the proceeding graph. Don’t mind the code for the plots –I deliberately left it out since it’s insignificant in this context.

## define layout
layout(matrix(c(1, 2, 3, 4), nrow=2, ncol=2, byrow=TRUE))

## plot 1
Code for plot 1 goes here

## plot 2
Code for plot 2 goes here

## plot 3
Code for plot 3 goes here

## plot 4
Code for plot 4 goes here
R Graphical Representation - 4 Plots on 4 Grids
R Graphical Representation – 4 Plots on 4 Grids

I find myself working with odd number of plots on an even grid and all I do is tell layout which grids my plots should be drawn in. For instance, assuming I wanted to print two plots in grids 1 and 4 using the example code above, all I would have to do is alter the layout function as follows.

layout(matrix(c(1, 0, 0, 2), nrow=2, ncol=2, byrow=TRUE))
R Graphical Representation - 2 Plots on 4 Grids
R Graphical Representation – 2 Plots on 4 Grids

R is awesome!