Suppose e.g. I want to shade the area under the density curve for the standard normal distribution by decile. I want the left-most 10% of the area to have a differ
Something that works and could generalise:
require(ggplot2)
g <- ggplot(z.df, aes(x=x, y=pdf, fill=decile)) +
scale_fill_gradient2(midpoint=5.5, guide="none") +
theme_bw()
for(n in 1:10) {
g <- g + geom_ribbon(data=z.df[z.df$decile == n,], aes(ymin=0, ymax=pdf), colour = "black")
}
print(g)
I don't find this particularly satisfactory since (1) I have to add a ribbon for each decile, and (2) if I'm using a for
loop in R I'm usually doing something wrong.
But the plot it gives is reasonable: