2014-11-05 98 views
0

我正在绘制一组带有阴影的数据,以遵循下限和上限值。我使用stat_smoothEst值应用平滑,但我无法获得由UB和LB值指定的阴影区域以跟随平滑线。这里的数据:在ggplot2下手动着色平滑线

Quantiles Est LB UB 
0.10 -4.39 -4.80 -4.00 
0.25 -3.46 -3.72 -3.22 
0.50 -3.11 -3.29 -2.91 
0.75 -2.89 -3.15 -2.60 
0.90 -1.69 -2.21 -1.09 

这是我的ggplot代码:

ggplot(data,aes(y=Est,x=Quantiles)) + stat_smooth() + 
geom_ribbon(aes(ymin=LB,ymax=UB),alpha=0.2) 

在此先感谢您的帮助!

+0

任何机会,你可以把问题一点点更多[重复性( http://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example)通过提供一些数据来处理? – shekeine 2014-11-05 13:37:46

回答

0

我的理解是,您希望带状边界遵循平滑曲线,而不是简单地连接LB和UB点。在你的情况下,stat_smooth使用黄土方法来计算平滑曲线。您没有足够的数据点来使用默认顺序2计算真实的平滑黄土曲线,因此黄土函数会返回穿过给定数据数据点的曲线,并使用二次平滑曲线将其连接起来,并在警告消息中报告。忽略这些警告,以获得平滑色带的一种方法是计算数据平滑点和色带边界,然后绘制效果如下图所示:

smooth <- data.frame(Quantiles= seq(min(data$Quantiles), max(data$Quantiles), length.out=100)) 
smooth$Est <- predict(loess(Est ~ Quantiles, data), newdata=smooth$Quantiles) 
smooth$LB <- predict(loess(LB ~ Quantiles, data), newdata=smooth$Quantiles) 
smooth$UB <- predict(loess(UB ~ Quantiles, data), newdata=smooth$Quantiles) 
ggplot(data=smooth,aes(y=Est,x=Quantiles)) + geom_line() + 
    geom_ribbon(aes(ymin=LB,ymax=UB),alpha=0.2) 
+0

从来没有想过我必须使用predict(),但它解决了这个问题。谢谢! – FadzliFuzi 2014-11-10 14:38:21