How to produce a pretty plot of the results of k-means cluster analysis? - Cross Validated most recent 30 from stats.stackexchange.com 2019-07-20T12:21:16Z https://stats.stackexchange.com/feeds/question/31083 http://www.creativecommons.org/licenses/by-sa/3.0/rdf https://stats.stackexchange.com/q/31083 76 How to produce a pretty plot of the results of k-means cluster analysis? JEquihua https://stats.stackexchange.com/users/11748 2012-06-25T17:47:20Z 2018-10-05T07:40:33Z <p>I'm using R to do K-means clustering. I'm using 14 variables to run K-means</p> <ul> <li>What is a pretty way to plot the results of K-means? </li> <li>Are there any existing implementations?</li> <li>Does having 14 variables complicate plotting the results? </li> </ul> <p>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? </p> https://stats.stackexchange.com/questions/31083/-/31087#31087 56 Answer by Manuel Ramón for How to produce a pretty plot of the results of k-means cluster analysis? Manuel Ramón https://stats.stackexchange.com/users/221 2012-06-25T18:21:40Z 2012-06-25T19:15:59Z <p>Here an example that can helps you:</p> <pre><code>library(cluster) library(fpc) data(iris) dat &lt- iris[, -5] # without known classification # Kmeans clustre analysis clus &lt- kmeans(dat, centers=3) </code></pre> <pre><code># Fig 01 plotcluster(dat, clus\$cluster) </code></pre> <p><img src="https://i.stack.imgur.com/6yP2z.png" alt=""></p> <pre><code># More complex clusplot(dat, clus\$cluster, color=TRUE, shade=TRUE, labels=2, lines=0) </code></pre> <p><img src="https://i.stack.imgur.com/OQlbA.png" alt=""></p> <pre><code># Fig 03 with(iris, pairs(dat, col=c(1:3)[clus\$cluster])) </code></pre> <p><img src="https://i.stack.imgur.com/c7Dhd.png" alt=""></p> <p>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.</p> https://stats.stackexchange.com/questions/31083/-/31094#31094 27 Answer by user603 for How to produce a pretty plot of the results of k-means cluster analysis? user603 https://stats.stackexchange.com/users/603 2012-06-25T19:46:25Z 2014-02-09T04:30:51Z <p>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.</p> <pre><code>library(cluster) library(HSAUR) data(pottery) km &lt;- kmeans(pottery,3) dissE &lt;- daisy(pottery) dE2 &lt;- dissE^2 sk2 &lt;- silhouette(km\$cl, dE2) plot(sk2) </code></pre> <p>This approach is highly cited and well known (see <a href="ftp://adrem.ua.ac.be/pub/preprints/87/Silgra87.pdf">here</a> for an explanation). </p> <p>Rousseeuw, P.J. (1987) <a href="http://www.sciencedirect.com/science/article/pii/0377042787901257">Silhouettes: A graphical aid to the interpretation and validation of cluster analysis</a>. <em>J. Comput. Appl. Math.</em>, <em>20</em>, 53-65.</p> https://stats.stackexchange.com/questions/31083/-/246467#246467 4 Answer by darioSka for How to produce a pretty plot of the results of k-means cluster analysis? darioSka https://stats.stackexchange.com/users/138834 2016-11-17T11:22:13Z 2016-11-17T11:22:13Z <p>The simplest way I know to do that is the following:</p> <pre><code>X &lt;- data.frame(c1=c(0,1,2,4,5,4,6,7),c2=c(0,1,2,3,3,4,5,5)) km &lt;- kmeans(X, center=2) plot(X,col=km\$cluster) points(km\$center,col=1:2,pch=8,cex=1) </code></pre> <p>In this way you can draw the points of each cluster using a different color and their centroids.</p>