heatmap.2- Error: Colv dendrogram doesn't match size of x
heatmap.2- Error: Colv dendrogram doesn't match size of x
我正在使用 R 生成热图。
我使用 hclust
函数并得到错误:
Colv dendrogram doesn't match size of x
。
下面是我的代码,它与方阵完美配合。现在我的输入矩阵是 400x2000
。谁能告诉我为什么会出现此错误?
data <- read.table("mydata.txt",sep="\t",header=TRUE,row.names=1)
data_matrix <- data.matrix(data)
library(gplots)
library("RColorBrewer")
colors <- colorRampPalette(rev(brewer.pal(9, "Blues")) )(255)
hc_dist= dist(data_matrix)
hc_clust= hclust(hc_dist)
hr_dist= dist(t(data_matrix))
hr_clust= hclust(hr_dist)
heatmap.2(data_matrix, col=brewer.pal(11,"RdBu"), labRow=NA, density.info="none", scale="row",trace="none",Colv=as.dendrogram(hc_clust), Rowv=as.dendrogram(hr_clust))
Error in heatmap.2(data_matrix, col = brewer.pal(11, "RdBu"), labRow = NA,:Colv dendrogram doesn't match size of x
发展我的评论:
想象一下这个矩阵:
matex<-matrix(1:50,ncol=5)
colnames(matex)<-paste0("col",1:5)
rownames(matex)<-paste0("row",1:10)
head(matex)
# col1 col2 col3 col4 col5
#row1 1 11 21 31 41
#row2 2 12 22 32 42
#row3 3 13 23 33 43
#row4 4 14 24 34 44
#row5 5 15 25 35 45
#row6 6 16 26 36 46
现在,像您一样计算距离:
hc_dist= dist(matex) # what you think are between columns distances
hr_dist= dist(t(matex)) # what you think are between rows distances
并检查对象 hc_dist
和 hr_dist
:
hc_dist
# row1 row2 row3 row4 row5 row6 row7 row8 row9
# row2 2.236068
# row3 4.472136 2.236068
# row4 6.708204 4.472136 2.236068
# row5 8.944272 6.708204 4.472136 2.236068
# row6 11.180340 8.944272 6.708204 4.472136 2.236068
# row7 13.416408 11.180340 8.944272 6.708204 4.472136 2.236068
# row8 15.652476 13.416408 11.180340 8.944272 6.708204 4.472136 2.236068
# row9 17.888544 15.652476 13.416408 11.180340 8.944272 6.708204 4.472136 2.236068
# row10 20.124612 17.888544 15.652476 13.416408 11.180340 8.944272 6.708204 4.472136 2.236068
hr_dist
# col1 col2 col3 col4
# col2 31.62278
# col3 63.24555 31.62278
# col4 94.86833 63.24555 31.62278
# col5 126.49111 94.86833 63.24555 31.62278
所以实际上,hc_dist
包含行之间的距离和 hr_dist
之间的列距离,因此会出现错误(对于方阵你不会得到,因为行数和列数相同)
我正在使用 R 生成热图。
我使用 hclust
函数并得到错误:
Colv dendrogram doesn't match size of x
。
下面是我的代码,它与方阵完美配合。现在我的输入矩阵是 400x2000
。谁能告诉我为什么会出现此错误?
data <- read.table("mydata.txt",sep="\t",header=TRUE,row.names=1)
data_matrix <- data.matrix(data)
library(gplots)
library("RColorBrewer")
colors <- colorRampPalette(rev(brewer.pal(9, "Blues")) )(255)
hc_dist= dist(data_matrix)
hc_clust= hclust(hc_dist)
hr_dist= dist(t(data_matrix))
hr_clust= hclust(hr_dist)
heatmap.2(data_matrix, col=brewer.pal(11,"RdBu"), labRow=NA, density.info="none", scale="row",trace="none",Colv=as.dendrogram(hc_clust), Rowv=as.dendrogram(hr_clust))
Error in heatmap.2(data_matrix, col = brewer.pal(11, "RdBu"), labRow = NA,:Colv dendrogram doesn't match size of x
发展我的评论:
想象一下这个矩阵:
matex<-matrix(1:50,ncol=5)
colnames(matex)<-paste0("col",1:5)
rownames(matex)<-paste0("row",1:10)
head(matex)
# col1 col2 col3 col4 col5
#row1 1 11 21 31 41
#row2 2 12 22 32 42
#row3 3 13 23 33 43
#row4 4 14 24 34 44
#row5 5 15 25 35 45
#row6 6 16 26 36 46
现在,像您一样计算距离:
hc_dist= dist(matex) # what you think are between columns distances
hr_dist= dist(t(matex)) # what you think are between rows distances
并检查对象 hc_dist
和 hr_dist
:
hc_dist
# row1 row2 row3 row4 row5 row6 row7 row8 row9
# row2 2.236068
# row3 4.472136 2.236068
# row4 6.708204 4.472136 2.236068
# row5 8.944272 6.708204 4.472136 2.236068
# row6 11.180340 8.944272 6.708204 4.472136 2.236068
# row7 13.416408 11.180340 8.944272 6.708204 4.472136 2.236068
# row8 15.652476 13.416408 11.180340 8.944272 6.708204 4.472136 2.236068
# row9 17.888544 15.652476 13.416408 11.180340 8.944272 6.708204 4.472136 2.236068
# row10 20.124612 17.888544 15.652476 13.416408 11.180340 8.944272 6.708204 4.472136 2.236068
hr_dist
# col1 col2 col3 col4
# col2 31.62278
# col3 63.24555 31.62278
# col4 94.86833 63.24555 31.62278
# col5 126.49111 94.86833 63.24555 31.62278
所以实际上,hc_dist
包含行之间的距离和 hr_dist
之间的列距离,因此会出现错误(对于方阵你不会得到,因为行数和列数相同)