2013-11-25 29 views
-1

我有一個由以下R代碼生成的圖 - 基本上是一個包含許多直方圖/小節的面板。對每一個我想添加一條垂直線,但每個方面的垂直線在它的位置是不同的。可替換地,我想杆變成紅色取決於x的值是否大於閾值高到彩色 - 如何做到這一點與GGPLOT2這樣的情節/ R.將geom_vlines添加到 - 或顏色 - 小平面包裝圖

我生成的圖表像這樣:

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

其中,completeFrame是我的數據框,P_Value是我的x變量,Facet Wrap變量生成是一個因素。

+0

人一般更樂意幫助,如果你提供一個[_minimal_,重複的例子, ](http://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example/5963610#5963610)並顯示你已經嘗試過的代碼。謝謝! – Henrik

回答

5

它更容易幫助具體的例子,但模擬了一些數據,也許這將幫助:

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 
相關問題