It is possible to create a multi-paneled plotting window with the par function and the layout function. The par function is easy to use and creates a simple panel while layout should be used for customized panels of varying sizes.
par(mfrow)
The par(mfrow) function is handy for creating multiple figures on the same plot. The parameter mfrow is a vector of length 2, where the first argument specifies the number of rows and the second the number of columns of plots. Or if you want to define the columns first, you may use the mfcol parameter.
The following example creates a multi-panel with two rows and three columns and adds five plots. The titles of the plots are set to number 1 to 5 to show the layout of the panel.
x <- c(1,2,3,4,5)
y <- c(4,3,5,1,7)
par(mfrow = c(2,3))
plot(x,y, type = ‚p‘, main = ‚1‘)
plot(x,y, type = ‚l‘, main = ‚2‘)
plot(x,y, type = ‚b‘, main = ‚3‘)
plot(x,y, type = ‚o‘, main = ‚4‘)
plot(x,y, type = ’s‘, main = ‚5‘)
This will create the following graphic.
Layout
The layout function offers some more possibilities to define the panel content and sizes. The function contains the following parameters:
layout(positions, widths, heights)
- positions: A matrix describing the panel layout, where the numbers describe the order in which to add the plots. A zero entry is interpreted as don’t plot anything here.
- widths: A vector with the widths of the panel columns.
- heights: A vector with the heights of the panel rows.
Let’s have a look at an example to explain these parameters. Let’s say we want to change the previous view a little bit. We want to show the plot 4 and 5 in the first row and the plots 1,2 and 3 in the second row. Furthermore we want to increase the size of the plots 4 and 5.
So we have to define the content positions as a matrix.
positions <- matrix(c(4,1,0,2,5,3), nrow = 2)
This will create the following matrix:
[,1] [,2] [,3]
[1,] 4 0 5
[2,] 1 2 3
At next we want to set the width and height of the panel. This can be done with the following vectors:
widths <- c(1,1,1) #same width for all columns
heights <- c(3,2) #first row is higher than second row
The following source code will show the complete example to create the plot panel:
x <- c(1,2,3,4,5)
y <- c(4,3,5,1,7)
positions <- matrix(c(4,1,0,2,5,3), nrow = 2)
widths <- c(1,1,1)
heights <- c(3,2)
layout(positions, widths, heights)
plot(x,y, type = ‚p‘, main = ‚1‘)
plot(x,y, type = ‚l‘, main = ‚2‘)
plot(x,y, type = ‚b‘, main = ‚3‘)
plot(x,y, type = ‚o‘, main = ‚4‘)
plot(x,y, type = ’s‘, main = ‚5‘)
This will create the following graphic.
Summary
You can use the par function to create a standard multi-paneled plotting window. If you want to customize the panel you should use the layout function which offers parameters to change the panel layout.