By Jochen Voss, 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:
width
and height
arguments of the pdf
command.
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.
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:
par(oma=c(0, 0, 0, 0))
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:
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:
This is an excerpt from Jochen's blog.
Newer entry: 3D graphics in R (updated)
Older entry: Finite Element Discretisation for SPDEs
Copyright © 2012 Jochen Voss. All content on this website (including text, pictures, and any other original works), unless otherwise noted, is licensed under the CC BY-SA 4.0 license.