简体   繁体   English

ggplot2,facet_grid,自由秤?

[英]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?在以下示例中,如何根据每个面板中的数据来缩放 y 轴范围?

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.也许是因为你只有一个 y 轴,用你的方式。 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您可以改用facet_wrap ,这将“释放”两个轴

希望这会有所帮助。

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.我很抱歉用一个新的答案来回答这个 12 岁的问题,但我认为它可能有用。 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.如果您想保留网格布局,但想要类似环绕的自由比例,您可能会对ggh4x::facet_grid2()感兴趣,它有一个independent的参数,可以让轴在网格布局中的行或列内变化。

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)reprex 包(v2.0.1) 创建于 2022-06-09

(Disclaimer: I'm the author of ggh4x) (免责声明:我是ggh4x的作者)

Try: https://teunbrand.github.io/ggh4x/reference/facet_grid2.html .试试: 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.此代码允许使用 facet_grid 使每个面板的比例独立,但使用 facet_grid2。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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