Search code examples
rggplot2cluster-analysis

How can I change the legend in fviz_silhouette?


I am trying to generate some silhouette with a R library called "factoextra". Unfortunately, I end up in a problem. Let's look at the following example with the "iris" dataset.

library(factoextra)
library(cluster)
set.seed(123)
data("iris")
iris.scaled <- scale(iris[, -5])
# K-means clustering

km.res <- kmeans(iris.scaled, 3, nstart = 2)
# Visualize silhouhette information
sil <- silhouette(km.res$cluster, dist(iris.scaled))
fviz_silhouette(sil)+
  scale_fill_discrete(labels=c("a","b","c"))

I tried to add a specific legend "a","b","c". Example of double legend

As you can see the legend duplicate, showing "a","b","c" and then "1","2","3". Is there a way to show only "a","b","c"?


Solution

  • You can simply remove the color guide:

    sil <- silhouette(km.res$cluster, dist(iris.scaled))
    fviz_silhouette(sil)+
      scale_fill_discrete(labels=c("a","b","c"))+
      guides(col=FALSE)
    

    If you look at the source code, you can see this line that controls the mappings:

    mapping <- aes_string(x = "name", y = "sil_width", 
            color = "cluster", fill = "cluster")
    

    Setting a discrete fill and removing the color yields the expected plot.

    Result:

    enter image description here