twoway_kmeans <- function(xname, yname, kmax, xlabel, ylabel, guidecolor) {
data.km <- cbind(x1 = xname, x2 = yname)
kclusts <- data.frame(kmax=1:kmax) %>% group_by(kmax) %>% do(kclust=kmeans(data.km, .$kmax))
#do kmeans on data for each k-number of clusters
assignments <- kclusts %>% group_by(kmax) %>% do(augment(.$kclust[[1]], data.km))
twkm <- ggplot(assignments, aes_string(x = "x1", y = "x2")) + geom_point(aes(color= .cluster)) +
facet_wrap(~ kmax) + xlab(xlabel) + ylab(ylabel) +
guides(color = guide_legend(title = guidecolor, title.position = "left", nrow=1)) +
theme(legend.position="bottom")
twkm
}
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.