Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.2k views
in Technique[技术] by (71.8m points)

ggplot2 - Remove Grid in fviz_silhouette?

I've tried to set the colors in fviz_silhouette with transparency to achieve a more beautiful look:

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_color_manual('', values = c("#008B4544", "#FF000033", "#1C86EE44"), aesthetics = c("color", "fill")) +     # change color 
  theme(panel.background = element_blank(), #try to remove the lines, dosen′t work......
        panel.grid.major = element_blank(),
        panel.grid.minor = element_blank())

Unfortunately, some vlines have appeared (see image below). I don’t know why this has happened. Perhaps it's related to the distance between grid lines? How I can remove these lines?

silhouette plot

question from:https://stackoverflow.com/questions/65861244/remove-grid-in-fviz-silhouette

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

The dashed line is an expected behaviour of fviz_silhoutte; it's the visualization of a summary statistic, rather than a stray grid line.

It's lightly hacky, but you can edit the parameters of the plot object to make the line disappear. Setting aes_params$colour to NA will render it invisible.

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

viz_sil <- fviz_silhouette(sil) +
  scale_color_manual('', values = c("#008B4544", "#FF000033", "#1C86EE44"), aesthetics = c("color", "fill"))
#>   cluster size ave.sil.width
#> 1       1   50          0.64
#> 2       2   53          0.39
#> 3       3   47          0.35

viz_sil$layers[[2]]$aes_params$colour <- NA

viz_sil

Created on 2021-01-25 by the reprex package (v0.3.0)

Solution based off of this answer.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...