ggplot2, facet_grid, free scales?

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")
115233 次浏览

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")

You can't. See here

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

Hopefully, this helps.

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

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.

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)