如何在fviz_silhouette中将颜色更改为除簇号以外的变量 [英] How can I change the color to a variable other than cluster number in fviz_silhouette

查看:220
本文介绍了如何在fviz_silhouette中将颜色更改为除簇号以外的变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 R 中的软件包 factoextra 生成轮廓图.当前,轮廓通过聚类自动为图形着色.我想用另一个已定义为x的变量Site进行着色.我已经尝试过将其填充和颜色更改为站点变量,但是似乎没有任何效果.我也尝试使用scale_color_manualscale_fill_discrete.我认为关键在scale_fill_discrete中,因为另一个用户的源代码指出了

I am using package factoextra in R to generate a silhouette plot. Currently the silhouette automatically colours your graph via clustering. I want to color by another variable Site which I have defined as x. I have tried both fill and color changing it to the site variable but nothing seems to work. I have also tried using scale_color_manual and scale_fill_discrete. I think the key is in scale_fill_discrete as the source code from another user points out

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

我基本上需要将color="cluster"更改为colour= "x".
我已经将最终的绘图代码恢复为最基本的形式.

I basically need to change color="cluster" to colour= "x".
I have reverted the final plotting code back to its most basic form.

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

推荐答案

我建议使用附加的var.col输入参数的fviz_silhouette的以下修改版本.

I suggest the following modified version of fviz_silhouette with the additional var.col input argument.

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

这篇关于如何在fviz_silhouette中将颜色更改为除簇号以外的变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆