使用多面图确定 ylim 时如何忽略一个因素

发布于 2024-11-01 19:18:53 字数 596 浏览 3 评论 0 原文

在下面的(无意义的)示例中,我想绘制 y1 和 y2 曲线,但根据 y1 曲线确定 ylim,忽略 y2。

这是示例:

library(ggplot2)

curves <- data.frame(expand.grid(seq(-2,2,0.1), c(2,4), c(1,2)))
names(curves) <- c("x","p","c")

curves$y1 <- splat(function(x,p,c, ...) c * p * exp(- x^p))(curves)
curves$y2 <- splat(function(x,p,c, ...) c + x * p)(curves)
curves <- melt.data.frame(curves, id.vars=1:3)

ggplot(curves, aes(x, value, color = variable)) +
    geom_line() +
    facet_grid(p ~ c, scales="free_y")

我希望第一行具有 ylim(0,4),第二行具有 ylim(0,8)。有什么想法吗?最好是如何让 ggplot 确定正确的限制,而不是手动输入它们?

In the following (nonsensical) example, I would like to plot both y1 and y2 curves, but have the ylim determined according to the y1 curve, ignoring y2.

Here's the example:

library(ggplot2)

curves <- data.frame(expand.grid(seq(-2,2,0.1), c(2,4), c(1,2)))
names(curves) <- c("x","p","c")

curves$y1 <- splat(function(x,p,c, ...) c * p * exp(- x^p))(curves)
curves$y2 <- splat(function(x,p,c, ...) c + x * p)(curves)
curves <- melt.data.frame(curves, id.vars=1:3)

ggplot(curves, aes(x, value, color = variable)) +
    geom_line() +
    facet_grid(p ~ c, scales="free_y")

I would like the first row to have ylim(0,4) and the second row to have ylim(0,8). Any thoughts? Preferably on how to have ggplot determine the correct limits, rather than entering them manually?

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

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

发布评论

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

评论(2

把时间冻结 2024-11-08 19:18:53

下面的作品,虽然感觉很笨拙。毫无疑问,你可以在这方面进行改进。

我的解决方法是从 data.frame 中删除您不想包含在图中的值:

curves <- subset(curves, !(curves$p==2 & (curves$value>4 | curves$value<0)))
curves <- subset(curves, !(curves$p==4 & (curves$value>8 | curves$value<0)))

ggplot(curves, aes(x, value, color = variable)) +
        geom_line() +
        facet_grid(p ~ c, scales="free_y")

在此处输入图像描述

The following works, although it feels clumsy. No doubt you can improve on this.

My workaround is to delete values from the data.frame that you don't want to include in the plot:

curves <- subset(curves, !(curves$p==2 & (curves$value>4 | curves$value<0)))
curves <- subset(curves, !(curves$p==4 & (curves$value>8 | curves$value<0)))

ggplot(curves, aes(x, value, color = variable)) +
        geom_line() +
        facet_grid(p ~ c, scales="free_y")

enter image description here

淡紫姑娘! 2024-11-08 19:18:53

如果你以这个相当冗长的代码结束,

ylimits <- c(  floor(min(curves$value[curves$variable == "y1"])),
             ceiling(max(curves$value[curves$variable == "y1"])) )

ggplot(curves, aes(x, value, color = variable)) +
    geom_line() +
    facet_grid(p ~ c, scales = "free_y") +
    scale_y_continuous(breaks = ylimits[1]:ylimits[2]) +
    coord_cartesian(ylim = ylimits) 

你会得到这个,

它基于y轴比例y1 曲线(虽然不在你的 4 和 8 上)。

If you end with this rather verbose code

ylimits <- c(  floor(min(curves$value[curves$variable == "y1"])),
             ceiling(max(curves$value[curves$variable == "y1"])) )

ggplot(curves, aes(x, value, color = variable)) +
    geom_line() +
    facet_grid(p ~ c, scales = "free_y") +
    scale_y_continuous(breaks = ylimits[1]:ylimits[2]) +
    coord_cartesian(ylim = ylimits) 

you get this,

Scale decided by red curves

which bases the y-axis scale on the y1 curve (though not on your 4 and 8).

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