Factoextra:如何在fviz_silhouette函数中更改平均轮廓宽度的颜色? [英] Factoextra: How to change color of the average silhouette width in the fviz_silhouette function?
问题描述
我很好奇如何在fviz_silhouette函数中为默认轮廓宽度覆盖默认红色虚线的颜色值.只是偷看了fviz_silhouette代码,这让我感到困惑,为什么作者固定了行颜色参数?(从函数源代码列出.)
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, ...)
并且调色板="grey"和+ theme_bw()的结果仍然保留红色虚线,如下面的图像所示.
And the result with palette = "grey" and + theme_bw(), still preserves red dashed line, as in the image bellow.
推荐答案
您可以通过以下方式编辑颜色
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
这篇关于Factoextra:如何在fviz_silhouette函数中更改平均轮廓宽度的颜色?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!