library(dplyr)
library(ggplot2)
set.seed(2014)
centers <- data.frame(cluster=factor(1:3), size=c(100, 150, 50),
x1=c(5, 0, -3), x2=c(-1, 1, -2))
points <- centers %>% group_by(cluster) %>%
do(data.frame(x1=rnorm(.$size[1], .$x1[1]),
x2=rnorm(.$size[1], .$x2[1])))
k <- kmeans(points %>% dplyr::select(x1, x2), 3)
tidy(k)
head(augment(k, points))
glance(k)
ggplot(augment(k, points), aes(x1, x2)) +
geom_point(aes(color = .cluster)) +
geom_text(aes(label = cluster), data = tidy(k), size = 10)
Run the code above in your browser using DataLab