Skip to content

The mgp graphics parameter in base R graphics…very useful

June 20, 2012

This one’s simple but very useful for things like changing the distances between the axis and its titles and labels. From the help file:

The margin line (in mex units) for the axis title, axis labels and axis line. Note that mgp[1] affects title whereas mgp[2:3] affect axis. The default is c(3, 1, 0).

But I find this a little cryptic. Here’s how I look at it. Unless you want to actually move the location of the axes (in which case change the last number in mgp), just leave the last number at 0 (the default). If you do this, then the first two numbers can be thought of as the distance between (1) the axis titles and the axes and (2) the axis labels and the axes. Negative distances are allowed, resulting in titles and / or labels inside the plotting region.

So to make the labels closer to the axis use:

plot(1:10, 1:10, mgp = c(3, 0.1, 0))

To make the labels further from the axis use:

plot(1:10, 1:10, mgp = c(3, 2, 0))

To make the labels strangely inside the plotting region use negative numbers:

plot(1:10, 1:10, mgp = c(3, -3, 0))

To make the axis titles very close to the axis use:

plot(1:10, 1:10, mgp = c(0, 1, 0))

Try out more combinations to better understand how it works.

Advertisements
No comments yet

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: