# How to produce a pretty plot of the results of k-means cluster analysis?

I'm using R to do K-means clustering. I'm using 14 variables to run K-means

• What is a pretty way to plot the results of K-means?
• Are there any existing implementations?
• Does having 14 variables complicate plotting the results?

I found something called GGcluster which looks cool but it is still in development. I also read something about sammon mapping, but didn't understand it very well. Would this be a good option?

• If for some reason you are concerned with the present solutions for this very practical problem, please consider adding comments to existing replies or update your post with more context. Working with 40,000 cases is an important information here. – chl Jun 27 '12 at 19:57
• Another example with 11 classes and 10 variables is on page 118 of Elements of Statistical Learning; not terribly informative. – denis Nov 4 '13 at 16:56
• library(animation) kmeans.ani(yourData, centers = 2) – Kartheek Palepu Aug 12 '15 at 11:10

I'd push the silhouette plot for this, because it's unlikely that you'll get much actionable information from pair plots when the number of dimension is 14.

library(cluster)
library(HSAUR)
data(pottery)
km    <- kmeans(pottery,3)
dissE <- daisy(pottery)
dE2   <- dissE^2
sk2   <- silhouette(km$cl, dE2) plot(sk2)  This approach is highly cited and well known (see here for an explanation). Rousseeuw, P.J. (1987) Silhouettes: A graphical aid to the interpretation and validation of cluster analysis. J. Comput. Appl. Math., 20, 53-65. • I like this. I'll look further into it. Thank you. – JEquihua Jun 25 '12 at 20:39 • @user603: Would you care to give the gist of the explanation in your answer? The link you gave 2.5 years ago has gone dead. The article is still there but a short intro to this technique would be nice. – Steen Mar 19 '15 at 20:54 • The link was pointing to the paper (it was an un-gated access point, which has indeed gone dark). – user603 Mar 19 '15 at 21:01 • I got a weird plot with this silhoette (on left is the clusplot and on right is silhoette plot, is this expected?) - i.imgur.com/ZIpPlhT.png – vipin8169 Apr 20 '17 at 9:33 Here an example that can helps you: library(cluster) library(fpc) data(iris) dat <- iris[, -5] # without known classification # Kmeans clustre analysis clus <- kmeans(dat, centers=3)  # Fig 01 plotcluster(dat, clus$cluster)


# More complex
clusplot(dat, clus$cluster, color=TRUE, shade=TRUE, labels=2, lines=0)  # Fig 03 with(iris, pairs(dat, col=c(1:3)[clus$cluster]))


Based on the latter plot you could decide which of your initial variables to plot. Maybe 14 variables are huge, so you can try a principal component analysis (PCA) before and then use the first two or three components from the PCA to perform the cluster analysis.

• I am unable to figure out how to intepret dc1 and dc2? Could you point me to the right direction? – UD1989 Sep 17 '14 at 10:31
• @Upasana Datta: The two components are the result of applying principle component analysis (PCA, function princomp) on the data. They are linear combinations of the input variables which account for most of the variability of the observations. – rakensi Nov 11 '14 at 12:14
• Hi, I am puzzled about how are the ellipse being computed in the 2nd plot ? How does it determine the "these two components explain 95.81% of point variability" ? – mynameisJEFF May 10 '15 at 4:54
• @mynameisJEFF I would assume that it's using latent/canonical variables, eignvalues, etc. You could check the documentation, but that's typically what it means when you see a biplot labeled as such. It's saying that 95.81% of the variation in the data is explained by the 2 latent variables the data is plotted upon. Update -- I just Googled it and, indeed, it uses principal components. – Hack-R May 19 '15 at 20:01
• Why do you need "with" here? It would be leaner to just leave the pairs function. – Anatolii Stepaniuk Dec 25 '16 at 19:00

The simplest way I know to do that is the following:

X <- data.frame(c1=c(0,1,2,4,5,4,6,7),c2=c(0,1,2,3,3,4,5,5))
km <- kmeans(X, center=2)
plot(X,col=km$cluster) points(km$center,col=1:2,pch=8,cex=1)


In this way you can draw the points of each cluster using a different color and their centroids.