Import package

library(dplyr)
library(ggplot2)
library(ggfortify)

Import Data

View(iris)
mydata = select(iris, c(1,2,3,4))
mydata

K-Means clusters

KM = kmeans(mydata, 3)
KM
K-means clustering with 3 clusters of sizes 38, 50, 62

Cluster means:
  Sepal.Length Sepal.Width Petal.Length Petal.Width
1     6.850000    3.073684     5.742105    2.071053
2     5.006000    3.428000     1.462000    0.246000
3     5.901613    2.748387     4.393548    1.433871

Clustering vector:
  [1] 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2
 [34] 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 3 3 1 3 3 3 3 3 3 3 3 3 3 3 3 3
 [67] 3 3 3 3 3 3 3 3 3 3 3 1 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3
[100] 3 1 3 1 1 1 1 3 1 1 1 1 1 1 3 3 1 1 1 1 3 1 3 1 3 1 1 3 3 1 1 1 1
[133] 1 3 1 1 1 1 3 1 1 1 3 1 1 1 3 1 1 3

Within cluster sum of squares by cluster:
[1] 23.87947 15.15100 39.82097
 (between_SS / total_SS =  88.4 %)

Available components:

[1] "cluster"      "centers"      "totss"        "withinss"    
[5] "tot.withinss" "betweenss"    "size"         "iter"        
[9] "ifault"      

View cluster plot

autoplot(KM, mydata, frame = TRUE)

LS0tDQp0aXRsZTogIlIgTm90ZWJvb2siDQpvdXRwdXQ6IGh0bWxfbm90ZWJvb2sNCi0tLQ0KDQoNCkltcG9ydCBwYWNrYWdlDQoNCmBgYHtyfQ0KbGlicmFyeShkcGx5cikNCmxpYnJhcnkoZ2dwbG90MikNCmxpYnJhcnkoZ2dmb3J0aWZ5KQ0KDQpgYGANCg0KDQoNCkltcG9ydCBEYXRhDQoNCmBgYHtyfQ0KVmlldyhpcmlzKQ0KYGBgDQoNCg0KYGBge3J9DQpteWRhdGEgPSBzZWxlY3QoaXJpcywgYygxLDIsMyw0KSkNCm15ZGF0YQ0KYGBgDQoNCkstTWVhbnMgY2x1c3RlcnMNCg0KYGBge3J9DQpLTSA9IGttZWFucyhteWRhdGEsIDMpDQpLTQ0KYGBgDQoNCg0KVmlldyBjbHVzdGVyIHBsb3QNCg0KYGBge3J9DQphdXRvcGxvdChLTSwgbXlkYXRhLCBmcmFtZSA9IFRSVUUpDQoNCmBgYA0KDQoNCmBgYHtyfQ0KDQpgYGANCg0KDQoNCg0KDQoNCg0K