R中未标出斜线

时间:2019-03-13 15:03:44

标签: r

代码:

plot(medalswimmers$Height, medalswimmers$Weight , cex=1.3, pch=16, xlab="Height of medal winning swimmers (in years)", ylab="Respective weight of medal winning swimmers (in cm)")
lm(medalswimmers$Height ~ medalswimmers$Weight)

输出:

Call:
lm(formula = medalswimmers$Height ~ medalswimmers$Weight)

系数:

(Intercept)  medalswimmers$Weight  
    129.2058                0.7146  

代码:

  abline(a = 129.2058, b= 0.7146, col="Blue") #-THIS DOES NOT PLOT???

An image of the plot without the regression line

1 个答案:

答案 0 :(得分:1)

您要绘制的线在打印窗口之外。您可以通过在图的极限处计算x值的y值来看到这一点:

# What value on the y-axis does the line have when x = 160?
> 129.2058 + 0.7146 * 160
[1] 243.5418

# What value on the y-axis does the line have when x = 200?
> 129.2058 + 0.7146 * 200
[1] 272.1258

这样做的原因是要在与您在线性模型中输入的轴相反的轴上绘制“高度”和“重量”。

请尝试:

 l1 <- lm(Height ~ Weight, data=medalswimmers)

 plot(medalswimmers$Weight, medalswimmers$Height, cex=1.3, pch=16, 
      ylab="Height of medal winning swimmers (in years)", 
      xlab="Respective weight of medal winning swimmers (in cm)")

 abline(a=coef(l1)["(Intercept)"], b=coef(l1)["Weight"], color="blue")