Plot fitted line within certain range R

前端 未结 5 2167
执笔经年
执笔经年 2020-12-31 05:26

Using R, I would like to plot a linear relationship between two variables, but I would like the fitted line to be present only within the range of the data.

For exam

5条回答
  •  小蘑菇
    小蘑菇 (楼主)
    2020-12-31 05:57

    You can do this using predict.

    You can predict on specific values of x (see ?predict)

    x<-1:10
    y<-1:10
    plot(x,y)
    new <- data.frame(x = seq(1, 5, 0.5))
    lines(new$x, predict(lm(y~x), new))
    

    enter image description here

提交回复
热议问题