Axis Labels in R

I use R for generating most of my scientific graphics, and it generally does a pretty good job with the axes by default. However, you are given a lot of flexibility if you want to tweak things and get them “just right.”

Recently, I was plotting a distribution with a very large mean (in the hundreds of thousands range). Printing out something like 350000 every X-axis tick mark would have cluttered the graphic, so R by default printed the tick mark labels in scientific notation. It was definitely less cluttered, but also not very readable. I decided I would prefer labels like 350k rather than 3.5e5.

The axis command is what I needed. First, I set the xaxt parameter to n in the initial plot command using the to leave the X axis blank. I then used the axis command to specify where I wanted the axis printed (bottom), the size of intervals between tick marks, and how I wanted to print the labels. This was my final code.

plot(x, y, type='l', xaxt='n')
axis(1, at = x <- seq(3.5e5, 8e5, by = 5e4),  labels = paste(x/1000, "k", sep = "" ))

Leave a Reply

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

You are commenting using your 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