0

I already have step plots with R on different scales, and I would like to put them all on the same graph so I can compare them , is there a function that can do that easily enough so that I don't have to change the codes ? Same problem with histograms, plots in general. For example, my code for 1 plot is the following with ggplot2 package :

 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))

I will probably add an xlim in there too.

Thanks.

Axeman
  • 32,068
  • 8
  • 81
  • 94
Sama
  • 3
  • 1
  • add a reproducible example using `dput()` or so. but look at `facet_wrap()`. – Roman Apr 13 '17 at 09:43
  • See http://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example for how to provide reproducible data. For data visualisation question, a mockup of a figure or example would help a lot in helping us understand what is it you are trying to achieve. – Adam Quek Apr 13 '17 at 09:45

1 Answers1

0

Just give some name to every plot and then use grid.arrange() function as follows:

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)

updated

    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))

and so on..

Ishan Juneja
  • 401
  • 4
  • 11
  • Thank you very much this is very useful too. But I think I wasn't clear enough, the thing I'm looking for is to put together several plots on the same graphe, not page. I have these plots already and would like to compare them on the same x axis. – Sama Apr 13 '17 at 11:29
  • Because I want to compare those geom_vline of each plot graphically. – Sama Apr 13 '17 at 11:36
  • @Sama See if the updated code works as per your requirement. – Ishan Juneja Apr 14 '17 at 05:08
  • Thanks for the idea, but this doesn't work cause the main function is ggplot() and it requires a dataframe, the thing i'm trying to achieve is to plot different dataframes with geom_step for example on the SAME graph. I tried the ggplot()+geom_step(a,aes(..))+geom_step(b,aes(..))+... trick but I get the message : "ggplot2 doesn't know how to deal with data of class uneval". Maybe I need to use rbind or something like that. Thanks for your help. – Sama Apr 16 '17 at 17:20
  • Sorry my mistake, this works perfectly that's exactly what I was looking for, this ggplot is just great ! @IshanJuneja Thank you ! ANother thing I would like to do if possible, is to show the values of those geom_vline on the x axis, is there an option ? Cause I didn't find it.. – Sama Apr 16 '17 at 20:31
  • @Sama for that plz provide some info on your input dataset, so that a reproducible example can be used – Ishan Juneja Apr 17 '17 at 17:14