如何将颜色更改为 fviz_silhouette 中簇号以外的变量
How can I change the color to a variable other than cluster number in fviz_silhouette
我在 R 中使用包 factoextra 来生成轮廓图。目前,轮廓会通过聚类自动为您的图形着色。我想用我定义为 x 的另一个变量 Site
着色。我已经尝试将填充和颜色更改为站点变量,但似乎没有任何效果。我也尝试过使用 scale_color_manual
和 scale_fill_discrete
。我认为关键在于 scale_fill_discrete
,因为另一个用户的源代码指出
mapping <- aes_string(x = "name", y = "sil_width", color = "cluster", fill = "cluster")
我基本上需要把color="cluster"
改成colour= "x"
。
我已将最终的绘图代码还原为最基本的形式。
pamspec <- pam(spec, 3, keep.diss = TRUE)
plot(pamspec)
spec <- cbind(pamspec$clustering)
autoplot(pam(spec,3), frame=TRUE, frame.type = "norm")
pamspec$site <- spec$Site
x <- pamspec$site
fviz_silhouette(pamspec, label=TRUE) + theme_classic()
我建议使用 fviz_silhouette
的以下修改版本,增加 var.col
输入参数。
myfviz_silhouette <- function (sil.obj, var.col, label = FALSE, print.summary = TRUE, ...) {
if (inherits(sil.obj, c("eclust", "hcut", "pam", "clara",
"fanny"))) {
df <- as.data.frame(sil.obj$silinfo$widths, stringsAsFactors = TRUE)
}
else if (inherits(sil.obj, "silhouette"))
df <- as.data.frame(sil.obj[, 1:3], stringsAsFactors = TRUE)
else stop("Don't support an oject of class ", class(sil.obj))
df <- df[order(df$cluster, -df$sil_width), ]
if (!is.null(rownames(df)))
df$name <- factor(rownames(df), levels = rownames(df))
else df$name <- as.factor(1:nrow(df))
df$cluster <- as.factor(df$cluster)
df$var_col <- var.col
mapping <- aes_string(x = "name", y = "sil_width", color = "var_col",
fill = "var_col")
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, ...)
if (!label)
p <- p + theme(axis.text.x = element_blank(), axis.ticks.x = element_blank())
else if (label)
p <- p + theme(axis.text.x = element_text(angle = 45))
ave <- tapply(df$sil_width, df$cluster, mean)
n <- tapply(df$cluster, df$cluster, length)
sil.sum <- data.frame(cluster = names(ave), size = n, ave.sil.width = round(ave,
2), stringsAsFactors = TRUE)
if (print.summary)
print(sil.sum)
p
}
这是一个使用示例:
library(factoextra)
library(cluster)
pamspec <- pam(iris[,-5], 3, keep.diss = TRUE)
color_var <- iris$Species
myfviz_silhouette(pamspec, color_var, label=TRUE) +
theme_classic()
我在 R 中使用包 factoextra 来生成轮廓图。目前,轮廓会通过聚类自动为您的图形着色。我想用我定义为 x 的另一个变量 Site
着色。我已经尝试将填充和颜色更改为站点变量,但似乎没有任何效果。我也尝试过使用 scale_color_manual
和 scale_fill_discrete
。我认为关键在于 scale_fill_discrete
,因为另一个用户的源代码指出
mapping <- aes_string(x = "name", y = "sil_width", color = "cluster", fill = "cluster")
我基本上需要把color="cluster"
改成colour= "x"
。
我已将最终的绘图代码还原为最基本的形式。
pamspec <- pam(spec, 3, keep.diss = TRUE)
plot(pamspec)
spec <- cbind(pamspec$clustering)
autoplot(pam(spec,3), frame=TRUE, frame.type = "norm")
pamspec$site <- spec$Site
x <- pamspec$site
fviz_silhouette(pamspec, label=TRUE) + theme_classic()
我建议使用 fviz_silhouette
的以下修改版本,增加 var.col
输入参数。
myfviz_silhouette <- function (sil.obj, var.col, label = FALSE, print.summary = TRUE, ...) {
if (inherits(sil.obj, c("eclust", "hcut", "pam", "clara",
"fanny"))) {
df <- as.data.frame(sil.obj$silinfo$widths, stringsAsFactors = TRUE)
}
else if (inherits(sil.obj, "silhouette"))
df <- as.data.frame(sil.obj[, 1:3], stringsAsFactors = TRUE)
else stop("Don't support an oject of class ", class(sil.obj))
df <- df[order(df$cluster, -df$sil_width), ]
if (!is.null(rownames(df)))
df$name <- factor(rownames(df), levels = rownames(df))
else df$name <- as.factor(1:nrow(df))
df$cluster <- as.factor(df$cluster)
df$var_col <- var.col
mapping <- aes_string(x = "name", y = "sil_width", color = "var_col",
fill = "var_col")
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, ...)
if (!label)
p <- p + theme(axis.text.x = element_blank(), axis.ticks.x = element_blank())
else if (label)
p <- p + theme(axis.text.x = element_text(angle = 45))
ave <- tapply(df$sil_width, df$cluster, mean)
n <- tapply(df$cluster, df$cluster, length)
sil.sum <- data.frame(cluster = names(ave), size = n, ave.sil.width = round(ave,
2), stringsAsFactors = TRUE)
if (print.summary)
print(sil.sum)
p
}
这是一个使用示例:
library(factoextra)
library(cluster)
pamspec <- pam(iris[,-5], 3, keep.diss = TRUE)
color_var <- iris$Species
myfviz_silhouette(pamspec, color_var, label=TRUE) +
theme_classic()