Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Merge branch 'master' of git+ssh://scm.gforge.inria.fr//gitroot/simgrid/simgrid
[simgrid.git] / contrib / benchmarking_code_block / Rplot_hist.R
1 # R script showing .pdf file with plots of all injection histograms for a certain file
2 # Can be called from the command line with: Rscript $this_script.R inputfile
3
4 # Necessary libraries
5 library(plyr)
6 library(ggplot2)
7 library(data.table)
8 library(grid)
9
10 # Functions for arranging multiple plots
11 vp.layout <- function(x, y) viewport(layout.pos.row=x, layout.pos.col=y)
12 arrange_ggplot2 <- function(list, nrow=NULL, ncol=NULL, as.table=FALSE) {
13   n <- length(list)
14   if(is.null(nrow) & is.null(ncol)) { nrow = floor(n/2) ; ncol = ceiling(n/nrow)}
15   if(is.null(nrow)) { nrow = ceiling(n/ncol)}
16   if(is.null(ncol)) { ncol = ceiling(n/nrow)}
17   ## NOTE see n2mfrow in grDevices for possible alternative
18   grid.newpage()
19   pushViewport(viewport(layout=grid.layout(nrow,ncol) ) )
20   ii.p <- 1
21   for(ii.row in seq(1, nrow)){
22     ii.table.row <- ii.row
23     if(as.table) {ii.table.row <- nrow - ii.table.row + 1}
24     for(ii.col in seq(1, ncol)){
25       ii.table <- ii.p
26       if(ii.p > n) break
27       print(list[[ii.table]], vp=vp.layout(ii.table.row, ii.col))
28       ii.p <- ii.p + 1
29     }
30   }
31 }
32
33 ### Main
34 # Reading command line argument with the input file path
35 args <- commandArgs(trailingOnly = TRUE)
36 fp  <- file(args[1], open = "r")
37
38 plots<-list()
39 i<-1
40
41 # Reading histograms one by one, line by line
42 while (length(oneLine <- readLines(fp, n = 1, warn = FALSE)) > 0){
43   myVector <- (strsplit(oneLine, "\t")) 
44   
45   dfl <- ldply (myVector, data.frame)
46
47   name<-as.character(dfl[1,])
48   nbins<-as.numeric(as.character(dfl[4,]))
49   allbreaks<-as.numeric(as.character(dfl[5:(5+nbins-1),]))
50   
51   dh<-data.frame(Name=as.character(dfl[1,]), Total=as.numeric(as.character(dfl[2,])),
52                  Mean=as.numeric(as.character(dfl[3,])), Nbins=as.numeric(as.character(dfl[4,])))
53   dh<-cbind(dh,Bstart=allbreaks[-length(allbreaks)])
54   dh<-cbind(dh,Bend=allbreaks[-1])
55   dh<-cbind(dh,Density=as.numeric(as.character(dfl[(5+nbins):(5+nbins*2-2),])))
56
57   # Plotting single histogram, if it only has one value then use geom_bar
58   if (nbins > 2)
59     plots[[i]]<-ggplot(data=data.frame(dh), aes(xmin=Bstart, xmax=Bend, ymin=0, ymax=Density)) +
60         geom_rect(aes(fill=Density)) + theme_bw() + scale_x_continuous("Time [s]", allbreaks) +
61             labs(title=name, y=element_text("Density %"))
62   else
63     plots[[i]]<-ggplot(data=data.frame(dh), aes(factor(Bstart))) + geom_bar(aes(fill=Density)) +
64         theme_bw() + labs(title=name, y=element_text("Density %"), x=element_text("Time [s]"))
65   i<-i+1
66 }
67
68 # Printing all plots together in a table
69 arrange_ggplot2(plots, as.table=TRUE)
70
71 write("Done producing a histogram plot. Open Rplots.pdf located in this folder to see the results", stdout())