I am trying to draw an ECDF of some data with a "confidence interval" represented across a shaded area using ggplot2 . I am having problems combining geom_ribbon() with stat_ecdf() to achieve the effect that I get after.
Consider the following data examples:
set.seed(1) dat <- data.frame(variable = rlnorm(100) + 2) dat <- transform(dat, lower = variable - 2, upper = variable + 2) > head(dat) variable lower upper 1 2.534484 0.5344838 4.534484 2 3.201587 1.2015872 5.201587 3 2.433602 0.4336018 4.433602 4 6.929713 4.9297132 8.929713 5 3.390284 1.3902836 5.390284 6 2.440225 0.4402254 4.440225
I can create an ECDF variable using
library("ggplot2") ggplot(dat, aes(x = variable)) + geom_step(stat = "ecdf")
However, I cannot use lower and upper as the ymax and ymax aesthetics of geom_ribbon() to impose a confidence interval on the chart as another layer. I tried:
ggplot(dat, aes(x = variable)) + geom_ribbon(aes(ymin = lower, ymax = upper), stat = "ecdf") + geom_step(stat = "ecdf")
but this causes the following error:
Error: geom_ribbon requires the following missing aesthetics: ymin, ymax
Is there a way to coax geom_ribbon() to work with stat_ecdf() to create a shaded confidence interval? Or, can anyone suggest an alternative means of adding a shaded polygon defined by lower and upper as a layer for the ECDF graph?
source share