If you want to plot a chart with a few outliers in ggplot, you might be temped to use ylim. The problem with ylim is that it removes the data points that go beyond the limits.

For instance, if you have the following data:

data <- data.frame(x = 1:20, y = c(rnorm(19), 500))

If you plot this with no changes, this is what you get:

ggplot(data, aes(x,y)) + geom_line()
Image for post
Image for post

It’s kind of hard to understand what’s going on in other points other than the last, so some zooming comes in handy. If you use ylim, this is what happens:

ggplot(data, aes(x,y)) + geom_line() + ylim(-5…


Rodrigo Sousa Coutinho

Hi! I’m co-founder and Strategic Product Manager at OutSystems, with a passion for mobile and web development, great products, and geeky stuff!

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store