r - Heatmap in Grid with normalized color intensity based on highest value among all heatmaps -


i trying plot number of heatmaps in grid , have heat colors based on 0 max value of heat maps, heat legend comparable among several heatmaps , have same range of heats in color legends. has done data matrices provided, here rscript have till create heatmap while restricting max value of individual heatmaps 99 percentile, required need keep. so, looking @ max value after capping each heatmaps 99 percentile. please suggest how can implement similar heat scaling.

start.time <- sys.time() options(echo=f) # if want see commands in output file setwd("./") args <- commandargs(trailingonly = true) numargs<-length(args) print(numargs-2) if(args[1] == "-h"){ writelines("\nto run : rscript readdensity_heatmap.r <base name of tag dir folder> <sample 1> <sample 2>") writelines("e.g rscript readdensity_heatmap.r peaks <datamatrix1 datamatrix2 .......\n") stop("", call.=f) writelines("\n") }else if(numargs < 3){ stop("please supply 3 arguments") } library(pheatmap) library(gplots) library(reshape2) library(fields) z=3 (i in 1:as.integer(args[2])){ eqn<-paste("cond", i, '<-colsplit("', args[z], '", "_",', 'c("a", "b" ))', sep="" ) eval(parse(text=eqn)) z=z+1 } var_names <- paste("file", 1:(numargs-1), sep="") df_names <- paste("df", 1:(numargs-1), sep="") d=1 j=3 prefix<-args[1] pdf(paste0(prefix, "_readdensityheatmap_tfs.pdf"), height=20, width=10) par(mfrow=c(10,3)) par(mar=c(3,3,3,3)) (i in 1:(numargs-2)){ var_names[d]=paste("heatmatrix/", prefix, "_", args[j], "_heatmatrix.txt", sep="") if (!file.exists(var_names[d]) ){ writelines(paste('"', var_names[d], '"', "not present\n", sep=" ")) stop(" ") } lhs<-df_names[d] rhs<-paste('read.delim("', var_names[d], '"' , ",header=t, sep='\t')", sep="") dfeqn=paste(lhs, "<-", rhs, sep="") eval(parse(text=dfeqn)) eqn<-paste0("x<-df", d) eval(parse(text=eqn)) x<-x[,-1] x<-x[order(rowmeans(x)),] quantile.range <- quantile(as.matrix(x), probs = seq(0, 1, 0.01)) palette.breaks <- seq(quantile.range["0%"], quantile.range["99%"], .001) mycol <- colorpanel(n=length(palette.breaks)-1,low="white",mid="blue",high="red") eqn<-paste0("cond<-cond", d, "$a" ) # eval(parse(text=eqn)) eqn<-paste0("condp2<-cond", d, "$b" ) # eval(parse(text=eqn)) image(as.matrix(t(x)), col=mycol,breaks=palette.breaks, xaxt="n", yaxt="n", axes=f, main=paste(cond, condp2, sep=" ") ) mtext(text=c(-2000, 0, 2000), side=1, line=0.3, at=c(0, 0.5, 1), las=1, cex=0.8) image.plot(as.matrix(t(x)), col=mycol,breaks=palette.breaks,legend.only=t ) d=d+1 j=j+1 } ######################## dev.off() end.time <- sys.time() time.taken <- end.time-start.time time.taken 

edit: samples file. 50 columns (out of 400) around center of peak , 10 rows several thousand. several such files read based on first, second , third onwards arguments provided script. first argument provides base name of file, 2nd tells how many files read names follow second arguments (these can many user want supply)

-270    -260    -250    -240    -230    -220    -210    -200    -190    -180    -170    -160    -150    -140    -130    -120    -110    -100    -90 -80 -70 -60 -50 -40 -30 -20 -10 0   10  20  30  40  50  60  70  80  90  100 110 120 130 140 150 160 170 180 190 200 210 220 230 4.47767857142857    4.47767857142857    4.47767857142857    4.21428571428571    4.47767857142857    4.21428571428571    5.26785714285714    5.26785714285714    4.74107142857143    4.74107142857143    5.53125 6.05803571428571    6.05803571428571    6.05803571428571    5.26785714285714    5.53125 6.58482142857143    6.84821428571429    6.84821428571429    7.11160714285714    7.11160714285714    6.84821428571429    7.11160714285714    7.63839285714286    7.63839285714286    8.42857142857143    9.21875 8.95535714285714    9.21875 8.95535714285714    8.16517857142857    7.90178571428571    8.69196428571429    8.16517857142857    7.63839285714286    7.11160714285714    7.375   7.375   7.11160714285714    6.32142857142857    6.05803571428572    5.79464285714286    5.53125 5.26785714285715    4.47767857142857    3.95089285714286    3.42410714285715    3.42410714285715    2.89732142857143    2.37053571428572    1.84375 5.53125 5.79464285714286    6.05803571428571    6.32142857142857    6.58482142857143    6.58482142857143    6.84821428571428    7.90178571428571    7.63839285714286    7.90178571428571    7.63839285714286    7.63839285714286    8.16517857142857    8.95535714285714    8.42857142857143    8.42857142857143    7.90178571428571    7.90178571428571    7.63839285714286    7.63839285714286    7.63839285714286    7.63839285714286    7.11160714285714    6.05803571428571    5.79464285714286    5.79464285714286    5.53125 5.26785714285714    5.26785714285714    5.00446428571429    4.47767857142857    4.21428571428571    5.00446428571429    5.00446428571429    5.00446428571429    4.47767857142857    3.95089285714286    4.47767857142857    4.74107142857143    4.47767857142857    4.74107142857143    4.74107142857143    4.21428571428571    3.6875  3.6875  3.6875  3.6875  3.6875  3.6875  3.6875  3.6875 0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0.263392857142857   0.263392857142857   0.263392857142857   0.263392857142857   0.263392857142857   0.263392857142857   0.263392857142857   0.263392857142857   0.263392857142857   0.263392857142857   0.263392857142857   0.263392857142857   0.263392857142857   0.263392857142857   0.263392857142857   0.263392857142857   0.263392857142857   0.263392857142857   0.263392857142857 1.58035714285714    2.37053571428571    2.89732142857143    2.63392857142857    3.95089285714286    4.21428571428571    4.47767857142857    4.47767857142857    4.47767857142857    4.47767857142857    4.74107142857143    4.74107142857143    5.00446428571428    5.26785714285714    5.79464285714286    6.05803571428571    6.05803571428571    7.11160714285714    7.375   7.375   7.63839285714285    7.90178571428571    8.16517857142857    8.42857142857143    8.42857142857143    7.375   7.63839285714285    7.375   6.32142857142857    5.79464285714285    6.32142857142857    6.32142857142857    6.58482142857143    6.32142857142857    6.32142857142857    6.58482142857143    6.05803571428571    5.79464285714285    5.53125 5.26785714285714    4.74107142857143    4.47767857142857    4.21428571428571    3.6875  3.42410714285714    2.89732142857143    2.37053571428571    2.37053571428571    2.37053571428571    1.84375 2.10714285714286 0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0 0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0 3.95089285714286    4.47767857142857    4.21428571428571    4.74107142857143    4.74107142857143    4.74107142857143    5.00446428571429    5.26785714285714    5.26785714285714    5.53125 5.53125 5.79464285714286    6.05803571428571    6.32142857142857    6.05803571428571    7.11160714285714    7.375   7.90178571428571    7.63839285714286    7.63839285714286    8.16517857142857    9.21875 10.0089285714286    9.74553571428571    9.74553571428571    9.74553571428571    9.48214285714285    9.48214285714285    9.74553571428571    9.48214285714285    9.74553571428571    9.48214285714285    9.48214285714285    9.21875 9.74553571428571    9.48214285714285    9.74553571428571    9.48214285714285    8.69196428571428    8.16517857142857    7.90178571428571    7.63839285714285    7.11160714285714    7.11160714285714    6.84821428571428    6.05803571428571    6.05803571428571    6.32142857142857    6.05803571428571    5.79464285714285    5.79464285714285 0   0.526785714285714   0.526785714285714   0.526785714285714   0.526785714285714   0.526785714285714   0.526785714285714   0.526785714285714   0.526785714285714   0.526785714285714   0.526785714285714   0.526785714285714   0.526785714285714   0.526785714285714   0.526785714285714   0.526785714285714   0.526785714285714   0.526785714285714   0.526785714285714   0.526785714285714   0.526785714285714   0.526785714285714   0.526785714285714   0.526785714285714   0.263392857142857   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0 0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0