431

I plot the following:

library(ggplot2)    

carrots <- data.frame(length = rnorm(500000, 10000, 10000))
cukes <- data.frame(length = rnorm(50000, 10000, 20000))
carrots$veg <- 'carrot'
cukes$veg <- 'cuke'
vegLengths <- rbind(carrots, cukes)

ggplot(vegLengths, aes(length, fill = veg)) +
 geom_density(alpha = 0.2)

Now say, I only want to plot the region between x=-5000 to 5000, instead of the entire range.

How can I do that?

AndrewGB
  • 16,126
  • 5
  • 18
  • 49
David B
  • 29,258
  • 50
  • 133
  • 186

2 Answers2

693

Basically you have two options

scale_x_continuous(limits = c(-5000, 5000))

or

coord_cartesian(xlim = c(-5000, 5000)) 

Where the first removes all data points outside the given range and the second only adjusts the visible area. In most cases you would not see the difference, but if you fit anything to the data it would probably change the fitted values.

You can also use the shorthand function xlim (or ylim), which like the first option removes data points outside of the given range:

+ xlim(-5000, 5000)

For more information check the description of coord_cartesian.

The RStudio cheatsheet for ggplot2 makes this quite clear visually. Here is a small section of that cheatsheet:

enter image description here

Distributed under CC BY.

Axeman
  • 32,068
  • 8
  • 81
  • 94
midtiby
  • 14,550
  • 6
  • 34
  • 43
  • 20
    there's also now `library(scales); ... + scale_x_continuous(limits = c(-5000, 5000), oob=squish)` (the default is `oob=censor`); see `?squish`, `?censor`: https://groups.google.com/forum/#!topic/ggplot2/AsJ6xpmR9tU – Ben Bolker Nov 02 '13 at 21:07
  • 6
    NB. this might be problematic if you're dealing with lines/polygons where some vertices are outside the limits, as the whole object is removed from the plot – geotheory Sep 05 '14 at 11:35
  • 1
    @geotheory: is that also true of the `coord_cartesian` approach? – Nick Stauner Oct 16 '14 at 02:03
  • 1
    No I should've been more specific, just the first method – geotheory Oct 16 '14 at 08:30
  • In practice, for 'printing' purposes, with ``coord_cartesian(xlim = `` you probably need to reset ``ylim`` as well, and reset the label and grid breaks. – PatrickT Oct 17 '15 at 13:30
  • Hello, I'm creating a complex plot that uses geom_smooth(). And I don't know why these two approaches produce different curves, though keeping everything else the same: scale_y_continuous(limits=c(0,0.013)) and coord_cartesian(ylim = c(0,0.013)) Which one is the proper one? I just want to cut the y-axis. – skan Sep 18 '18 at 15:01
55

Quick note: if you're also using coord_flip() to flip the x and the y axis, you won't be able to set range limits using coord_cartesian() because those two functions are exclusive (see here).

Fortunately, this is an easy fix; set your limits within coord_flip() like so:

p + coord_flip(ylim = c(3,5), xlim = c(100, 400))

This just alters the visible range (i.e. doesn't remove data points).

Bill
  • 670
  • 5
  • 6
  • I similar but harder question posted here https://stackoverflow.com/questions/61531149/how-to-set-just-one-limit-for-axes-in-ggplot2-r-plots on how to limit one SIDE ONLY – IVIM Apr 30 '20 at 19:22