gpt4 book ai didi

r - 向密度图添加百分位线

转载 作者:行者123 更新时间:2023-12-04 16:29:23 27 4
gpt4 key购买 nike

这个问题在这里已经有了答案:





Shading a kernel density plot between two points.

(5 个回答)


8年前关闭。




我有一些数据dt = data.table(x=c(1:200),y=rnorm(200))我从使用 ggplot2 的密度图开始:

plot = ggplot(dt,aes(y)) + geom_density(aes(y=..density..))

有没有办法可以添加类似于 this 的百分位线?

如果进一步,我可以对类似于 this 的图形段(由百分位线创建)进行着色。 ,那就太好了!

最佳答案

这是一种深受 this answer 启发的可能性:

dt <- data.table(x=c(1:200),y=rnorm(200))
dens <- density(dt$y)
df <- data.frame(x=dens$x, y=dens$y)
probs <- c(0.1, 0.25, 0.5, 0.75, 0.9)
quantiles <- quantile(dt$y, prob=probs)
df$quant <- factor(findInterval(df$x,quantiles))
ggplot(df, aes(x,y)) + geom_line() + geom_ribbon(aes(ymin=0, ymax=y, fill=quant)) + scale_x_continuous(breaks=quantiles) + scale_fill_brewer(guide="none")

enter image description here

关于r - 向密度图添加百分位线,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14863744/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com