Often, it is useful to have multiple plots in the same frame as it allows us to get a comprehensive view of a particular variable or compare among different variables. The Graphics package offers two methods to combine multiple plots.
par() can be used to set graphical parameters regarding plot layout using the mfcol and mfrow arguments.
layout() serves the same purpose but offers more flexibility by allowing us to modify the height and width of rows and columns.
par() allows us to customize the graphical parameters(title, axis, font, color, size) for a particular session. For combining multiple plots, we can use the graphical parameters mfrow and mfcol. These two parameters create a matrix of plots filled by rows and columns respectively. Let us combine plots using both the above parameters.
|mfrow||Fill by rows||Number of rows and columns|
|mfcol||Fill by columns||Number of rows and columns|
mfrow combines plots filled by rows i.e it takes two arguments, the number of rows and number of columns and then starts filling the plots by row. Below is the syntax for mfrow.
# mfrow syntax mfrow(number of rows, number of columns)
Let us begin by combining 4 plots in 2 rows and 2 columns:
Let us begin by arranging 4 plots in 2 rows and 2 columns. The plots will be filled by rows as we are using the mfrow function:
# store the current parameter settings in init init <- par(no.readonly=TRUE) # specify that 4 graphs to be combined and filled by rows par(mfrow = c(2, 2)) # specify the graphs to be combined plot(mtcars$mpg) plot(mtcars$disp, mtcars$mpg) hist(mtcars$mpg) boxplot(mtcars$mpg) # restore the setting stored in init par(init)