ggplot2、facet_grid、自由尺度?

发布于 2024-09-18 18:25:55 字数 267 浏览 1 评论 0原文

在下面的示例中,如何根据每个面板中的数据来缩放 y 轴限制?

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

这些都不会做到这一点:

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

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

秋意浓 2024-09-25 18:25:56

尝试: https://teunbrand.github.io/ggh4x/reference/facet_grid2.html< /a>.

此代码允许使用facet_grid(但使用facet_grid2)使每个面板的比例独立。

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.

天冷不及心凉 2024-09-25 18:25:55

也许是因为你只有一个 y 轴,用你的方式。你尝试过这样的事情吗?

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

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")
尐籹人 2024-09-25 18:25:55

我很抱歉突然提出这个 12 年前的问题并给出了新答案,但我认为它可能有用。如果您想保留网格布局,但想要像环绕一样的自由刻度,您可能会对 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")

reprex 包 (v2.0.1)

(免责声明:我是 ggh4x 的作者)

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)

江南月 2024-09-25 18:25:55

你不能。请参阅此处

您可以使用facet_wrap代替,它将“免费” ' 两个轴

You can't. See here

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

故事未完 2024-09-25 18:25:55

希望这会有所帮助。

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

Hopefully, this helps.

mt + facet_wrap(. ~ cyl, scales="free_y")
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文