简体   繁体   English

在ggplot2下手动着色平滑线

[英]Manually shading a smoothed line under ggplot2

I am plotting a set of data with shading to follow the Lower and Upper Bound values. 我正在绘制一组带有阴影的数据,以遵循“下边界”和“上边界”值。 I applied smoothing using stat_smooth for Est values but I couldn't get shading region specified by the UB and LB values to follow the smoothed line. 我对Est值使用stat_smooth进行了平滑处理,但无法获得UB和LB值指定的阴影区域来遵循平滑线。 Here's the data: 数据如下:

 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

And here's my ggplot code: 这是我的ggplot代码:

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

Thanks in advance for any help! 在此先感谢您的帮助!

My understanding is that you would like the ribbon boundaries to followed the smoothed curve rather than simply connect the LB and UB points. 我的理解是,您希望色带边界遵循平滑的曲线,而不是简单地连接LB和UB点。 In your case, stat_smooth is using the loess method to compute the smoothing curve. 在您的情况下,stat_smooth使用黄土方法来计算平滑曲线。 You don't have enough data points to compute a true smoothed loess curve using the default order of 2 so the loess function returns a curve passing through the given data data points, joins these using a quadratic smoothing curve, and reports this in warning messages. 您没有足够的数据点来使用默认的2阶计算真实的平滑黄土曲线,因此loess函数返回一条曲线,该曲线穿过给定的数据数据点,使用二次平滑曲线将它们连接起来,并在警告消息中报告此情况。 。 Ignoring these warnings, one way to get the smoothed ribbon is to compute the data smoothing points and ribbon boundaries and then plot the results as shown below: 忽略这些警告,获得平滑功能区的一种方法是计算数据平滑点和功能区边界,然后绘制结果,如下所示:

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)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM