How to fit a smooth curve to my data in R?

前端 未结 8 1912
无人共我
无人共我 2020-11-29 15:59

I\'m trying to draw a smooth curve in R. I have the following simple toy data:

> x
 [1]  1  2  3  4  5  6  7  8  9 10
> y
 [1]  2  4  6  8         


        
8条回答
  •  时光说笑
    2020-11-29 16:29

    In order to get it REALLY smoooth...

    x <- 1:10
    y <- c(2,4,6,8,7,8,14,16,18,20)
    lo <- loess(y~x)
    plot(x,y)
    xl <- seq(min(x),max(x), (max(x) - min(x))/1000)
    lines(xl, predict(lo,xl), col='red', lwd=2)
    

    This style interpolates lots of extra points and gets you a curve that is very smooth. It also appears to be the the approach that ggplot takes. If the standard level of smoothness is fine you can just use.

    scatter.smooth(x, y)
    

提交回复
热议问题