Factoextra: How to change color of the average silhouette width in the fviz_silhouette function?

烈酒焚心 提交于 2021-02-11 16:59:12

问题


I'm very curious about the ways to override the color value of the default red dashed line for average silhouette width in the fviz_silhouette function. Just peeked the fviz_silhouette code, and it puzzling me, why the author fixed line color parameter? (Listing from the function source code.)

p <- ggplot(df, mapping) + geom_bar(stat = "identity") + labs(y = "Silhouette width Si", x = "", title = paste0("Clusters silhouette plot ", 
            "\n Average silhouette width: ", round(mean(df$sil_width), 
                2))) + ggplot2::ylim(c(NA, 1)) + geom_hline(yintercept = mean(df$sil_width), 
        linetype = "dashed", color = "red")
    p <- ggpubr::ggpar(p, ...)

And the result with palette = "grey" and + theme_bw(), still preserves red dashed line, as in the image bellow.


回答1:


You can edit the color via

p$layers[[2]]$aes_params$colour <- "black" # or whatever color you like

To demonstrate first make a plot (with the default red color line):

library(factoextra)
library(cluster)
data("iris")

iris.scaled <- scale(iris[, -5])

km.res <- kmeans(iris.scaled, 3, nstart = 2)

sil <- silhouette(km.res$cluster, dist(iris.scaled))
p <- fviz_silhouette(sil)
p

Now change the color to black:

p$layers[[2]]$aes_params$colour <- "black"
p



来源:https://stackoverflow.com/questions/61548005/factoextra-how-to-change-color-of-the-average-silhouette-width-in-the-fviz-silh

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!