简体   繁体   English

如何从 R 中的矩阵创建精确对应的热图

[英]How to create an exact corresponding heatmap from a matrix in R

I am producing a random matrix using the following code:我正在使用以下代码生成一个随机矩阵:

a <- round(matrix(c(runif(100, 10, 50)), byrow = T, ncol = 10), 2) ; a

I want to create a heatmap that corresponds to this matrix.我想创建一个对应于这个矩阵的热图。 So I used the following code:所以我使用了以下代码:

makeColorRampPalette <- function(colors, cutoff.fraction, num.colors.in.palette) {
          stopifnot(length(colors) == 4)
          ramp1 <- colorRampPalette(colors[1:2])(num.colors.in.palette * cutoff.fraction)
          ramp2 <- colorRampPalette(colors[3:4])(num.colors.in.palette * (1 - cutoff.fraction))
          return(c(ramp1, ramp2))
         }
cutoff.distance <- 30  
cols <- makeColorRampPalette(c("white", "yellow", "yellow", "red"), cutoff.distance / max(b), 100)

library(pheatmap)
pheatmap(a, treeheight_row = 0, treeheight_col = 0, color = cols, display_numbers = T)

However, the values of the heatmap are not in the same order as the ones in the matrix.但是,热图的值与矩阵中的值的顺序不同。 The matrix produced was this:产生的矩阵是这样的:

       [,1]  [,2]  [,3]  [,4]  [,5]  [,6]  [,7]  [,8]  [,9] [,10]
 [1,] 30.84 35.73 27.19 31.20 40.00 30.18 40.79 45.87 12.78 46.18
 [2,] 43.11 29.70 35.56 45.72 43.70 31.33 34.07 36.88 16.08 27.11
 [3,] 32.48 19.54 21.93 39.89 23.09 13.56 10.42 38.54 29.21 32.47
 [4,] 21.09 41.88 41.21 24.49 24.45 45.23 21.41 44.73 33.96 32.85
 [5,] 39.46 24.24 18.76 36.27 18.54 21.99 22.76 39.49 21.77 23.82
 [6,] 37.28 40.39 30.66 26.30 47.54 14.26 19.26 36.07 24.42 10.02
 [7,] 36.06 10.30 37.00 33.50 33.60 26.81 27.41 33.79 10.90 37.28
 [8,] 37.98 31.93 38.70 31.24 43.76 32.37 35.47 37.14 30.99 49.50
 [9,] 37.85 42.69 33.31 28.46 34.07 18.91 43.74 29.16 41.35 28.63
[10,] 10.75 38.09 31.11 44.74 15.77 32.78 20.02 33.39 19.49 23.87

But the heatmap with the values is the following one and as you can see the values are mixed up and not as in the matrix:但是带有值的热图如下所示,正如您所看到的,这些值是混合在一起的,而不是矩阵中的:

在此处输入图像描述

How can I create an exact corresponding heatmap from a matrix?如何从矩阵创建精确对应的热图? Any ggplot2 solutions are welcome too.也欢迎任何 ggplot2 解决方案。

You might want to try superheat pacakge in R您可能想在 R 中尝试过热包

library(superheat)
library(RColorBrewer)
pal <- brewer.pal(10, "RdYlGn")

a <- round(matrix(c(runif(100, 10, 50)), byrow = T, ncol = 10), 2) ; a
a
       [,1]  [,2]  [,3]  [,4]  [,5]  [,6]  [,7]  [,8]  [,9] [,10]
 [1,] 35.85 19.23 35.66 43.25 31.02 17.63 38.37 18.95 29.32 13.25
 [2,] 37.39 11.72 21.61 46.24 30.48 38.34 32.49 22.96 39.50 42.44
 [3,] 45.69 11.03 17.50 33.84 15.76 14.36 34.65 22.14 11.16 24.36
 [4,] 37.74 22.92 14.06 16.83 38.67 34.20 32.39 37.95 35.73 33.75
 [5,] 35.14 12.46 21.07 16.83 15.59 28.46 13.79 36.93 44.71 19.53
 [6,] 34.50 20.11 38.21 18.36 39.13 31.74 37.17 14.69 14.11 45.88
 [7,] 14.11 31.96 18.20 21.35 46.27 21.30 40.11 46.75 31.54 21.23
 [8,] 46.69 14.54 42.98 10.91 37.88 10.88 33.04 38.62 16.69 44.17
 [9,] 39.30 22.38 27.23 11.25 11.77 27.49 39.49 30.37 29.66 47.50
[10,] 49.15 42.44 13.92 43.34 28.68 13.47 45.34 22.09 38.02 10.31

superheat(X = a, X.text = a,
          heat.pal = pal,
          bottom.label.text.size = 2,
          smooth.heat = TRUE, pretty.order.cols = FALSE, pretty.order.rows = FALSE)

热图

If by 'exact corresponding heatmap from a matrix' you mean generating a coloured image where the colour of each element corresponds to the value in the matrix, you can simply use:如果“来自矩阵的精确对应热图”是指生成彩色图像,其中每个元素的颜色对应于矩阵中的值,则可以简单地使用:

image(a)

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

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