Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.0k views
in Technique[技术] by (71.8m points)

r - Shade (fill or color) area under density curve by quantile

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 different shading to the next 10% and so on.

This is a variant on the questions "Shading a kernel density plot between two points" and "ggplot2 shade area under density curve by group", but I want to shade each quantile (in my example, each group is a decile but the process should easily generalise to other quantiles).

I don't mind whether a solution uses ggplot2 or base graphics, and whether this is done directly from a formula (which would be really neat) or based on making a data frame first. If the latter, you may want:

delta <- 0.0001 
z.df <- data.frame(x = seq(from=-3, to=3, by=delta))
z.df$pdf <- dnorm(z.df$x)
z.df$decile <- floor(10*pnorm(z.df$x) + 1)

Note that the naive solution ggplot(z.df, aes(x = x, fill = quantile)) + geom_ribbon(aes(ymin = 0, ymax = pdf)) would fail because Aesthetics can not vary with a ribbon.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Actually aesthetics can vary with geom_ribbon(...) (or geom_area(...), which is basically the same thing), as long as you set the group aesthetic as well.

delta     <- 0.001 
quantiles <- 10
z.df     <- data.frame(x = seq(from=-3, to=3, by=delta))
z.df$pdf <- dnorm(z.df$x)
z.df$qt  <- cut(pnorm(z.df$x),breaks=quantiles,labels=F)

library(ggplot2)
ggplot(z.df,aes(x=x,y=pdf))+
  geom_area(aes(x=x,y=pdf,group=qt,fill=qt),color="black")+
  scale_fill_gradient2(midpoint=median(unique(z.df$qt)), guide="none") +
  theme_bw()

Setting quantiles <- 20 at the beginning produces this:


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...