微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

如何在 R 中的轮廓图中更改集群名称

如何解决如何在 R 中的轮廓图中更改集群名称

我想看看一些已知标签如何解释,或者根据我从 UMAP 获得的数据调整到二维表示。

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

例如:

# run hierarchical clustering
if(!require("cluster")) { install.packages("cluster");  require("cluster") } 
tmp <- matrix(c( 0,20,40,60,100,120,30,50,80,140,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

解决方法

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

if(!require("cluster")) { install.packages("cluster");  require("cluster") } 
if(!require("factoextra")) { install.packages("factoextra");  require("factoextra") } 
tmp <- matrix(c( 0,20,40,60,100,120,30,50,80,140,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)

enter image description here

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。