ggplot2 axis limit gotchas
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.
Setting axis limits in ggplot has behaviour that may be unexpected: any data that falls outside of the limits is ignored, instead of just being hidden. This means that if you are apply a statistic or calculation on the data, like plotting a box and whiskers plot, the result will only be based on the data within the limits.
In other words, ggplot doesn’t “zoom in” on a part of your plot when you apply an axis limit, it recalculates a new plot with the restricted data.
For example, here is a boxplot without any axis limits:
and here is the same one with a set Y axis limit:
Note how the box and whiskers are recalculated in this plot. To be fair, ggplot does warn you that it is removing rows!
If you want to zoom in without removing values, use coord_cartesian
:
R-bloggers.com offers daily e-mail updates about R news and tutorials about learning R and many other topics. Click here if you're looking to post or find an R/data-science job.
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.