使用分层聚类制作简单的树状图

问题描述 投票:0回答:1

我有一个简单的距离矩阵:

I tried making my own distance matrix:

 distances=matrix(c(0,  6.32,  11.3,     1,    10,  5.66,  4.24,
           6.32,     0,  6.32,  6.08,     6,  2.83,  3.16,
           11.3,  6.32,     0,  10.6,     2,  5.66,  7.07,
           1,  6.08,  10.6,     0,  9.22,     5,  3.61,
           10,     6,     2,  9.22,     0,  4.47,  5.83,
           5.66,  2.83,  5.66,     5,  4.47,     0,  1.41,
           4.24,  3.16,  7.07,  3.61,  5.83,  1.41,     0),nrow=7,   ncol=7, byrow = TRUE)

But this doesn't cluster

    > hc <- hclust(distances)
Error in if (is.na(n) || n > 65536L) stop("size cannot be NA nor exceed 65536") : 
  missing value where TRUE/FALSE needed

我想基于测量簇之间的最小距离来创建树状图。最终产品看起来像这样:

enter image description here

有什么想法吗?帮助将不胜感激!

r hierarchical-clustering dendrogram
1个回答
0
投票

hclust的输入需要a dissimilarity structure as produced by ‘dist’,使用矩阵,您可以将其转换为'dist'对象,然后将hclust转换为图:

plot(hclust(as.dist(distances)))

enter image description here

© www.soinside.com 2019 - 2024. All rights reserved.