我使用R中的cluster
包进行层次聚类。使用silhouette
函数,我可以获得树形图中任何给定高度(h)截止的聚类输出的轮廓图
# run hierarchical clustering
if(!require("cluster")) { install.packages("cluster"); require("cluster") }
tmp <- matrix(c( 0, 20, 20, 20, 40, 60, 60, 60, 100, 120, 120, 120,
20, 0, 30, 50, 60, 80, 40, 80, 120, 100, 140, 120,
20, 30, 0, 40, 60, 80, 80, 80, 120, 140, 140, 80,
20, 50, 40, 0, 60, 80, 80, 80, 120, 140, 140, 140,
40, 60, 60, 60, 0, 20, 20, 20, 60, 80, 80, 80,
60, 80, 80, 80, 20, 0, 20, 20, 40, 60, 60, 60,
60, 40, 80, 80, 20, 20, 0, 20, 60, 80, 80, 80,
60, 80, 80, 80, 20, 20, 20, 0, 60, 80, 80, 80,
100, 120, 120, 120, 60, 40, 60, 60, 0, 20, 20, 20,
120, 100, 140, 140, 80, 60, 80, 80, 20, 0, 20, 20,
120, 140, 140, 140, 80, 60, 80, 80, 20, 20, 0, 20,
120, 120, 80, 140, 80, 60, 80, 80, 20, 20, 20, 0),
nr=12, dimnames=list(LETTERS[1:12], LETTERS[1:12]))
cl <- hclust(as.dist(tmp,diag = TRUE, upper = TRUE), method= 'single')
sil_cl <- silhouette(cutree(cl, h=25) ,as.dist(tmp), title=title(main = 'Good'))
plot(sil_cl)
这给出了下图,这是让我感到沮丧的一点。 如何在轮廓图中使用观察标签rownames(tmp)
而不是数字索引(1到12) - 这对我来说毫无意义。
答案 0 :(得分:3)
我不确定为什么,但silhouette
调用似乎删除了行名称。您可以使用
cl <- hclust(as.dist(tmp,diag = TRUE, upper = TRUE), method= 'single')
sil_cl <- silhouette(cutree(cl, h=25) ,as.dist(tmp), title=title(main = 'Good'))
rownames(sil_cl) <- rownames(tmp)
plot(sil_cl)
答案 1 :(得分:1)
我发现将参数cex.names = par("cex.axis")
添加到plot()
函数中可以为您提供所需的标签:
cl <- hclust(as.dist(tmp,diag = TRUE, upper = TRUE), method= 'single')
sil_cl <- silhouette(cutree(cl, h=25) ,as.dist(tmp), title=title(main = 'Good'))
plot(sil_cl, ces.names = par("cex.axis"))