简体   繁体   English

如何使用R在散点图上创建线性回归线?

[英]How can I create a linear regression line on a scatterplot with R?

I tried abline function to create a linear regression line on a scatterplot. 我尝试使用abline函数在散点图上创建一个线性回归线。

x= c (1.0325477, 0.6746901, 1.0845737, 1.1123872, 1.1060822, 0.8595918, 0.8512941, 1.0148842, 1.0722369, 0.9019220 , 0.8809147, 1.0358256, 0.9903858, 1.0715174 , 1.1034405, 1.0143966,0.9802365, 0.7177169 , 0.9190783, 0.8408701 ) 
y= c (0.8550177, 0.8352162 ,1.0236998, 1.1071665, 0.6768144, 0.8449983 ,0.7616483, 0.8259199, 1.1539598, 1.4125006, 1.0511816, 0.9366184, 1.4101268, 1.2937913, 1.4147219 ,1.2943105 ,0.7859749, 0.6689330, 0.6940164, 0.8093392)
plot(x,y) 
abline(lm(y ~ x))
Error in int_abline(a = a, b = b, h = h, v = v, untf = untf, ...) : 
 plot.new has not been called yet

Any suggestions please 请给我任何建议

Works fine for me too as for the rest. 和其他人一样对我来说也很好。 Try to close and re-open R. 尝试关闭并重新打开R.

If still problematic, can try: 如果仍有问题,可以尝试:

# put x and y in a data frame
dat<-data.frame(x=x,y=y)
attach(dat)
plot(x,y) 
abline(lm(y ~ x))

plot(x~y) 积(X〜Y)

abline(lm(x~y)) abline(LM(X〜Y))

should fix that code. 应修复该代码。 On MacOs it makes it work. 在MacOs上它使它工作。

First create a file to show your output such as pdf or svg files then same code will obviously help. 首先创建一个文件来显示你的输出,如pdf或svg文件,然后相同的代码显然会有所帮助。 For more clarification 有关更多说明

    svg("filename1.svg")
    plot(x ~ y)
    abline(lm(y ~ x))
    dev.off()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM