将摘要信息添加到使用ggplot创建的密度图中

栏目: R语言 · 发布时间: 6年前

内容简介:翻译自:https://stackoverflow.com/questions/4542438/adding-summary-information-to-a-density-plot-created-with-ggplot

我有一个密度图,我想添加一些摘要信息,例如在中位数放置一条线并遮蔽90%可信区间(第5和第95分位数).有没有办法在ggplot中这样做?

这是我想总结的情节类型:

我可以弄清楚如何从y = 0到y =密度(中位数(x))绘制一条线,但是我不清楚我是否可以使用90%CI对阴影进行着色.或者,我可以在密度图上方添加一个水平箱图,但不清楚如何在不旋转密度图的情况下自动旋转箱图.

x <- as.vector(rnorm(10000))
d <- as.data.frame(x=x)
library(ggplot2)
ggplot(data = d) + theme_bw() + 
  geom_density(aes(x=x, y = ..density..), color = 'black')
您可以使用geom_area()函数.首先使用density()函数显示密度.
x <- as.vector(rnorm(10000))
d <- as.data.frame(x=x)
library(ggplot2)
p <- ggplot(data = d) + theme_bw() + 
  geom_density(aes(x=x, y = ..density..), color = 'black')
# new code is below
q5 <- quantile(x,.05)
q95 <- quantile(x,.95)
medx <- median(x)
x.dens <- density(x)
df.dens <- data.frame(x = x.dens$x, y = x.dens$y)
p + geom_area(data = subset(df.dens, x >= q5 & x <= q95), 
              aes(x=x,y=y), fill = 'blue') +
    geom_vline(xintercept = medx)

翻译自:https://stackoverflow.com/questions/4542438/adding-summary-information-to-a-density-plot-created-with-ggplot


以上所述就是小编给大家介绍的《将摘要信息添加到使用ggplot创建的密度图中》,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对 码农网 的支持!

查看所有标签

猜你喜欢:

本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们

Head First WordPress

Head First WordPress

Siarto Jeff / O'Reilly Media / 2010-02-15 / USD 34.99

Whether you're promoting your business or writing about your travel adventures, "Head First WordPress" will teach you not only how to make your blog look unique and attention-grabbing, but also how to......一起来看看 《Head First WordPress》 这本书的介绍吧!

JS 压缩/解压工具
JS 压缩/解压工具

在线压缩/解压 JS 代码

MD5 加密
MD5 加密

MD5 加密工具