简体   繁体   中英

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

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

希望这会有所帮助。

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

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)

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM