How to get axis limit in R
The manual of
?plot command in R does not have limit options, but you can call, for instances (see the figure on the left):
It’s because the
ylim are optional arguments in the
plot.default() command. The
plot() is a generic name, which will call other specialized plot commands depending the given data/object.
Here’s the annoying part. You cannot find
ylim options in the list of graphic parameters by the
par() command. Also you cannot adjust these limit yourself in some specialized plot commands. Sometimes you need to adjust these parameters yourself. So how can you do that?
Well, these axis limit options are actually listed in the
par() parameters, but under strangely different name. It took me quite some time to find it. You can ask the boundary limit of an axis with
usr command. So for instances (I took this example from the
clip help), if you have this histogram plot:
> x = rnorm(1000) > hist(x, xlim=c(-4,4))
You can ask and use its axis limit, for instances for applying the clipping region:
> usr = par("usr") > clip(usr, -1, usr, usr) > hist(x, col = 'red', add = TRUE) > clip(1, usr, usr, usr) > hist(x, col = 'blue', add = TRUE) > do.call("clip", as.list(usr)) # reset to plot region
That’s annoying, because the name of
usr has nothing to do with axis limit. Again to note that this parameter is not the same as the tick mark intervals that you see in the axis. You must use another strange
yaxp, i.e., that give the tick mark parameters. Why can’t R use XTickMarks or YTickMarks like in Matlab that are more easy to remember?
So here are the three parameter returns for the above histogram plot:
> par('usr')  -4.32 4.32 -8.12 211.12 > par('xaxp')  -4 4 4 > par('yaxp')  0 200 4