File: Rplot_hist.R

package info (click to toggle)
simgrid 4.1-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 39,192 kB
  • sloc: cpp: 124,913; ansic: 66,744; python: 8,560; java: 6,773; fortran: 6,079; f90: 5,123; xml: 4,587; sh: 2,194; perl: 1,436; makefile: 111; lisp: 49; javascript: 7; sed: 6
file content (71 lines) | stat: -rw-r--r-- 2,665 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
# R script showing .pdf file with plots of all injection histograms for a certain file
# Can be called from the command line with: Rscript $this_script.R inputfile

# Necessary libraries
library(plyr)
library(ggplot2)
library(data.table)
library(grid)

# Functions for arranging multiple plots
vp.layout <- function(x, y) viewport(layout.pos.row=x, layout.pos.col=y)
arrange_ggplot2 <- function(list, nrow=NULL, ncol=NULL, as.table=FALSE) {
  n <- length(list)
  if(is.null(nrow) & is.null(ncol)) { nrow = floor(n/2) ; ncol = ceiling(n/nrow)}
  if(is.null(nrow)) { nrow = ceiling(n/ncol)}
  if(is.null(ncol)) { ncol = ceiling(n/nrow)}
  ## NOTE see n2mfrow in grDevices for possible alternative
  grid.newpage()
  pushViewport(viewport(layout=grid.layout(nrow,ncol) ) )
  ii.p <- 1
  for(ii.row in seq(1, nrow)){
    ii.table.row <- ii.row
    if(as.table) {ii.table.row <- nrow - ii.table.row + 1}
    for(ii.col in seq(1, ncol)){
      ii.table <- ii.p
      if(ii.p > n) break
      print(list[[ii.table]], vp=vp.layout(ii.table.row, ii.col))
      ii.p <- ii.p + 1
    }
  }
}

### Main
# Reading command line argument with the input file path
args <- commandArgs(trailingOnly = TRUE)
fp  <- file(args[1], open = "r")

plots<-list()
i<-1

# Reading histograms one by one, line by line
while (length(oneLine <- readLines(fp, n = 1, warn = FALSE)) > 0){
  myVector <- (strsplit(oneLine, "\t"))

  dfl <- ldply (myVector, data.frame)

  name<-as.character(dfl[1,])
  nbins<-as.numeric(as.character(dfl[4,]))
  allbreaks<-as.numeric(as.character(dfl[5:(5+nbins-1),]))

  dh<-data.frame(Name=as.character(dfl[1,]), Total=as.numeric(as.character(dfl[2,])),
                 Mean=as.numeric(as.character(dfl[3,])), Nbins=as.numeric(as.character(dfl[4,])))
  dh<-cbind(dh,Bstart=allbreaks[-length(allbreaks)])
  dh<-cbind(dh,Bend=allbreaks[-1])
  dh<-cbind(dh,Density=as.numeric(as.character(dfl[(5+nbins):(5+nbins*2-2),])))

  # Plotting single histogram, if it only has one value then use geom_bar
  if (nbins > 2)
    plots[[i]]<-ggplot(data=data.frame(dh), aes(xmin=Bstart, xmax=Bend, ymin=0, ymax=Density)) +
        geom_rect(aes(fill=Density)) + theme_bw() + scale_x_continuous("Time [s]", allbreaks) +
            labs(title=name, y=element_text("Density %"))
  else
    plots[[i]]<-ggplot(data=data.frame(dh), aes(factor(Bstart))) + geom_bar(aes(fill=Density)) +
        theme_bw() + labs(title=name, y=element_text("Density %"), x=element_text("Time [s]"))
  i<-i+1
}

# Printing all plots together in a table
arrange_ggplot2(plots, as.table=TRUE)

write("Done producing a histogram plot. Open Rplots.pdf located in this folder to see the results", stdout())