-1

I have a plot generated by the following R code - basically a panel of many histograms/bars. and to each one I'd like to add a vertical line, but the vertical line for each facet is different in it's position. Alternatively I'd like to colour the bars red depending on whether the x value is higher than a threshold - how do I do this to such a plot with ggplot2 / R.

I generated the chart like so:

Histogramplot3 <- ggplot(completeFrame, aes(P_Value)) + geom_bar() + facet_wrap(~ Generation)

Where completeFrame is my dataframe, P_Value is my x variable, and the Facet Wrap Variable Generation is a factor.

SJWard
  • 3,629
  • 5
  • 39
  • 54
  • People are generally much more happy to help if you provide a [_minimal_, reproducible example](http://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example/5963610#5963610) and show the code you already have tried. Thanks! – Henrik Nov 25 '13 at 16:38

1 Answers1

5

It's easier to help with specific examples, but simulating some data, maybe this will help:

enter image description here

#simulate data
completeFrame<-data.frame(P_Value=rnorm(200,0.8,0.1),Generation=rep(1:4,times=50))

#draw the basic plot
h3 <- qplot(data=completeFrame,x=P_Value,geom="blank") + 
  geom_bar(binwidth=0.02, col="black", fill="black") + 
# overlay the "red" bars for the subset of data
  geom_bar(data=completeFrame[which(completeFrame$P_Value>0.8),],binwidth=0.02, col="black", fill="red") +
  facet_wrap(~ Generation)

#add lines to the subsets
h3 <-     h3+geom_hline(data=completeFrame[which(completeFrame$Generation==2),],aes(yintercept=max(P_Value)))
h3 <- h3+geom_hline(data=completeFrame[which(completeFrame$Generation==1),],aes(yintercept=2.5))
h3 <- h3+geom_hline(data=completeFrame[which(completeFrame$Generation==3),],aes(yintercept=mean(P_Value)))

h3
Troy
  • 8,581
  • 29
  • 32