如何强制回归线从100开始?

问题描述 投票:0回答:1
ggplot(DTT_Half, aes(x = Time.in.minutes, y = Percentage.of.DTT)) + 
  geom_point() + 
    coord_cartesian(ylim = c(0,105), xlim = c(0, 10.5)) +
  stat_smooth(method = "lm", se = FALSE) +
  scale_y_continuous(expand = c(0,0), breaks = seq(0, 100, by = 10))+
  scale_x_continuous(expand = c(0,0)) +
  stat_regline_equation(label.y = 100, label.x = 7.5, 
                        aes(label = ..eq.label..)) 

Regression line not starting from 100

我希望回归线从 0,100 开始,这应该反映在显示的线的方程中。

我尝试将 stat_smooth 函数中的公式设置为各种方程,但没有一个成功。

r ggplot2
1个回答
0
投票

lm
与自定义
offset
一起使用,如评论中所述。然后
plot
并添加
abline
配合。

> fit <- lm(perc ~ time, dat, offset=rep_len(-1, nrow(dat)))
> plot(dat, ylim=c(0, 100), xpd=TRUE, pch=20)
> abline(fit, col='blue')
> legend('topright', do.call('sprintf', 
+                            as.list(c('y = %s%sx', 
+                                      signif(unlist(fit['coefficients']), 2)))), 
+        bty='n', text.font=3)


数据:

> dput(dat)
structure(list(time = c(0, 5, 10), perc = c(100L, 69L, 44L)), row.names = c(1L, 
3L, 5L), class = "data.frame")
© www.soinside.com 2019 - 2024. All rights reserved.