使用fviz_cluster可视化CLARA聚类结果时如何突出中心点medoid
方案1:调整原生参数突出中心点
fviz_cluster本身提供了中心点样式的自定义参数,直接设置即可让中心点盖在普通数据点上方,不会被密集的样本淹没:
library(factoextra) library(ggplot2) p <- fviz_cluster(clara.res, palette = c("#004c6d", "#00ffff", "#00a1c1", "#6efa75", "#78ab63", "#cc0089", "#ffc334", "#ff9509", "#ffb6de", "#00cfe3" ), ellipse.type = "t", geom = "point", show.clust.cent = TRUE, # 新增中心点自定义参数 cent.point.size = 10, # 中心点尺寸远大于普通点的0.5 cent.point.shape = 23, # 菱形带填充的形状,和普通圆点区分 cent.point.color = "black", # 中心点边框颜色 cent.point.fill = "red", # 中心点填充色,和聚类颜色区分开 repel = TRUE, pointsize = 0.5, alpha = 0.1, # 普通点调低透明度,避免糊成一团遮挡中心点 ggtheme = theme_classic() ) p
方案2:隐藏所有普通数据点仅保留中心点
只需要给普通点设置完全透明即可,聚类椭圆、坐标轴等元素都可以正常保留:
p_hide_points <- fviz_cluster(clara.res, palette = c("#004c6d", "#00ffff", "#00a1c1", "#6efa75", "#78ab63", "#cc0089", "#ffc334", "#ff9509", "#ffb6de", "#00cfe3" ), ellipse.type = "t", geom = "point", show.clust.cent = TRUE, cent.point.size = 10, cent.point.shape = 23, cent.point.color = "black", cent.point.fill = "red", repel = TRUE, pointsize = 0.5, alpha = 0, # 普通点完全透明,相当于隐藏 ggtheme = theme_classic() ) p_hide_points
方案3:手动添加你已获取的medoid
fviz_cluster返回的是标准ggplot2对象,你可以直接叠加自定义的点层,完全可控中心点的样式,优先级最高不会被遮挡。
假设你已将medoid对应的行号存储在向量medoid_rows中,操作如下:
# 先生成基础绘图对象,关闭默认中心点展示 p_base <- fviz_cluster(clara.res, palette = c("#004c6d", "#00ffff", "#00a1c1", "#6efa75", "#78ab63", "#cc0089", "#ffc334", "#ff9509", "#ffb6de", "#00cfe3" ), ellipse.type = "t", geom = "point", show.clust.cent = FALSE, repel = TRUE, pointsize = 0.5, alpha = 0.1, ggtheme = theme_classic() ) # 提取medoid对应的降维坐标 medoid_data <- p_base$data[medoid_rows, ] # 手动叠加中心点层,显示在最上层 p_custom_medoid <- p_base + geom_point(data = medoid_data, aes(x = x, y = y), shape = 23, fill = "red", color = "black", size = 10, alpha = 1) p_custom_medoid
内容的提问来源于stack exchange,提问作者samudra_biru
相关产品推荐
相关产品推荐

