heatmap.2()获得聚类之后的矩阵

在做heatmap聚类之后,常常因为一些分析,需要获取聚类后的矩阵。这里分享一种刚刚学习到的方法。

该方法基于heatmap.2的hierarchical clustering聚类。

首先产生随机数据作为演示,生成的数据如下(因为是随机生成,所以数字会不一样)

> before <- matrix(rnorm(50), 10, 5, dimnames=list(paste("g", 1:10, sep=""),paste("t", 1:5, sep="")))
> before
             t1         t2          t3         t4          t5
g1  -1.89937785 -0.2112492  0.80475998 -0.1437429 -0.88203601
g2   0.75889366  1.5513556 -1.08355333 -1.7635301 -0.65163564
g3   0.53893684 -0.3485060 -0.86417609  0.6294090  0.33726749
g4   1.42944861  1.1869920 -0.50002826  0.8134602 -2.11838364
g5   0.25707340  0.8692485 -0.47245735  2.2251189 -0.05577767
g6  -0.36162410 -1.7610345 -0.04321973 -0.8802015  0.76510606
g7   0.95574696 -1.4834578  1.82203982 -1.1817150 -0.05954665
g8   0.03605131 -0.2152805 -0.05155960  2.1196481  0.79576233
g9  -2.39660463  1.1517748  0.38779793 -1.2362860 -0.83512569
g10  0.99780002  1.3384064 -1.45549251 -0.7208040 -1.65369287

聚类:

> library(gplots)
> test <- heatmap.2(before)

获取聚类之后的矩阵

> after = before[rev(test$rowInd), test$colInd]
> after
             t5          t3         t4          t1         t2
g5  -0.05577767 -0.47245735  2.2251189  0.25707340  0.8692485
g8   0.79576233 -0.05155960  2.1196481  0.03605131 -0.2152805
g3   0.33726749 -0.86417609  0.6294090  0.53893684 -0.3485060
g4  -2.11838364 -0.50002826  0.8134602  1.42944861  1.1869920
g2  -0.65163564 -1.08355333 -1.7635301  0.75889366  1.5513556
g10 -1.65369287 -1.45549251 -0.7208040  0.99780002  1.3384064
g7  -0.05954665  1.82203982 -1.1817150  0.95574696 -1.4834578
g6   0.76510606 -0.04321973 -0.8802015 -0.36162410 -1.7610345
g1  -0.88203601  0.80475998 -0.1437429 -1.89937785 -0.2112492
g9  -0.83512569  0.38779793 -1.2362860 -2.39660463  1.1517748

发表评论

匿名网友

拖动滑块以完成验证