dfrankow
dfrankow

Reputation: 21447

How to get geom_boxplot to apply y-limits before calculating boxes?

Here is a boxplot:

qplot(cyl, mpg, data=mtcars) +
  geom_boxplot() +
  scale_y_continuous()

that looks like this: enter image description here

Now I add limits to the y axis:

qplot(cyl, mpg, data=mtcars) +
  geom_boxplot() +
  scale_y_continuous(limits=c(0,20))

and the whole picture changes:

enter image description here

How do I get the same picture as the first, just with a 'viewport' showing y=0 to 20?

And .. what is the second picture? It looks like there is actually data missing (example: 3-4 points for cyl=4).

Upvotes: 3

Views: 6993

Answers (1)

dfrankow
dfrankow

Reputation: 21447

From joran's comment, this works:

qplot(cyl, mpg, data=mtcars) +
  geom_boxplot() +
  coord_cartesian(ylim=c(0,20))

From the docs:

There are two ways of zooming the plot display: with scales or with coordinate systems. They work in two rather different ways.

Setting the limits on a scale will throw away all data that's not inside these limits. This is equivalent to plotting a subset of the original data

Setting the limits on the coordinate system performs a visual zoom the data is unchanged, and we just view a small portion of the original plot.

Thanks joran!

Upvotes: 10

Related Questions