2

I have the following R code:

library(factoextra)

kms<-kmeans(df,18,nstart=100)

fviz_cluster(kms, data = df, alpha=0.2,shape=19,geom = "point")

It outputs the following picture:

enter image description here

Is possible to add the number of the clusters inside the respective cluster in the picture?. Or show the cluster numbers instead fo the point centers.

UPDATE.

Something similar to I want to achieve I found in : https://www.r-bloggers.com/2016/11/hybrid-hierarchical-k-means-clustering-for-optimizing-clustering-outputs-unsupervised-machine-learning/

I tried it, but I got error. So I have to install the version of factoextra that is used and change the code. So I got this:

fviz_cluster(kms, data = df,frame.level = 0.68)

enter image description here

How can i remove the numbers except the numbers on the center of the cluster?

StupidWolf
  • 45,075
  • 17
  • 40
  • 72
Lev
  • 693
  • 1
  • 8
  • 24

1 Answers1

4

There doesn't seem to be a simple solution; here is a potential workaround:

library(tidyverse)
library(factoextra)

data("iris")

# Select a single point for each category (i.e. setosa = the 25th value)
# label the selected value, then label the rest of the points with nothing ("")
iris$label <- c(rep("", 24), "setosa", rep("", 25),
                    rep("", 23), "versicolor", rep("", 26),
                    rep("", 24), "virginica", rep("", 25))


# Remove species column (5) and label column and scale the data
iris.scaled <- scale(iris[, -c(5,6)])

# K-means clustering
km.res <- kmeans(iris.scaled, 3, nstart = 10)

# Visualize clusters
fviz_cluster(km.res, iris[, -c(5,6)], alpha = 0.2, shape = 19, geom = c("point")) +
# Label the points (only the 3 with actual labels show up on the plot)
  geom_text(aes(label = iris$label))

example_1.png

jared_mamrot
  • 22,354
  • 4
  • 21
  • 46