Reputation:
I would like to always start with the first axis tick at the origin, such as in this image:
Playing around with xlim
, expand_limits
or scale_x_continuous
doesn't really work - there's always a little spacing between first axis tick and origin. There should be an easy way to do this. Many thanks in advance.
Upvotes: 0
Views: 2307
Reputation:
The solution that worked for me is to set the option expand=c(0,0)
together with the limits option, e.g.:
scale_y_continuous(expand = c(0, 0), limits = c(10,NA))
Y axis then starts at the origin with the first tick with label 10
.
Upvotes: 0
Reputation: 1580
Unfortunately, ggplot2
is set up expand the axes and thus pad the axis ticks, and there's not a simple function to force them to be flush at the origin. I find the best way to get axis ticks at the origin is to use coord_cartesian()
to turn off axis expansion and manually specify limits
require(ggplot2)
ggplot(mtcars, aes(wt, mpg)) +
geom_point() +
theme_classic() +
coord_cartesian(expand = FALSE, #turn off axis expansion (padding)
xlim = c(1, 6), ylim = c(10, 35)) #manually set limits
I prefer to define limits in coord_cartesian()
rather than in scale_y_...()
and scale_x_...()
because coord_cartesian()
crops the view but does not alter the underlying data. scale...()
functions remove data outside the limits, which could mess up fitted lines (stat_smooth()
) or summary stats if you crop out an outlier.
Upvotes: 1