如何在 R 中的单个图中制作两条特定长度的回归线?

How to make two specific lengths of regression lines in a single plot in R?

我在minitab中做了线性回归

我想在 R 中制作一个漂亮的图,但我不知道如何限制线条的长度。我能够制作出类似的东西 这个 但我想限制线条的长度,并希望制作出与 minitab 输出一样的线条。 感谢您的时间和帮助!

代码如下:

plot(y=c(-3,-2,-1,0,1,2),  x=c(-102,-100,-98,-96,-94,-92), type="n", xlab = 'Longitude', ylab = 'Factor4Score')
g <- points(f1[1:38] ~ LON[1:38], col = "red", pch=21)
g.lm <-lm(f4[1:38] ~ LON[1:38])
abline(g.lm, col = "red")
plot(y=c(-3,-2,-1,0,1,2), x=c(-102,-100,-98,-96,-94,-92), type="n", xlab = 'Longitude', ylab = 'Factor3Score')
s <-points(f4[39:102] ~ LON[39:102], col="green",pch=23 )
s.lm <-lm(f4[39:102] ~ LON[39:102])
abline(s.lm, col = "green")

而不是使用

abline(g.lm, col = "red")
abline(s.lm, col = "green")

您可以使用内置的 lines() 函数来创建回归线。

在你的例子中,我会使用

lines(x = -102:-95, y = coefficients(g.lm)[1] + coefficients(g.lm)[2]* (-102:-95), col = 'red')
lines(x = -95:-92, y = coefficients(s.lm)[1] + coefficients(s.lm)[2]* (-95:-92), col = 'green')