已经完成了几个与R放在同一图表上的情节?

时间:2017-04-13 09:42:01

标签: r graph ggplot2

我已经有不同尺度的R的步骤图,我想将它们全部放在同一个图表上以便我可以比较它们,是否有一个功能可以很容易地做到这一点,所以我不会必须改变代码? 直方图,一般情节也是如此。 例如,我的1个绘图的代码如下是ggplot2包:

 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。

感谢。

1 个答案:

答案 0 :(得分: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))

依旧......