In [7]:
library(ggplot2)
c0 = data.frame(x1=rnorm(100, mean=0, sd=1), x2=rnorm(100, mean=0, sd=1))
c1 = data.frame(x1=rnorm(100, mean=3, sd=1), x2=rnorm(100, mean=3, sd=1))
c2 = data.frame(x1=rnorm(100, mean=-3, sd=1), x2=rnorm(100, mean=3, sd=1))
c3 = data.frame(x1=rnorm(100, mean=-2, sd=0.5), x2=rnorm(100, mean=-2, sd=0.5))
data=rbind(c0,c1,c2,c3)
ggplot(data, aes(x=x1, y=x2)) + geom_point(shape=1) + theme_bw()
In [46]:
set.seed(20)
myCluster <- kmeans(data, 4, nstart = 30)
myCluster$centers
In [47]:
ggplot(as.data.frame(myCluster$centers), aes(x=x1, y=x2)) + geom_point(shape=1) +
theme_bw() +
geom_point(data=data, aes(x=x1, y=x2, col=factor(myCluster$cluster)))