This post includes 1 update. In this post, I answer a question about how to adjust the offset for x and y axes in a simple R chart. The R script also shows how to make side by side comparison plots.
Andreas asked the question:
“… right now I wonder about this: When doing a normal (not lattice) plot, xlim doesn’t really force the axis to start at 0
> x y plot(y~x, xlim=c(0,10),ylim=c(0,10))
there is still some kind of inner margin. Any idea how to remove it?”
I had noticed that the plot() function offsets the x and y axes so that values can not overplot the axis lines. I just assumed that was the way R handled charts. Based on Andreas’s question, I decided to do a little search through R documentation to see if I could find a way to adjust the axes offset. This short post shows how I found the necessary parameter and shows a comparison of the same plot with and without the axes offsets.
When I first started using R documentation, I found it intimidating, often giving me more options than I could possibly handle. In this case, however, it gave me exactly what I needed.
I typed ?par in my R console and got a multipage listing of everything I could want to know about graphical parameters. Since I had a specific question, I scanned through the pars until I found ‘xaxs’, just what Andreas and I were looking for. Here’s part of the description’:
“‘xaxs’ The style of axis interval calculation to be used for the
x-axis. Possible values are ‘”r”‘, ‘”i”‘, ‘”e”‘, ‘”s”‘,
‘”d”‘. The styles are generally controlled by the range of
data or ‘xlim’, if given. Style ‘”r”‘ (regular) first extends
the data range by 4 percent and then finds an axis with
pretty labels that fits within the range. Style ‘”i”‘
(internal) just finds an axis with pretty labels that fits
within the original data range.”
That’s it! We need to set ’xaxs = “i” to get the axes to meet at 0. Let’s try it and see if it work.
Plots With ‘xaxs=”r” and ‘xaxs = “i”
Here’s a simple comparison chart that shows xaxs = “r” on left and xaxs = “i” on right.
It works just as advertised. Here’s the script.
# Here are two simple x and y vectors to be plotted
x <- c(0,1,2,3,4,5,6,7,8)
y <- c(0,1,2,3,4,5,6,7,8)
# Set layout() for side by side plots
layout(matrix(c(1,2), byrow=TRUE, ncol=2))
# xaxs = “r” adds 4% over specified limits
plot (y~x, xlim=c(0,10), ylim=c(0,10),
xaxs=”r”, yaxs = “r”, las = 1,
main = “Plot with ‘r’ axes placement”)
# The 4% expansion can be eliminated by specifying xaxs = “i” and yaxs = “i”
plot (y~x, xlim=c(0,10), ylim = c(0,10),
xaxs = “i”, yaxs = “i” , las = 1,
main = “Plot with ‘i’ axes placement”)
This small example demonstrates both the power of R and the quality of the documentation. While it can be overwhelming for beginners, it’s nice to know that R has both the capabilities and documentation to let me customize my charts as I like.
Hadley asks in his comment: “But why would you want to do that? It’s such a bad idea because your data points get squashed by the axes!”
Good question. For me, I am used to looking at Excel charts that set the axes at the specified value (often 0), with no wiggle room. Let’s see how points along the x and y axis plot when we have them right up against the axis. Here’s our earlier chart with some 0 points added:
Hadley is right. The points that are right on the x and y axis limits are visible in the left chart (xaxs = “r”), however, they are almost completely hidden in the right chart (xaxs = “i”).
So what have we learned?
- R documentation is extensive, you can find what you need if you keep looking
- R default chart settings are well thought out
- Excel users may need to adjust our thinking about what an effective chart looks like