1

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))

enter image description here

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

Community
  • 1
  • 1
user1617979
  • 2,370
  • 3
  • 25
  • 30
  • Do you have an example of the desired output? What will this "shaded region" look like exactly? – MrFlick Oct 13 '14 at 18:32
  • Would this help? http://stackoverflow.com/questions/3494593/shading-a-kernel-density-plot-between-two-points – Roman Luštrik Oct 13 '14 at 18:42
  • MrFlick, I do not have an example as I have no way to generate it - but on the lower chart the area between the 2 middle dashed lines (from roughly -0.2 to roughly .7) would be a gray transparent rectangle (I may end up drawing it first so the histogram is above) – user1617979 Oct 13 '14 at 20:52
  • Roman, thanks but I am not wish to draw the area under a curve - but your sugestion make sme think but maybe I can shade those bins differently which is analogous – user1617979 Oct 13 '14 at 20:53

1 Answers1

-3

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

enter image description here

user1617979
  • 2,370
  • 3
  • 25
  • 30
  • 1
    Besides the lacking readability your code is faulty and does not -- even after fixing errors -- produce the shown plot. You should correct it. – jay.sf Nov 19 '17 at 11:35