Reputation: 1768
In the following example, the last x-axis label ("4.0") is omitted.
df <- data.frame(x = c(1, 2, 3.8), y = c(1, 2, 3))
#png(filename = "cutoff.png")
plot(df$x, df$y, xaxt = "n")
axis(side = 1, at = seq(0, 4, 0.5), labels = seq(0, 4, 0.5))
#dev.off()
How to prevent this behaviour?
Upvotes: 1
Views: 54
Reputation: 37661
As @griffinevo answered (+1), If you want the axis limits to go to 4, you must specify that using xlim
. However, it is probably worth explaining how the default limits are computed. This is explained in the documentation, but in a slightly obscure place. On the help page ?par
search for xaxs
. There you will see
Style "r" (regular) first extends the data range by 4 percent at each end and then finds an axis with pretty labels that fits within the extended range.
In your case, the data ranges from 1 to 3.8. So plot will look for pretty labels inside the range
1 - 0.04*(3.8-1) = 0.888
to
3.8 + 0.04*(3.8-1) = 3.912
4 is outside of this range and so will not appear as an axis label. For completeness, it is worth noting that "pretty" sounds like just a word, but actually has a technical meaning here - related to the pretty
function. If you look at the help page ?pretty
You will see the description:
Compute a sequence of about n+1 equally spaced ‘round’ values which cover the range of the values in x. The values are chosen so that they are 1, 2 or 5 times a power of 10.
There is additional detail on the help page.
Upvotes: 2
Reputation: 4169
You axis limit does not include 4; you need to overwrite the default limits of the plot (which it derives from the data) using xlim
:
plot(df$x, df$y, xaxt = "n", xlim = c(1, 4))
Note that when using axis
your specification of at
will become your labels
unless you overwrite that, so your script doesn't need to specify labels
; your script can become:
axis(side = 1, at = seq(0, 4, 0.5))
Upvotes: 2