67
votes

In the following example, how do I get the y-axis limits to scale according to the data in each panel?

mt <- ggplot(mtcars, aes(mpg, wt, colour = factor(cyl))) + geom_point() 

Neither of these will do it:

mt + facet_grid(. ~ cyl, scales="free")
mt + facet_grid(. ~ cyl, scales="free_y")
3
Anyone have any idea what the intended purpose is of the scales argument for facet_grid?geotheory
@geotheory it's in the docs. The scales argument is for freeing the x, y, or both scales for each facetted plot. Your options are 'fixed' (default), 'free_x', 'free_y', or 'free' for both. Use it when the ranges of your variables vary greatly and need to be freed.kentkr

3 Answers

72
votes

Perhaps it's because you have only one y axis, using your way. Did you try something like this?

mt + facet_grid(cyl ~ ., scales="free")
28
votes

You can't. See here

You can use facet_wrap instead, which will 'free' both axes

12
votes

Hopefully, this helps.

mt + facet_wrap(. ~ cyl, scales="free_y")