76
$\begingroup$

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?

$\endgroup$
  • 1
    $\begingroup$ 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. $\endgroup$ – chl Jun 27 '12 at 19:57
  • $\begingroup$ Another example with 11 classes and 10 variables is on page 118 of Elements of Statistical Learning; not terribly informative. $\endgroup$ – denis Nov 4 '13 at 16:56
  • $\begingroup$ library(animation) kmeans.ani(yourData, centers = 2) $\endgroup$ – Kartheek Palepu Aug 12 '15 at 11:10
27
$\begingroup$

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.

$\endgroup$
  • $\begingroup$ I like this. I'll look further into it. Thank you. $\endgroup$ – JEquihua Jun 25 '12 at 20:39
  • $\begingroup$ @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. $\endgroup$ – Steen Mar 19 '15 at 20:54
  • $\begingroup$ The link was pointing to the paper (it was an un-gated access point, which has indeed gone dark). $\endgroup$ – user603 Mar 19 '15 at 21:01
  • $\begingroup$ 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 $\endgroup$ – vipin8169 Apr 20 '17 at 9:33
56
$\begingroup$

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.

$\endgroup$
  • 1
    $\begingroup$ I am unable to figure out how to intepret dc1 and dc2? Could you point me to the right direction? $\endgroup$ – UD1989 Sep 17 '14 at 10:31
  • 1
    $\begingroup$ @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. $\endgroup$ – rakensi Nov 11 '14 at 12:14
  • $\begingroup$ 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" ? $\endgroup$ – mynameisJEFF May 10 '15 at 4:54
  • $\begingroup$ @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. $\endgroup$ – Hack-R May 19 '15 at 20:01
  • $\begingroup$ Why do you need "with" here? It would be leaner to just leave the pairs function. $\endgroup$ – Anatolii Stepaniuk Dec 25 '16 at 19:00
4
$\begingroup$

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.

$\endgroup$

protected by kjetil b halvorsen Oct 5 '18 at 8:18

Thank you for your interest in this question. Because it has attracted low-quality or spam answers that had to be removed, posting an answer now requires 10 reputation on this site (the association bonus does not count).

Would you like to answer one of these unanswered questions instead?

Not the answer you're looking for? Browse other questions tagged or ask your own question.