密度曲线下的分位(填充或颜色)区域 [英] Shade (fill or color) area under density curve by quantile
问题描述
这是关于为两点之间的内核密度绘制阴影和 ggplot2密度曲线下的阴影面积组,但我想遮荫 each 分位数(在我的例子中,每个组都是一个十分位数,但这个过程应该很容易推广到其他分位数)。
我不介意解决方案是否使用 ggplot2
或 base
图形,以及是否直接从公式中完成)或基于首先制作数据帧。如果是后者,您可能需要:
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)
请注意,天真的解决方案 ggplot(z.df,aes(x = x,fill = quantile))+ geom_ribbon(aes(ymin = 0,ymax = pdf))会失败,因为
美学不能随着功能区而变化。
实际上,美学可以随着 geom_ribbon(...)
(或者 geom_area(...)
,这是基本相同的事情),只要您也设置了 group
审美。
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)
库(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(z.df $ qt) guide =none)+
theme_bw()
设置 quantiles < - 20
在开始处产生:
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
.
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:
这篇关于密度曲线下的分位(填充或颜色)区域的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!