R - silhouette with observation labels

I am doing hierarchical clustering with batch cluster

in R. Using a function silhouette

, I can get a silhouette plot to output my cluster for any given height (h) in the dendrogram.

# 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)

      

This gives the figure below and this is what disappoints me. How can I use watch labels rownames(tmp)

in the silhouette plot rather than numeric indices (1 to 12), which doesn't make any sense to me.

enter image description here

+3


source to share


1 answer


I'm not sure why, but the call silhouette

seems to leave the row names. You can add them back with

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)

      



enter image description here

+3


source







All Articles