我有一个由以下R代码生成的图 - 基本上是许多直方图/条的面板.我希望每一个都添加一条垂直线,但每个方面的垂直线在它的位置上是不同的.或者,我想根据x值是否高于阈值将条纹颜色设置为红色 - 如何使用ggplot2/R对此进行绘图.
我这样生成了图表:
Histogramplot3 <- ggplot(completeFrame, aes(P_Value)) + geom_bar() + facet_wrap(~ Generation)
Run Code Online (Sandbox Code Playgroud)
在completeFrame是我的数据框架的地方,P_Value是我的x变量,而Facet Wrap Variable Generation是一个因素.
对特定示例提供帮助更容易,但是模拟一些数据,这可能会有所帮助:

#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
Run Code Online (Sandbox Code Playgroud)