2014-11-05 56 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