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:
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:
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:
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 (%)",
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 2×11 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