# R-Sessions 14: Multiple Graphs

Sometimes it takes more than just one graph to illustrate your arguments. In those cases, you’ll often want these graphs to be held closely together in the output. R has several ways of doing so. In the results that can be achieved with them, they differ a great deal, but their syntax differs only slightly. These different functions cannot be used together on the same graph-window though.

As is traditional on this website, all the examples used will be fully self-contained. So, first some data is assigned to objects. Then, a hypothetical case will be used of a ‘project’ to compare several methods of graphing the same data. Let’s say that we want to make a plot, containing four types of graphs next to each other: a scatterplot, a barchart, a pie-chart and a boxplot.

x1 <- c(4, 3, 4, 3, 4, 5, 4, 5, 6, 5, 5, 6, 7, 6, 7, 8 )
x2 <- c(8, 7, 8, 7, 8, 7, 6, 5, 5, 6, 5, 4, 4, 5, 4, 3 )
x3 <- c(1, 2, 1, 2, 1, 2, 3, 3, 3, 3, 3, 4, 4, 4, 4, 4 )
x4 <- c(1, 2, 3, 4, 8, 7, 8, 9, 7, 6, 5, 4, 3, 3, 2, 1 )
df <- data.frame(x1, x2, x3, x4)

## Par(mfrow) {graphics}

The par (parameter) command from the graphics package lets you set (or read) graphical parameters. It’s mfrow subcommand sets the number of rows and columns the plotting device will be divided in. In the example below, a vector of c(2,2) is entered to the mfrow subcommand. Subsequently, four plots are made as usual. Finally, the graphical parameter is set to normal again by entering the vector c(1,1) to the mfrow subcommand.

par(mfrow=c(2, 2))
pie(mean(df),main=”Pie-chart”)
barplot(mean(df), main=”Barplot”)
boxplot(df, main=”Boxplots”)
matplot(df,type=”l”, main=”Line diagram”)
par(mfrow=c(1, 1)) ## Layout {Graphics}

The mfrow-parameter that was described above has the drawback that all plots need to have identical dimensions, while we might want, for instance, the line diagram to take more place. In order to be able to do that, we must use the layout() function, that divides a graphical device in separate parts that can be plotted on succeedingly. The layout() function takes a matrix as an argument. This matrix contains number that correspond to the numbered screens the graphics device will be divided in. Parts with the same number will be regarded as belonging together.

In the example below, we will need four screens. We chose to make one screen on the bottom quite large, the other three screens will be smaller and above the large screen. So, we need a matrix of two rows and three columns, containing the number 1 to 4. The second row will have to have the same number, in this case this will be the number four.

matrix(c(1, 2, 3, 4, 4, 4), 2, 3, byrow=TRUE)
layout(matrix(c(1, 2, 3, 4, 4, 4), 2, 3, byrow=TRUE))
layout.show(4)

```> matrix(c(1, 2, 3, 4, 4, 4), 2, 3, byrow=TRUE)
[,1] [,2] [,3]
[1,]    1    2    3
[2,]    4    4    4
``` In the example above, firstly the matrix is shown. As can be seen, we indeed enter a 2 * 3 matrix into the layout() function. Next, we use the layout.show() function to create the visual shown directly below. This can be very useful to check whether the division of the graphics device went well. The parameter (4) entered to the layout.show function reflects the number of screens you want to see and cannot (meaningfully) be higher than the total number of screens on the graphics device. Note the correspondence of the shown layout and the printed matrix that was entered into the layout() functions.

pie(mean(df),main=”Pie-chart”)
barplot(mean(df), main=”Barplot”)
boxplot(df, main=”Boxplots”)
matplot(df,type=”l”, main=”Line diagram”) Next, we simply have to call four plots in the right order to create the output shown below. Note, that the order in which the different plots are called correspond with the layout shown above.

matrix(c(1, 3, 2, 3, 4, 3), 3, 2, byrow=TRUE)
matrix(c(1, 2, 4, 3, 3, 3), 3, 2, byrow=FALSE)
layout(matrix(c(1, 3, 2, 3, 4, 3), 3, 2, byrow=TRUE))
layout.show(4) The syntax shown above and the output shown below are another example of using layout(). Now, we choose to put emphasis on the boxplot, by giving it a full column of its own. Note how the matrix is now different, but the order in which the four plots are created the same. The matrix is created in two ways, but notice that the results are identical. Use the method that suits best to the plot you’re making. In more elaborate examples, creating the matrix can become highly complex.

```> matrix(c(1, 3, 2, 3, 4, 3), 3, 2, byrow=TRUE)
[,1] [,2]
[1,]    1    3
[2,]    2    3
[3,]    4    3
> matrix(c(1, 2, 4, 3, 3, 3), 3, 2, byrow=FALSE)
[,1] [,2]
[1,]    1    3
[2,]    2    3
[3,]    4    3
```

pie(mean(df),main=”Pie-chart”)
barplot(mean(df), main=”Barplot”)
boxplot(df, main=”Boxplots”)
matplot(df,type=”l”, main=”Line diagram”) ## Screen {Graphics}

split.screen(c(2, 1))
split.screen(c(1, 3),screen=2)
screen(5)
pie(mean(df),main=”Pie-chart”)
screen(3)
barplot(mean(df), main=”Barplot”)
screen(4)
boxplot(df, main=”Boxplots”)
screen(1)
matplot(df,type=”l”, main=”Line diagram”)

The last method to split screens, is a combination of the screen() and the split.screen() command. In some cases this method proves useful when complex layouts need to be made. split.screen() basically splits a designated screen into the designated number of rows and columns. By using the command several times, it is possible to create layouts containing screens of different sizes.
The first row of the syntax above splits the screen (there is only one screen right now) into two rows and one column. These screens are numbered 1 and 2 automatically. Next, we want to split the second row onto three columns. We use the split.screen() command again while designating it to screen 2, telling it that it needs to splits the screen into 1 row and 3 columns. Screen 2 does no longer exist after the second time split.screen() is called for, and the new screens are numbered 3, 4, and 5. Another advantage of this method, is that we don’t have to create the plots in a determined order. We use the screen() command to activate a specific screen. If we create plots / graphics now, they are assigned to that specific screen, until a new screen is called for. Notice how the order in which the several graphics are created is the same as in the previous examples, but the resulting plot shown below show a different configuration of these plots. This is because we use the screen() command to alter the order in which the created screens are used.

– – — — —– ——–

– – — — —– ——–
R-Sessions is a collection of manual chapters for R-Project, which are maintained on Curving Normality. All posts are linked to the chapters from the R-Project manual on this site. The manual is free to use, for it is paid by the advertisements, but please refer to it in your work inspired by it. Feedback and topic requests are highly appreciated.
——– —– — — – –