我尝试在绘图上创建包含数据点最大值的平滑线。我已经搜索了很多并且用黄土()和ksmooth()进行了修补,但我还没有让它工作。
到目前为止,我最好的尝试是使用ksmooth(),但该行没有通过最大数据点
我是化学家,而不是统计学家,因此各种平滑技术的方法/描述经常超出我的想象。任何建议都会非常感激。
编辑:只是想让一些事情更清晰。基本上我所追求的是以下绘图的平滑版本,线条经过最大y值。
要在第一张图片中生成绘图,我使用了以下代码:
plot(ChiM~Temp, xlim=c(2,6), ylim=c(0,0.225), lwd=2, pch=16, col='red',subset=(v=='20'), main='Out-of-Phase AC Suscetability Plot', xlab='Temperature (K)', ylab=expression(chi[M]*'" (cm'^3*~'mol'^-1*')'))
setone <- subset(DSM32ac, v=='20') #v=20 is the subset of the data I have provided
attach(setone)
lines(ksmooth(Temp, ChiM, 'normal', bandwidth=0.5), col='red',lwd=2)
我希望这会让事情变得更加清晰。如果您需要更多信息来回答这个问题,请告诉我。
编辑2 我删除了数据,因为我无法制作整洁的表格。如果它非常重要,我会尝试将其重新放入。
答案 0 :(得分:2)
试试这个:
y <- c(.07, .12, .17, .11, .04, .02, .01)
x <- seq_along(y)
s <- spline(x, y)
plot(y ~ x)
lines(s)
,并提供:
答案 1 :(得分:0)
你可以试试这个:
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)