89
$\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$
3
  • 2
    $\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
    Commented Jun 27, 2012 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
    Commented Nov 4, 2013 at 16:56
  • $\begingroup$ library(animation) kmeans.ani(yourData, centers = 2) $\endgroup$ Commented Aug 12, 2015 at 11:10

4 Answers 4

28
$\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$
4
  • $\begingroup$ I like this. I'll look further into it. Thank you. $\endgroup$
    – JEquihua
    Commented Jun 25, 2012 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
    Commented Mar 19, 2015 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
    Commented Mar 19, 2015 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$ Commented Apr 20, 2017 at 9:33
63
$\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$
5
  • 1
    $\begingroup$ I am unable to figure out how to intepret dc1 and dc2? Could you point me to the right direction? $\endgroup$
    – UD1989
    Commented Sep 17, 2014 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
    Commented Nov 11, 2014 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$ Commented May 10, 2015 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
    Commented May 19, 2015 at 20:01
  • $\begingroup$ Why do you need "with" here? It would be leaner to just leave the pairs function. $\endgroup$ Commented Dec 25, 2016 at 19:00
5
$\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$
1
$\begingroup$

This is an old question at this point, but I think the factoextra package has several useful tools for clustering and plots. For example, the fviz_cluster() function, which plots PCA dimensions 1 and 2 in a scatter plot and colors and groups the clusters. This demo goes through some different functions from factoextra.

$\endgroup$

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