你可以試試這個:
n <- 10 # generate 10 data points
d <- data.frame(x = 1:n, y = rnorm(n))
# with loes smoothing (span parameter controls the degree of smoothing)
library(ggplot2)
ggplot() + geom_point(data=d,aes(x,y), size=5) +
geom_smooth(data=d,aes(x,y, colour='span=0.5'), span=0.5, se=FALSE) +
geom_smooth(data=d,aes(x,y, colour='span=0.6'), span=0.6, se=FALSE) +
geom_smooth(data=d,aes(x,y, colour='span=0.7'), span=0.7, se=FALSE) +
geom_smooth(data=d,aes(x,y, colour='span=0.8'), span=0.8, se=FALSE)
# with B-spline curves using lm (degree of polynomial fitted controls the smoothness)
ggplot() +
geom_point(data=d, aes(x, y), size=5) +
geom_smooth(data=d, aes(x, y,col='degree=3'), method = "lm", formula = y ~ splines::bs(x, 3), se = FALSE) +
geom_smooth(data=d, aes(x, y,col='degree=4'), method = "lm", formula = y ~ splines::bs(x, 4), se = FALSE) +
geom_smooth(data=d, aes(x, y,col='degree=5'), method = "lm", formula = y ~ splines::bs(x, 5), se = FALSE) +
geom_smooth(data=d, aes(x, y,col='degree=6'), method = "lm", formula = y ~ splines::bs(x, 6), se = FALSE) +
geom_smooth(data=d, aes(x, y,col='degree=7'), method = "lm", formula = y ~ splines::bs(x, 6), se = FALSE)
# with smooth.spline (spar parameter control smoothness of the fitted curve)
colors <- rainbow(100)
plot(d$x, d$y, pch=19, xlab='x', ylab='y')
i <- 1
for (spar in seq(0.001,1,length=100)) {
lines(smooth.spline(d$x, d$y, spar=spar, all.knots=TRUE)$y, col=colors[i])
i <- i + 1
}
points(d$x, d$y, pch=19)
你是對的;平滑的插值是我想要的。感謝您的建議。 –
如果'DF'是一個輸入數據幀,那麼'dput(DF)'將以可重複的方式輸出它,以便響應者可以輕鬆地將其複製並粘貼到其會話中。 –
好吧我現在明白了。如果我有其他問題,我會確保使用dput()。 –