How can you visualize K-means clusters and their centers using a scatter plot in R?
You can visualize K-means clusters by creating a scatter plot of at least two variables, coloring the data points according to their cluster assignment, and adding the cluster centers as large, distinct points.
Conditions
- Requires at least two numeric variables for plotting.
- Requires cluster assignments and center coordinates from the kmeans output.
Reasoning, step by step
- Select two variables to plot on the x and y axes.
- Use the plot() function to create a scatter plot.
- Set the col argument in plot() to the cluster assignments from the kmeans result.
- Use the points() function to add the cluster centers to the plot.
- Customize the appearance of the center points using arguments like pch and cex.
Example
The video shows the code: plot(iris_data[c("Sepal.Length", "Sepal.Width")], col=kmeans_iris_resultcenters[,c("Sepal.Length", "Sepal.Width")], col=1:3, pch=23, cex=3).
Common misconceptions
- Forgetting to color the points by cluster assignment.
- Not adding the cluster centers to the plot, making it harder to see the partitioning.
Watch the explanation
Explore next
Answers are generated from source material and independently checked. Consult the original video or creator if something is unclear.