99

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")
2
  • Anyone have any idea what the intended purpose is of the scales argument for facet_grid? Commented Sep 24, 2018 at 13:41
  • @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. Commented Apr 9, 2021 at 21:26

5 Answers 5

95

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")
Sign up to request clarification or add additional context in comments.

3 Comments

I see - so it doesn't scale each panel by by rows or columns... in the case facet_wrap() also works I guess.
How about plotting the same data but usign barplots? I asked just that here
I feel this does not resolve the issue. I am having the same problem, and all "cyl ~ ." did was flip the axes. Now, the x axis is what I want to be free, but it is not free. I am using R version 3.2.3 with ggplot2 version 2.1.0.
85
Answer recommended by R Language Collective

I'm sorry for jumping on this 12 year old question with a new answer, but I think it might be useful. If you want to preserve the grid layout, but want wrap-like free scales, you might be interested in ggh4x::facet_grid2() which has an independent argument that lets an axis vary within a row or column in a grid-layout.

library(ggplot2)

ggplot(mtcars, aes(mpg, wt, colour = factor(cyl))) + 
  geom_point() +
  ggh4x::facet_grid2(. ~ cyl, scales = "free_y", independent = "y")

Created on 2022-06-09 by the reprex package (v2.0.1)

(Disclaimer: I'm the author of ggh4x)

7 Comments

Very neat solution, nicely written package! Thanks!!
This should be the accepted answer for this question. ggh4x is great, thank you!
Thank you so much for this. If I could upvote your answer (and your package) twice, I would!
Excellent extension, love the independent parameter!
I just discovered ggh4x from this answer, I think I'm a kid in the candy store... scale_colour_multi!
|
40

You can't. See here

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

Comments

22

Hopefully, this helps.

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

Comments

2

Try: https://teunbrand.github.io/ggh4x/reference/facet_grid2.html.

This code allows to make the scales of each panel independent using facet_grid, but with facet_grid2.

1 Comment

It's helpful to show the actual code that should be used rather than just providing a link

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.