2017-04-13 19 views
0

我已經有了不同尺度的R圖的步驟圖,我想把它們放在同一個圖上,這樣我就可以對它們進行比較,是否有一個功能可以輕鬆完成,以便我不必更改代碼? 與直方圖相同的問題,一般情節。 例如,我的1個情節代碼與GGPLOT2包以下:已經做了幾個地塊與R相同的圖表?

ggplot(e,aes(a,b/max(b)*100)) 
    +geom_step(lwd=1.2,direction="hv",colour="black") 
    + annotate(geom="rect", xmin  =0,xmax=160/60,ymin=0,ymax=100,alpha=.4,fill="green") 
    +annotate(geom="rect", xmin =160/60,xmax=779/60+160  /60,ymin=0,ymax=100,alpha=.4,fill="blue") 
    +ggtitle("Départ HTA MARCEAU DR 8A 25/11/2016") 
    +ylab("%Clients coupés")+xlab("Durées d'interruptions (en h)") 
    +annotate("text", x=c(1.5,10),y=c(70,80),  label=c("Localisation","Dépannage")) 
    + geom_vline(xintercept = (0/3600),col = c("red")) 
    +geom_vline(xintercept = (2475/3600),col = c("blue"),show.legend=TRUE) 
+geom_vline(xintercept = (939/60+2475/3600+13/60)) 

我可能會在那裏過添加XLIM。

謝謝。

+0

使用'dput添加重複的例子( )'左右。但看看'facet_wrap()'。 – Jimbou

+0

請參閱http://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example瞭解如何提供可重現的數據。對於數據可視化問題,一個圖或示例的模型將幫助我們理解您試圖實現的是什麼。 –

回答

0

只要給一些名字每一個情節,然後用grid.arrange()功能如下:

p1<-ggplot(e,aes(a,b/max(b)*100)) 
    +geom_step(lwd=1.2,direction="hv",colour="black") 
    + annotate(geom="rect", xmin 

    =0,xmax=160/60,ymin=0,ymax=100,alpha=.4,fill="green") 
     +annotate(geom="rect", xmin =160/60,xmax=779/60+160  /60,ymin=0,ymax=100,alpha=.4,fill="blue") 
     +ggtitle("Départ HTA MARCEAU DR 8A 25/11/2016") 
     +ylab("%Clients coupés")+xlab("Durées d'interruptions (en h)") 
     +annotate("text", x=c(1.5,10),y=c(70,80),  label=c("Localisation","Dépannage")) 
     + geom_vline(xintercept = (0/3600),col = c("red")) 
     +geom_vline(xintercept = (2475/3600),col = c("blue"),show.legend=TRUE) 
    +geom_vline(xintercept = (939/60+2475/3600+13/60)) 

Similarly define p2,p3,p4... as many plots you have 

Then use following: 

    library(gridExtra) 
    grid.arrange(p1,p2,p3,p4,ncol=2) 

更新

ggplot() #define first plot 
     +geom_step(e,aes(a,b/max(b)*100),lwd=1.2,direction="hv",colour="black") 
     + annotate(geom="rect", xmin  =0,xmax=160/60,ymin=0,ymax=100,alpha=.4,fill="green") 
     +annotate(geom="rect", xmin =160/60,xmax=779/60+160  /60,ymin=0,ymax=100,alpha=.4,fill="blue") 
     +ggtitle("Départ HTA MARCEAU DR 8A 25/11/2016") 
     +ylab("%Clients coupés")+xlab("Durées d'interruptions (en h)") 
     +annotate("text", x=c(1.5,10),y=c(70,80),  label=c("Localisation","Dépannage")) 
     + geom_vline(xintercept = (0/3600),col = c("red")) 
     +geom_vline(xintercept = (2475/3600),col = c("blue"),show.legend=TRUE) 
    +geom_vline(xintercept = (939/60+2475/3600+13/60)) 

#2nd plot 
+geom_step(e,aes(a,b/max(b)*100),lwd=1.2,direction="hv",colour="black") 
     + annotate(geom="rect", xmin  =0,xmax=160/60,ymin=0,ymax=100,alpha=.4,fill="green") 
     +annotate(geom="rect", xmin =160/60,xmax=779/60+160  /60,ymin=0,ymax=100,alpha=.4,fill="blue") 
     +ggtitle("Départ HTA MARCEAU DR 8A 25/11/2016") 
     +ylab("%Clients coupés")+xlab("Durées d'interruptions (en h)") 
     +annotate("text", x=c(1.5,10),y=c(70,80),  label=c("Localisation","Dépannage")) 
     + geom_vline(xintercept = (0/3600),col = c("red")) 
     +geom_vline(xintercept = (2475/3600),col = c("blue"),show.legend=TRUE) 
    +geom_vline(xintercept = (939/60+2475/3600+13/60)) 

等..

+0

非常感謝,這也是非常有用的。 但我覺得我還不夠清楚,我正在尋找的東西是在同一塊石頭上放置幾塊地塊,而不是頁面。 我已經有這些情節,並希望在同一個x軸上進行比較。 – Sama

+0

因爲我想用圖形來比較每個plot的geom_vline。 – Sama

+0

@Sama看看更新後的代碼是否按照你的要求工作。 –

相關問題