【问题标题】:ggplot smooth pass aes variable to method.argsggplot 平滑将 aes 变量传递给 method.args
【发布时间】:2017-05-12 06:29:39
【问题描述】:

经过多次谷歌搜索,我决定向你们寻求帮助,伙计们。

我只是在不同时间点绘制一些观察结果,我想用 stat_smooth 添加线性回归。但是,我想要截距为 100 的线性模型(因为数据是相对于时间 0 的百分比)。为此,我发现最简单的方法是使用 lm 中的 offset 参数。问题是如何获取每组(col 和 facet 组)的 'y' 观察数以将其传递给偏移参数。

如果我使用每组观察次数相同的数据(在我的情况下为 10 个),我可以只写数字,效果很好:

myplot <- ggplot(mydt2, aes(x=Time_point, y=GFP_rel, col=Gene, fill=Gene,group=Gene))
myplot <- myplot + stat_smooth(method='lm', formula = y ~ x + 0, method.args=list(offset=rep(100,10))) + 
  facet_wrap(~Cell_line)

但是,这不是很优雅和/或灵活。我的问题是:如何将观察次数传递给 method.args?我尝试了 offset(100,..count..),但出现错误:(列表)对象不能被强制输入“整数”)。

有什么建议吗?

谢谢

【问题讨论】:

  • 你能发布一些数据吗?
  • 我会这样做,但我不知道怎么做。我无法编辑问题。
  • 谢谢@aosmith。我已经尝试过了,但它绘制的数据相对于零而不是 100。

标签: r ggplot2


【解决方案1】:

您可以在公式中使用I(y - 100) 编码,如here 所示,而不是使用偏移量。

但是,stat_smooth 的预测值将是 y - 100 的预测值,而不是 y。此行将经过 0。您可以将行移回该位置以使用 position_nudge 显示原始 y 变量的预测。

所以stat_smooth 代码看起来像

stat_smooth(method = "lm", formula = I(y - 100) ~ x + 0, 
            position = position_nudge(y = 100))

【讨论】:

  • 谢谢@aosmith。 position_nudge 解决了偏移量。
猜你喜欢
  • 1970-01-01
  • 2020-03-06
  • 2012-07-27
  • 1970-01-01
  • 2018-07-29
  • 1970-01-01
  • 1970-01-01
  • 2014-08-29
  • 1970-01-01
相关资源
最近更新 更多