Zoom with break adjustment in ggplot2


coord_cartesian() supports to zoom by its argument xlim and ylim.

p <- qplot(disp, wt, data=mtcars) + geom_smooth()

p + coord_cartesian(xlim = c(325, 500), ylim=c(3, 5.5)) # keep original breaks
p + coord_cartesian(xlim = c(325, 500), ylim=c(3, 5.5), wise = TRUE) # re-fit the breaks

 

Keep original breaks:

Re-fit the breaks:


 
Note that function xlim() and ylim() do more than zooming: they trim the data points outside the limits BEFORE calculating stats.

3 thoughts on “Zoom with break adjustment in ggplot2

  1. Hi Kohske:
    I ran into your website and found very useful examples. I am fairly familiar with ggplot2 but not being a statistician is hard sometimes to know how some of the geoms are created. For instance, I use geom_smooth quite a lot but I don’t know where the data that creates it come from. Do you by any chance how to manually generate the data that creates stat_smooth or geom_smooth?

  2. kohske, I’ve seen your helpful tips in all over the place as I’ve worked with ggplot2. Thanks a million for helping us out. You’ve saved me more than once.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s