将阴影区域添加到五分位数之间的直方图中 [英] Adding Shaded region to histogram between quintiles

查看:95
本文介绍了将阴影区域添加到五分位数之间的直方图中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所有,

我有一个图表有2个直方图,其中我也绘制了表示第20,40,60和80百分位的线条,下面的代码重现了与虚拟数据相似的图表

  data <-rbind(data.frame(x = rnorm(1000,0,1), g =one),data.frame(x = rnorm(1000,0.2,1.5),g =two))
q1 = melt(ddply(melt(data,id.vars =g ),(g)总结,q20 =分位数(值.2,na.rm = T),
q40 =分位数(值,0.4,na.rm = T),q60 =分位数,.6,na.rm = T),q80 =分位数(值,.8,na.rm = T)))
ggplot(data,aes(x = x,fill = g))+
geom_vline(data = q1,aes(xintercept = value,group = variable),linetype = 2,color =black)+
#stat_bin(aes(y = .. count ../ sum(。 .count ..)),binwidth = 0.1,alpha = 0.4,geom =bar)+
geom_histogram(aes(y = .. count ../ sum(.. count ..)),alpha = 0.75,binwidth = 0.1)+ facet_grid(g〜。)+ theme_bw()+ coord_cartesian(xlim = c(-3,3))



我想在第40行和第60行之间添加阴影区域(可能是灰色)以显示中间五分位数 - 我希望该区域是数据驱动的(我正在使用dataframe q1这是数据派生的,这是可以接受的,我只是不想手动输入值)

这怎么能完成?我一直无法做到这一点



感谢您的所有帮助

解决方案

经过更多的实验后,我可以使用下面的代码来解决它:



不知何故,欺骗geom_rect的原因是它需要一个x变量(所以我将q40改名为q)。我不确定为什么ggplot不喜欢xmin = q40,但它喜欢xmax = q60

  data <-rbind(data .frame(x = rnorm(1000,0,1),g =one),data.frame(x = rnorm(1000,0.2,1.5),g =two))
q = ddply (数据,id.vars =g),(g),总结,q20 =分位数(值,.2,na.rm = T),
q40 =分位数(值,.4,na .rm = T),q60 =分位数(值,.6,na.rm = T),q80 =分位数(值,.8,na.rm = T))
q1 = melt(q)
姓名(q)[3] ='x'

ch < - ggplot(data,aes(x = x,fill = g))+
geom_vline(data = q1 ,aes(xintercept = value,group = variable),linetype = 2,color =black)+
geom_histogram(aes(y = .. density ..),alpha = 0.75,binwidth = 0.1)+ facet_grid (g〜。)+ theme_bw()+ coord_cartesian(xlim = c(-3,3))+
coord_cartesian(ylim = c(0,0.5))++ geom_rect(data = q,aes(xmin = x,xmax = x1,ymin = 0,ymax = 0.5,group = g),fill =gray,alpha = 0.1)
ch


All,

I have a chart that has 2 histograms in which I also plotted lines representing the 20th, 40th, 60th and 80th percentiles, the code below reproduces a similar chart with dummy data

data <- rbind(data.frame(x=rnorm(1000,0,1),g="one"),data.frame(x=rnorm(1000,0.2,1.5),g="two"))
q1 = melt(ddply(melt(data,id.vars="g"),.(g),summarise,q20=quantile(value,.2,na.rm=T),
           q40=quantile(value,.4,na.rm=T),q60=quantile(value,.6,na.rm=T),q80=quantile(value,.8,na.rm=T)))
ggplot(data,aes(x=x,fill=g))+
  geom_vline(data=q1,aes(xintercept=value,group=variable),linetype=2,color="black")+
  #stat_bin(aes(y=..count../sum(..count..)),binwidth=0.1,alpha=0.4,geom="bar")+
  geom_histogram(aes(y=..count../sum(..count..)),alpha=0.75,binwidth=0.1)+facet_grid(g~.)+theme_bw()+coord_cartesian(xlim=c(-3,3))

I want to add a shaded region (maybe in gray) between the 40 and 60th lines to show the middle quintile - I would like for the region to be data driven (I am using dataframe q1 which is data derived and that is acceptable, I just do not want to have to enter value manually)

How can this be accomplished? I have not been able to do it

thanks for all the help

解决方案

After more experimentation, I was able to solve it using the following code

What was tricking geom_rect somehow was that it wanted an x variable (so I renamed q40 on q). I am not sure why ggplot does not like xmin=q40, but it likes xmax=q60

data <- rbind(data.frame(x=rnorm(1000,0,1),g="one"),data.frame(x=rnorm(1000,0.2,1.5),g="two"))
q = ddply(melt(data,id.vars="g"),.(g),summarise,q20=quantile(value,.2,na.rm=T),
          q40=quantile(value,.4,na.rm=T),q60=quantile(value,.6,na.rm=T),q80=quantile(value,.8,na.rm=T))
q1 = melt(q)
names(q)[3] = 'x'

ch <- ggplot(data,aes(x=x,fill=g))+
  geom_vline(data=q1,aes(xintercept=value,group=variable),linetype=2,color="black")+
  geom_histogram(aes(y=..density..),alpha=0.75,binwidth=0.1)+facet_grid(g~.)+theme_bw()+coord_cartesian(xlim=c(-3,3))+
  coord_cartesian(ylim=c(0,0.5))++geom_rect(data=q,aes(xmin=x,xmax=x1,ymin=0,ymax=.5,group=g),fill="gray",alpha=0.1)
ch

这篇关于将阴影区域添加到五分位数之间的直方图中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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