如何在ggplot2中使用spline平滑算法

smooth.spline2 <- function(formula, data, ...) {
mat <- model.frame(formula, data)
smooth.spline(mat[, 2], mat[, 1])
}

predictdf.smooth.spline <- function(model, xseq, se, level) {
pred <- predict(model, xseq)
data.frame(x = xseq, y = pred$y)
}

qplot(mpg, wt, data = mtcars) + geom_smooth(method = "smooth.spline2", se= F)

spline是很好的连接点之后将其平滑的算法,但是ggplot中不能直接用,这里提供了一个子函数,可以使用spline让曲线平滑,非常好用。

原网址:http://www.lgbmi.com/2011/10/using-smooth-spline-in-stat-scale-in-ggplot2/

posted on 2014-06-19 22:23  云中道长  阅读(2816)  评论(0编辑  收藏  举报

导航