将百分比线添加到密度图 [英] adding percentile lines to a density plot

查看:177
本文介绍了将百分比线添加到密度图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一些数据 dt = data.table(x = c(1:200),y = rnorm(200)) ggplot2

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

有没有一种方法可以添加与 类似的百分比线条?

Is there a way I can add percentile lines similar to this?

如果进一步我可以遮蔽图表的分段(由百分位线创建),类似于 this ,那就太好了!

If further I could shade the segments of the graph (created by the percentile lines) similar to this, then that would be great!

推荐答案

以下是一个很可能受到此答案启发的可能性:

Here is a possibility heavily inspired by 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")

这篇关于将百分比线添加到密度图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆