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. 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(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. In your case, stat_smooth is using the loess method to compute the smoothing curve. 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. 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)
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.