Plotting an Odd number of plots in single image

March 3, 2014
By

(This article was first published on R Video tutorial for Spatial Statistics, and kindly contributed to R-bloggers)

Sometimes I have the need to reduce the number of images for a presentation or an article. A good way of doing it is putting multiple plot on the same tif or jpg file.
R has multiple functions to achieve this objective and a nice tutorial for this topic can be reached at this link: http://www.statmethods.net/advgraphs/layout.html

The most common function is par. This function let the user create a table of plots by defining the number of rows and columns.
An example found in website above, is:

attach(mtcars)par(mfrow=c(3,1)) hist(wt)hist(mpg)hist(disp)

In this case I create a table with 3 rows and 1 column and therefore each of the 3 plot will occupy a single line in the table.

The limitation of this method is that I can only create ordered tables of plots. So for example, if I need to create an image with 3 plots, my options are limited:

A plot per line, created with the code above, or a table of 2 columns and 2 rows:

attach(mtcars)
par(mfrow=c(2,2))
hist(wt)
hist(mpg)
hist(disp)

However, for my taste this is not appealing. I would rather have an image with 2 plots on top and 1 in the line below but centered.
To do this we can use the function layout. Let us see how it can be used:

First of all I created a fake dataset:

 data<-data.frame(D1=rnorm(500,mean=2,sd=0.5),   D2=rnorm(500,mean=2.5,sd=1),   D3=rnorm(500,mean=5,sd=1.3),   D4=rnorm(500,mean=3.5,sd=1),    D5=rnorm(500,mean=4.3,sd=0.8),   D6=rnorm(500,mean=5,sd=0.4),   D7=rnorm(500,mean=3.3,sd=1.3))  
I will use this data frame to create 3 identical boxplots.
The lines of code to create a single boxplot are the following:

 boxplot(data,par(mar = c(10, 5, 1, 2) + 0.1),    ylab="Rate of Change (%)",   cex.lab=1.5, names=c("24/01/2011","26/02/2011",   "20/03/2011","25/04/2011","23/05/2011",   "23/06/2011","24/07/2011"),   col=c("white","grey","red","blue"),   at=c(1,3,5,7,9,11,13),   yaxt="n",   las=2)      axis(side=2,at=seq(0,8,1),las=2)      abline(0,0)      mtext("Time (days)",1,line=8,at=7)      mtext("a)",2,line=2,at=-4,las=2,cex=2)  
This creates the following image:

I used the same options I explored in one of my previous post about box plots: BoxPlots

Notice however how the label of the y axes is bigger than the label on the x axes. This was done by using the option cex.lab = 1.5 in the boxplot function.

Also notice that the label on the x axes ("Time (days)") is two lines below the names. This was done by increasing the line parameter in the mtext call.

These two elements are crucial for producing the final image, because when we will plot the three boxplots together in a jpg file, all these elements will appear natural. Try different option to see the differences.

Now we can put the 3 plots together with the function layout.
This function uses a matrix to identify the position of each plots, in may case I use the function with the following options:

layout(matrix(c(1,1,1,1,1,0,2,2,2,2,2,0,0,0,3,3,3,3,3,0,0,0), 2, 11, byrow = TRUE))

This creates a 2x11 matrix that looks like this:

1  1  1  1  1  0  2  2  2  2  2
0  0  0  3  3  3  3  3  0  0  0
what this tells the function is:
• create a plotting window with 2 rows and 11 columns
• populate the first 5 cells of the first row with plot number 1
• create a space (that's what the 0 means)
• populate the remaining 5 spaces of the first row with plot number 2
• in the second row create 3 spaces
• add plot number 3 and use 5 spaces to do so
• finish with 3 spaces
The results is the image below:

The script is available here: Multiple_Plots_Script.r