【问题标题】:How to change cluster names in silhouette plot in R如何在 R 的剪影图中更改集群名称
【发布时间】:2021-05-20 08:49:21
【问题描述】:

我试图从我从 UMAP 获得的数据中了解一些已知标签如何解释或调整为 2D 表示。

我尝试使用silhouette 函数,但集群信息必须以数字向量的形式提供,然后这些是plot(sil) 显示的。有没有办法使用集群名称或至少在图中显示这些名称而不是数字? (与here 类似的问题,但我想更改集群标签而不是示例标签)

例如:

# 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')
cluster_labels<-cutree(cl, h=25)
#here I would like to change the cluster labels from numbers to letters, for example:
#cluster_labels<-LETTERS[1:length(unique(cluster_labels))][cluster_labels]
sil_cl <- silhouette( cluster_labels,as.dist(tmp), title=title(main = 'Good'))
plot(sil_cl)#the plot should show the cluster labels instead of the numbers

【问题讨论】:

    标签: r plot silhouette


    【解决方案1】:

    我发现这可以使用factoextra 包来完成。但是,如果有人能找到使用常规 plot() 函数的方法,那就太好了

    if(!require("cluster")) { install.packages("cluster");  require("cluster") } 
    if(!require("factoextra")) { install.packages("factoextra");  require("factoextra") } 
    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')
    cluster_labels<-cutree(cl, h=25)
    sil_cl <- silhouette( cluster_labels,as.dist(tmp), title=title(main = 'Good'))
    fviz_silhouette(sil_cl)+scale_fill_discrete(labels=LETTERS[1:12])+guides(col=FALSE)
    

    【讨论】:

      猜你喜欢
      • 2015-11-04
      • 1970-01-01
      • 2018-08-22
      • 1970-01-01
      • 2020-12-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-03-04
      相关资源
      最近更新 更多