R Plots Par
In a previous post, I showed how to keep text and symbols at the same size across figures that have different numbers of panels. The figures in that post were ugly because they used the default panel spacing associated with the mfrow
argument of the par
function. Below I will walk through how to adjust the spacing of the panels when using mfrow
.
To plot multiple lines in one chart, we can either use base R or install a fancier package like ggplot2. Here are two examples of how to plot multiple lines in one chart using Base R. Example 1: Using Matplot. If you have a dataset that is in a wide format, one simple way to plot multiple lines in one chart is by using matplot. Graphical Parameters. The value of adj determines the way in which text strings are justified in text, mtext and title.A value of 0 produces left-justified text, 0.5 (the default) centered text and 1 right-justified text. (Any value in (0, 1) is allowed, and on most devices values outside that interval will also work.). Saving Plots in R Since R runs on so many different operating systems, and supports so many different graphics formats, it's not surprising that there are a variety of ways of saving your plots, depending on what operating system you are using, what you plan to do with the graph, and whether you're connecting locally or remotely. As you can see in Figure 1, the previous R code created a multi-plot graphic. The value 2 within the mfrow argument specified to draw a graphic with two rows and the value 3 specified to draw a graphic with three columns. I recommend to set the par options back to their default values after you are finished with all your plots.
For this example, we will use Edgar Anderson’s iris data, which is distributed with R. The data set includes flower measurements for 3 iris species. In this case, the data would be more effectively plotted in a single panel with different colors or symbols for each species, but with larger data sets the different colors/symbols can create a jumbled mess and multi-panel figures illustrate patterns in the data more clearly.
The default values of the three plotting parameters are shown using character strings of their argument names in par (see par (char)), followed by sample code creating four plots, where one additional plotting parameter is modified in each plot.
To plot all the data on the same scale, we need to extract the max and min values of the variables that we are plotting.
Par In R
The next block of code plots 3 panels in a 2x2 arrangement1 with mostly default options. An empty panel is created by calling plot.new
. The empty panel can be placed in any of the 4 positions, but it is redundant to use plot.new
for the bottom right panel because mfrow
fills the graphics device by row, moving left to right along the row.
Yikes! What a mess!
We can use the mai
argument to the par
function to specify the margin (in inches) of each panel in the figure. It takes a little trial-and-error to hit on margins that produce the desired spacing. We can start to reduce the redundancy in the figure by removing the labels for the x- and y-axes.
Not bad, but we still have redundancy by including numbers on the x-axis scale in the top left panel and the y-axis scale in the bottom right panel. When we drop the numbers from those panels, it will create more white space. But we want to use as much white space as possible for plotting. Instead of including only a single call to the par
function to change the mai
argument, we will call par
before plotting each panel to customize the size of the margin on each side of the plot for each panel. The mai
argument specifies the margin by sides, i.e., c(bottom, left, top, right)
.
In the top left panel, we want a smaller bottom margin because we are going to drop the numbers from the x-axis scale. In the bottom left panel, we want larger margins on the bottom and left because we need to include the axis scales on those sides. Again, you simply use trial-and-error to get the margins how you want them, but there is one little catch. If you want all of your panels to have plots that fill the same area, then the bottom+top and left+right margins need to be the same in all panels. In our example, the sum is 0.4 for both, but bottom+top does not need to equal left+right.
The panels are now well placed within the figure. We just need to add a little extra space around the outer margin to place our axis labels. The omi
argument to the par
function specifies the outer margins (in inches) with the sides listed in the same order as for mai
. The mtext
function allows you to place text in the outer margin. I have also labeled each panel by species, which required some small adjustments to the y-axis scale.
We now have a figure that is several steps closer to being publication quality. The main adjustment left to make is customizing the scales of the axes. In another post, I demonstrate how to customize the axes scales, which is particularly useful when presenting transformed data on the original scale.
R Plot Parameters
R Plot Par
Lastly, I want to point out how useful the lattice
package is for quickly generating multi-panel figures with grouped data. As in the R base package, the default is not publication quality, but it requires a lot less code to generate a multi-panel figure that is at least easily readable.
Similarly, ggplot2
allows for quickly creating multi-panel figures.
R Plot Par Margins
The optimal way to plot this data in a multi-panel figure is a 3x1 arrangement, but using the 2x2 arrangement provides a better illustration of how
mfrow
works.↩︎