简体   繁体   English

pheatmap:使用从最大到最小的渐变为矩阵的每一行独立着色

[英]pheatmap: color each row of a matrix independently using a gradient from max to min

I am trying to show the colors for each row in my matrix (min to max gradient):我正在尝试显示矩阵中每一行的颜色(最小到最大梯度):

 [1,]  8 12   11  5   24
 [2,]  1  2    7  0    8
 [3,] 53 99 1501 15 9859
 [4,] 59 24   19 19   32
 [5,]  4  2   11  0   68
 [6,]  4  9  177  2  710
 [7,]  2  1    2  2    3
 [8,]  0  5  133  0 2195
 [9,]  3  3    2  1   15
[10,]  0  1    0  0   14
[11,]  0  3   21  0   17
[12,]  2  1    2  0    6
[13,] 11 26   22  3   16
[14,]  6 38  217  1  354
[15,]  3 10   17  0   68
[16,]  3  3   12  2   19
[17,]  7  5   26  1   40
[18,]  1  0    6  0   27
[19,]  1  0   37  0  434
[20,] 30 15   20  9   27

My code is as follows:我的代码如下:

RT<-read.table("tmp",header=FALSE,sep="\t");
mat <-data.frame(RT)
pheatmap(mat, scale = "row",cluster_cols=FALSE,cluster_rows = FALSE, row.names =FALSE);
dev.off()

I have received the following image:我收到了以下图片: 在此处输入图片说明

As you can see in the 2nd row the max needs to be red but it is orange because column 3 and 5 of the 2nd row have similar values.正如您在第二行中看到的那样,最大值需要为红色,但它是橙色的,因为第二行的第 3 列和第 5 列具有相似的值。 I want to do max is red, min is blue, and gradient between red,yellow and blue, for the values between max and min for each row independently.对于每行的最大值和最小值之间的值,我想做最大值为红色,最小值为蓝色,以及红色、黄色和蓝色之间的渐变。 0's always be blue. 0 永远是蓝色的。 Only identical values should get the same color.只有相同的值才能获得相同的颜色。 I have tried other solutions from R: Row scaling not working correctly for heatmap but that didn't work for me.我已经尝试过R 的其他解决方案:行缩放对热图不起作用,但这对我不起作用。 Please help.请帮忙。

I tried a few things and I think I may have an answer that is close:我尝试了几件事,我想我可能有一个接近的答案:

A) divide each row with its max. A)将每一行与其最大值分开。

 xx <- apply(mat,1, function(i) i/max(i));

B) minimum is always 0. So, I converted the minimum of each row to 0. B) 最小值始终为 0。因此,我将每行的最小值转换为 0。

xx1<-t(apply(xx, 1, function(x) replace(x, x== min(x), 0.0)))
pheatmap(xx1, scale = "none",cluster_cols=FALSE,cluster_rows = FALSE, row.names =FALSE);
dev.off()

This seems to be working but it can be done better.这似乎有效,但可以做得更好。 在此处输入图片说明

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM