我试图在网格中绘制一些热图,并希望根据所有热图的0到最大值来获得热颜色,这样热图可以在几个热图之间进行比较,并具有相同的热量范围 . 颜色传说 . 由于必须为所提供的任何数据矩阵完成,这里是Rscript,我到目前为止创建热图,同时还将单个热图的最大值限制为99百分位,这是我需要保留的另一个要求 . 因此,在将每个热图限制为99百分位后,我最终会查看最大值 . 请建议我如何实现类似的热量缩放 .

start.time <- Sys.time()
options(echo=F) # if you 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
for (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))
for (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

编辑:样本文件 . 在峰值中心周围只有50根柱子(400个),而在数千个柱子中只有10列 . 基于提供给脚本的第一,第二和第三个参数,读取几个这样的文件 . 第一个参数提供文件的基本名称,2nd表示从第二个参数后面的名称中读取的文件数(这些文件可以是用户想要提供的数量)

-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