0

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!

jazzurro
  • 23,179
  • 35
  • 66
  • 76
FadzliFuzi
  • 71
  • 1
  • 6
  • Any chance you could make the issue a tad more [reproducible](http://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) by providing some data to work with? – shekeine Nov 05 '14 at 13:37

1 Answers1

0

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)
WaltS
  • 5,410
  • 2
  • 18
  • 24