【问题标题】:Adding custom regression line with set intercept and slope to ggplot将具有设置截距和斜率的自定义回归线添加到 ggplot
【发布时间】:2021-10-25 19:59:20
【问题描述】:

我使用以下代码创建了一个绘图:

ggplot(Data, aes(x=damMean, y=progenyMean)) + 
    geom_point()

我想在图上叠加一条如下形式的回归线:y = 69.88 + 5.58*x

我尝试通过添加以下内容来做到这一点:

ggplot(Data, aes(x=damMean, y=progenyMean)) + 
    geom_point() + 
    geom_smooth(method = "lm", formula = y~69.88+5.58*x)

但这并没有在情节中添加一条线。

使用 ggplot 可以做到这一点吗?

【问题讨论】:

  • + geom_abline(slope=5.58, intercept=69.88)
  • @DaveArmstrong 这似乎没有用。没有任何错误消息,但曲线没有显示在图表上
  • 刚刚发布了一个适合我的答案。我认为问题在于回归线实际上不在窗口中。例如,5.58*40 + 69.88=289.88。因此,该线将包含点 (40,289.88)。您的 y 轴仅达到 110 左右,因此该线不包含在绘图区域内。

标签: r ggplot2 regression linear-regression


【解决方案1】:

这是一个带有一些虚假数据的示例:

mydat <- tibble(x=runif(100, 40, 90), 
                y = 80 + 5.5*x + rnorm(100, 0, 10))

ggplot(mydat, aes(x=x, y=y)) + 
  geom_point() + 
  geom_abline(slope=5.58, intercept=69.88)

【讨论】:

  • 看起来不错。我的geom_abline(slope=5.58, intercept=69.88) 没有出现在剧情中是有原因的吗?
  • 见上面的评论。
猜你喜欢
  • 2018-05-08
  • 1970-01-01
  • 2016-06-26
  • 2019-05-27
  • 1970-01-01
  • 2015-11-01
  • 2017-03-01
  • 2016-01-22
相关资源
最近更新 更多