Visualize K-Means Clustering on a Single Vector¶
By James Marquez, May 21, 2017
This is a quick demo on how to visualize K-Means clustering on a single vector. Usually, you have κ and γ values to visualize clusters using a scatter plot. However, we only have κ values in a single column vector. We'll use a density plot in our example.
# Load ggplot2 to graph our clusters library(ggplot2) set.seed(1) # Setting seed allows you to reproduce the same random generated values vector <- rnorm(100) # Create vector of random generated values from the normal distribution data <- data.frame(vector) # Assign vector to a data frame # Find six cluster centers cluster <- kmeans(data$vector, centers=6) # Extract cluster centers vector cluster.centers <- as.factor(round(fitted(cluster, method=c("centers", "classes")))) # Visualize clusters ggplot(data, aes(x=vector)) + # Below we color by cluser.centers vector geom_density(aes(group=cluster.centers, color=cluster.centers, fill=cluster.centers), alpha=0.3) + labs(title = "Six Clusters")
That's it. Please leave a comment if you have any questions. Thanks for reading!