#Generate a sample data set with two well-separated clusters
set.seed(2)
f.data <- data.frame("Group"=c(rep("G1",25),rep("G2",25)), "X1"=c(rnorm(25)+3,rnorm(25)), "X2"=c(rnorm(25)-4,rnorm(25)))

#Plot data set
library(ggplot2)
gg1 <- ggplot(f.data,aes(x=X1,y=X2,color=Group))+geom_point(size=4)
gg1

#Run k-means with k=2
km.out <- kmeans(f.data[,2:3],centers=2,nstart=20)
km.out

# Print the vector indicating cluster assignment
km.out$cluster

#Print the centroids
km.out$centers

#Prepare the output for plotting
f.data$Cluster <- as.factor(km.out$cluster)
#Plot the clustering result
gg2 <- ggplot(f.data,aes(x=X1,y=X2,color=Cluster,shape=Group)) + 
  geom_point(size = 4) +
  geom_point(aes(x = km.out$centers[1, 1], y = km.out$centers[1, 2]),
             size = 5, color = "black") +
  geom_point(aes(x = km.out$centers[2, 1], y = km.out$centers[2, 2]),
             size = 5, color = "black") +
  annotate("text", x = km.out$centers[1, 1], y = km.out$centers[1, 2] - 0.2, label = "Centroid 1") +
  annotate("text", x = km.out$centers[2, 1], y = km.out$centers[2, 2] - 0.2, label = "Centroid 2") +
  ggtitle("k-means clustering, k = 2") +
  theme(plot.title = element_text(size = rel(2)))

gg2

#Generate a sample data set with two well-separated clusters
set.seed(2)
f.data=data.frame("Group"=c(rep("G1",25),rep("G2",25)), "X1"=c(rnorm(25)+3,rnorm(25)), "X2"=c(rnorm(25)-4,rnorm(25)))

#Execute k-means from k=1 to k=15
set.seed(123)
k.max <- 15
wss <- numeric(k.max)
for (i in 1:k.max) {
  KM = kmeans(f.data[,2:3], centers=i, nstart=20, iter.max = 15)
  wss[i] = KM$tot.withinss 
}

#Plot total within-cluster sum of squares vs. number of clusters
plot(1:k.max, wss, 
     type="b", pch = 19, col="blue", 
     xlab="Number of clusters (k)",
     ylab="Total within-clusters sum of squares (WSS)", 
     lwd=2,
     main="Elbow Method for Selecting k")