用于通过loess
函数绘制一堆点的geom_smooth
估计值以及置信区间。
现在我需要改变计算置信界限的方法(即我需要改变模糊带的形状)。有没有办法在geom_smooth
中执行此操作?
或者,如何使用ggplot2
模拟它?我怎么能这么模糊乐队?
答案 0 :(得分:0)
如果你需要绘制一些不是geom_smooth
中任何一个选项的东西,你最好的选择是自己手动拟合模型。
你还没说过你需要什么方法。 但是这里有一个将黄土与家族对称拟合并计算其标准误差的例子。
d <- data.frame(x = rnorm(100), y = rnorm(100))
# The original plot using the default loess method
p <- ggplot(d, aes(x, y)) + geom_smooth(method = 'loess', se = TRUE)
# Fit loess model with family = 'symmetric'
# Replace the next 2 lines with whatever different method you need
loess_smooth <- loess(d$x ~ d$y, family = 'symmetric')
# Predict the model over the range of data you are interested in.
loess_pred <- predict(loess_smooth,
newdata = seq(min(d$x), max(d$x), length.out = 1000),
se = TRUE)
loess.df <- data.frame(fit = loess_pred$fit,
x = seq(min(d$x), max(d$x), length.out = 1000),
upper = loess_pred$fit + loess_pred$se.fit,
lower = loess_pred$fit - loess_pred$se.fit)
# plot to compare
p +
geom_ribbon(data = loess.df, aes(x = x, y = fit, ymax = upper, ymin = lower), alpha = 0.6) +
geom_line(data = loess.df, aes(x = x, y = fit))