publication-quality figures with R, part 2

By , on

Following up on my recent blog entry about generating publication quality figures with R, here are some more hints, this time about setting the figure margins.

An R figure consists of several regions. From the outside in, these are as follows:

  • The device region. This region contains the figure region, surrounded by the outer margin. The device region may also contain text (for titles etc.). The size of this region is determined by the width and height arguments of the pdf command.
  • The figure region contains the plot region, surrounded by the inner margin. The axis tick marks and labels are part of the figure region. The size of the figure region is determined by the size of the device region and the width of the outer margin.
  • The plot region contains the plot of the data, not including any labels and tick marks. The size of the plot region is normally determined by the size of the figure region and the width of the inner margin.

[figure regions in R]

figure 1. The different regions of a figure generated in R

These different regions are illustrated in figure 1, above. The figure was created using the following R code:

par(oma=c(2, 2, 2, 2), mar=c(4, 4, 2, 2))

set.seed(1)
plot(rnorm(10), xlab="x label", ylab="y label")

box("outer", col="red", lwd=2)
mtext("device region", side=3, line=1, col="red", outer=TRUE)

box("inner", col="green")
mtext("figure region", side=3, line=1, col="green")

box("plot", col="blue")
text(5, 0, "plot region", col="blue")

A plot for use in a publication will normally need to make efficient use of the available space. For this purpose, the default margins used by R are too generous. This is illustrated in figure 2.

[default plot margins for R plots]

figure 2. A simple R plot, using the default margin settings. The red box was added to show the total area covered by the figure. There are unnecessarily large margins on the top and the right of the figure.

To maximise the area available for displaying information, unnecessary white space around the plot should be avoided. To achieve this, the following suggestions may be useful:

  • Outer margins. A figure in a publication will normally have a legend to contain all accompanying information. Thus, no title etc. will be required for the figure. Thus, normally the outer margin size will be 0:
    par(oma=c(0, 0, 0, 0))
    
  • Inner margins. The inner margins need as small as possible, but large enough to contain the axis labels and tick marks. For the plot in figure 2, we can use the following margins:
    par(mai=c(0.85, 0.85, 0.1, 0.1))
    

    Here we use mai (margin in inches) instead of mar (margin in lines), to have better control over size of margins. The four numbers give the widths of the margins at the bottom, left, top and right in this order. Since the plot has no labels on the top and on the right, the last two numbers can be chosen close to 0.

A version of the above plot, with the margins adjusted as shown above, is displayed in figure 3:

[R plot with adjusted margins]

figure 3. The plot from figure 2, but with the margin sizes adjusted to maximise the area available for displaying information. This figure will take up the same amount of space as figure 2 in the enclosing document, but provides much more space for displaying the plot data.

References:

Newer entry: 3D graphics in R (updated)
Older entry: Finite Element Discretisation for SPDEs

Back to blog