Site icon R-bloggers

Controlling Axes of R Plots

[This article was first published on Carlisle Rainey » R, and kindly contributed to R-bloggers]. (You can report issue about the content on this page here)
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.

(This post is part of the #cumpa series of blog posts and tweets I am writing leading up to SPSA. For more information, see this blog post. To follow along, subscribe to my blog here or follow me on Twitter here. To engage in the conversation, reply to this tweet.)

R has powerful graphical capabilities and I use it in all my papers to plot data and illustrate theoretical ideas. The default plot function, however, doesn’t give the reader needed control over the axis labels. Below I’ve plotted the some data using the R defaults and then made several changes for comparison. I think the plot on the right looks much better. In this post, I’ll show you how to make the changes.

The default plot function in R works something like this.

n = 100 x = rnorm(n) y = rnorm(n, x)
png("fig1.png", width = 400, height = 300) plot(x, y, xlab = "Explanatory Variable", ylab = "Outcome Variable") abline(lm(y~x), col = "red", lwd = 2) dev.off()
This code produces the following figure.

Notice three things about the figure above.

  1. The tick marks along the axes are too long.
  2. The tick mark labels along the vertical axis are written vertically.
  3. The tick mark labels are too far from the tick marks.
  4. The axis labels are too far from the axes.

Below I discuss in detail how to fix each of these issues.

Orienting the Tick Mark Labels

To make the desired changes, we need to not rely so heavily on the axis annotation of the plot function. Instead, I like to initiate the plot using the plot function and add in the notations later.

png("fig2.png", width = 400, height = 300) plot(x, y, axes = F, xlab = NA, ylab = NA) abline(lm(y~x), col = "red", lwd = 2) dev.off()

This code produces the following figure without any axes or axis labels.

Now, all we need to do is add the axes into the figure and add the axis labels. To do this, I simply call the axis() command twice, once for each side I want to add an axis to. A simply call to the box() function adds a box around the plot. The code below adds a box around the plot and the tick marks and tick mark labels.

png("fig3.png", width = 400, height = 300) plot(x, y, axes = F, xlab = NA, ylab = NA) abline(lm(y~x), col = "red", lwd = 2) box() axis(side = 1) axis(side = 2) dev.off()

This code creates the following figure.

This plot matches the first plot produced using the notations in the plot function, but gives more control. We can make a simple change to the command that adds the axis to the vertical axis by setting the las option. This orients the tick mark labels horizontally.

png("fig4.png", width = 400, height = 300) plot(x, y, axes = F, xlab = NA, ylab = NA) abline(lm(y~x), col = "red", lwd = 2) box() axis(side = 1) axis(side = 2, las = 1) dev.off()

Now our tick mark labels along the vertical axis are labeled horizontally–the only way that really makes sense.

Length of Tick Marks

Let’s now turn to the length of the tick marks. Use the tck option in the axis command to control the length of the tick marks.

png("fig5.png", width = 400, height = 300) plot(x, y, axes = F, xlab = NA, ylab = NA) abline(lm(y~x), col = "red", lwd = 2) box() axis(side = 1, tck = -.01) axis(side = 2, las = 1, tck = -.01) dev.off()

Now that’s more like it. Unfortunately, making the tick marks shorter has only made their labels appear even further from the plot.

Tick Mark Labels

Fixing the tick mark labels requires a little bit of trickery. I fix this by calling the axis() command twice for each axis to be created. The first call plots the tick marks, but no labels. The second call plots the labels, but no tick marks. But adjust the line option in the second call, the labels can be repositioned.

png("fig6.png", width = 400, height = 300) plot(x, y, axes = F, xlab = NA, ylab = NA) abline(lm(y~x), col = "red", lwd = 2) box() axis(side = 1, tck = -.015, labels = NA) axis(side = 2, tck = -.015, labels = NA) axis(side = 1, lwd = 0, line = -.4) axis(side = 2, lwd = 0, line = -.4, las = 1) dev.off()

 

Axis Labels

All that is left is to add in the axis labels. This is done with a quick call to the mtext() command. Control the distance from the axis with the line command.

png("fig7.png", width = 400, height = 300) plot(x, y, axes = F, xlab = NA, ylab = NA) abline(lm(y~x), col = "red", lwd = 2) box() axis(side = 1, tck = -.015, labels = NA) axis(side = 2, tck = -.015, labels = NA) axis(side = 1, lwd = 0, line = -.4) axis(side = 2, lwd = 0, line = -.4, las = 1) mtext(side = 1, "Explanatory Variable", line = 2) mtext(side = 2, "Outcome Variable", line = 2) dev.off()

Done.

To leave a comment for the author, please follow the link and comment on their blog: Carlisle Rainey » R.

R-bloggers.com offers daily e-mail updates about R news and tutorials about learning R and many other topics. Click here if you're looking to post or find an R/data-science job.
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.