<p><div><div><div><div data-width="100" data-show-faces="false" data-send="true" data-layout="button_count" data-height="21" data-href="http://www.r-bloggers.com/principal-component-analysis-cluster-plots-with-plotly/"></div></div><div><a data-url="http://www.r-bloggers.com/principal-component-analysis-cluster-plots-with-plotly/" data-counturl="http://www.r-bloggers.com/principal-component-analysis-cluster-plots-with-plotly/" data-count="horizontal" data-text="Principal Component Analysis Cluster Plots with Plotly" class="twitter-share-button" data-via="rbloggers" href="https://twitter.com/share"></a></div><div></div></div><div></div></div>
<div>
(This article was first published on <strong><a href="http://moderndata.plot.ly/principal-component-analysis-cluster-plotly/"> R – Modern Data</a></strong>, and kindly contributed to <a rel="nofollow" href="http://www.r-bloggers.com/">R-bloggers)</a> </div>
<h3>The Problem</h3>
<p>When clustering data using principal component analysis, it is often of interest to visually inspect how well the data points separate in 2-D space based on principal component scores. While this is fairly straightforward to visualize with a scatterplot, the plot can become cluttered quickly with annotations as shown in the following figure:</p>
<p><a target="_blank" href="http://imgur.com/oBoVHiL"><img title="source: imgur.com" src="http://i2.wp.com/i.imgur.com/oBoVHiL.png?w=456" data-recalc-dims="1"></a></p>
<h3>Solution using <code>ggrepel</code></h3>
<p>The <a target="_blank" href="https://github.com/slowkow/ggrepel"><code>ggrepel</code></a> package by <a target="_blank" href="https://github.com/slowkow">Kamil Slowikowski</a> <em>implements functions to repel overlapping text labels away from each other and away from the data points that they label</em>. It’s an easy to use package that works well in this example as shown in the following figure:</p>
<p><a target="_blank" href="http://imgur.com/YBva7R4"><img title="source: imgur.com" src="http://i2.wp.com/i.imgur.com/YBva7R4.png?w=456" data-recalc-dims="1"></a></p>
<h3>Solution using <code>plotly</code></h3>
<p>An alternative solution is to use interactive plots that are usable from the <code>R</code> console, in the <code>RStudio</code> viewer pane, in <code>R Markdown</code> documents, and in <code>Shiny</code> apps. Annotations can be viewed by hovering the mouse pointer over a point or dragging a rectangle around the relevant area to zoom in. Interactive plots using <a target="_blank" href="https://plot.ly/r/"><code>plotly</code></a> allow you to de-clutter the plotting area, include extra annotation information and create interactive web-based visualizations directly from <code>R</code>. Once uploaded to a <code>plotly</code> account, <code>plotly</code> graphs (and the data behind them) can be viewed and modified in a web browser.</p>
<p><iframe height="800" width="900" src="https://plot.ly/~sahirbhatnagar/56.embed"></iframe></p>
<p>The resulting plot is clean and not cluttered with text annotations. While the <a target="_blank" href="https://github.com/slowkow/ggrepel"><code>ggrepel</code></a> package provides a nice solution in this example, the <a target="_blank" href="https://plot.ly/r/"><code>plotly</code></a> solution will be even more useful with a larger number of data points. </p>
<h3>The Code</h3>
<h4>Principal Component Analysis and Hierarchical Clustering</h4>

<pre># cor = TRUE indicates that PCA is performed on 
# standardized data (mean = 0, variance = 1)
pcaCars &lt;- princomp(mtcars, cor = TRUE)

# view objects stored in pcaCars
names(pcaCars)

# proportion of variance explained
summary(pcaCars)

# scree plot
plot(pcaCars, type = &quot;l&quot;)

# cluster cars
carsHC &lt;- hclust(dist(pcaCars$scores), method = &quot;ward.D2&quot;)

# dendrogram
plot(carsHC)

# cut the dendrogram into 3 clusters
carsClusters &lt;- cutree(carsHC, k = 3)

# add cluster to data frame of scores
carsDf &lt;- data.frame(pcaCars$scores, &quot;cluster&quot; = factor(carsClusters))
carsDf &lt;- transform(carsDf, cluster_name = paste(&quot;Cluster&quot;,carsClusters))</pre>

<h4>First figure using <code>ggplot2</code></h4>

<pre>library(ggplot2)
p1 &lt;- ggplot(carsDf,aes(x=Comp.1, y=Comp.2)) +
      theme_classic() +
      geom_hline(yintercept = 0, color = &quot;gray70&quot;) +
      geom_vline(xintercept = 0, color = &quot;gray70&quot;) +
      geom_point(aes(color = cluster), alpha = 0.55, size = 3) +
      xlab(&quot;PC1&quot;) +
      ylab(&quot;PC2&quot;) + 
      xlim(-5, 6) + 
      ggtitle(&quot;PCA Clusters from Hierarchical Clustering of Cars Data&quot;) 

p1 + geom_text(aes(y = Comp.2 + 0.25, label = rownames(carsDf)))</pre>

<h4>Second figure using <code>ggplot2</code> with <code>ggrepel</code></h4>

<pre>library(ggplot2)
library(ggrepel)

p1 + geom_text_repel(aes(y = Comp.2 + 0.25, label = rownames(carsDf)))</pre>

<h4>Interactive plot using <code>plotly</code></h4>

<pre>library(plotly)
p &lt;- plot_ly(carsDf, x = Comp.1 , y = Comp.2, text = rownames(carsDf),
             mode = &quot;markers&quot;, color = cluster_name, marker = list(size = 11)) 

p &lt;- layout(p, title = &quot;PCA Clusters from Hierarchical Clustering of Cars Data&quot;, 
       xaxis = list(title = &quot;PC 1&quot;),
       yaxis = list(title = &quot;PC 2&quot;))

p</pre>

<h3>References</h3>
<p><a target="_blank" href="https://www.dropbox.com/s/mjdtdpgji74cut1/PCA_with_R.pdf?dl=0">PCA with R</a> by <a target="_blank" href="http://gastonsanchez.com/">Gaston Sanchez</a></p>
<p><div><div><div><div data-width="55" data-show-faces="false" data-send="true" data-layout="box_count" data-height="62" data-href="http://www.r-bloggers.com/principal-component-analysis-cluster-plots-with-plotly/"></div></div><div><a data-url="http://www.r-bloggers.com/principal-component-analysis-cluster-plots-with-plotly/" data-counturl="http://www.r-bloggers.com/principal-component-analysis-cluster-plots-with-plotly/" data-count="vertical" data-text="Principal Component Analysis Cluster Plots with Plotly" class="twitter-share-button" data-via="rbloggers" href="https://twitter.com/share"></a></div><div></div></div><div></div></div>
<div>
<div>To <strong>leave a comment</strong> for the author, please follow the link and comment on their blog: <strong><a href="http://moderndata.plot.ly/principal-component-analysis-cluster-plotly/"> R – Modern Data</a></strong>.</div>
<hr>
<a rel="nofollow" href="http://www.r-bloggers.com/">R-bloggers.com</a> offers <strong><a rel="nofollow" href="http://feedburner.google.com/fb/a/mailverify?uri=RBloggers">daily e-mail updates</a></strong> about <a title="The R Project for Statistical Computing" rel="nofollow" href="http://www.r-project.org/">R</a> news and <a title="R tutorials" rel="nofollow" href="http://www.r-bloggers.com/?s=tutorial">tutorials</a> on topics such as: <a title="Data science" rel="nofollow" href="http://www.r-bloggers.com/?s=data%20science">Data science</a>, <a title="Big Data" rel="nofollow" href="http://www.r-bloggers.com/?s=Big%20Data">Big Data, <a title="R jobs" rel="nofollow" href="http://www.r-users.com/">R jobs</a>, visualization (<a title="ggplot and ggplot2 tutorials" rel="nofollow" href="http://www.r-bloggers.com/?s=ggplot2">ggplot2</a>, <a title="Boxplots using lattice and ggplot2 tutorials" rel="nofollow" href="http://www.r-bloggers.com/?s=boxplot">Boxplots</a>, <a title="Maps and gis" rel="nofollow" href="http://www.r-bloggers.com/?s=map">maps</a>, <a title="Animation in R" rel="nofollow" href="http://www.r-bloggers.com/?s=animation">animation</a>), programming (<a title="RStudio IDE for R" rel="nofollow" href="http://www.r-bloggers.com/?s=RStudio">RStudio</a>, <a title="Sweave and literate programming" rel="nofollow" href="http://www.r-bloggers.com/?s=sweave">Sweave</a>, <a title="LaTeX in R" rel="nofollow" href="http://www.r-bloggers.com/?s=LaTeX">LaTeX</a>, <a title="SQL and databases" rel="nofollow" href="http://www.r-bloggers.com/?s=SQL">SQL</a>, <a title="Eclipse IDE for R" rel="nofollow" href="http://www.r-bloggers.com/?s=eclipse">Eclipse</a>, <a title="git and github, Version Control System" rel="nofollow" href="http://www.r-bloggers.com/?s=git">git</a>, <a title="Large data in R using Hadoop" rel="nofollow" href="http://www.r-bloggers.com/?s=hadoop">hadoop</a>, <a title="Web Scraping of google, facebook, yahoo, twitter and more using R" rel="nofollow" href="http://www.r-bloggers.com/?s=Web+Scraping">Web Scraping</a>) statistics (<a title="Regressions and ANOVA analysis tutorials" rel="nofollow" href="http://www.r-bloggers.com/?s=regression">regression</a>, <a title="principal component analysis tutorial" rel="nofollow" href="http://www.r-bloggers.com/?s=PCA">PCA</a>, <a title="Time series" rel="nofollow" href="http://www.r-bloggers.com/?s=time+series">time series</a>, <a title="finance trading" rel="nofollow" href="http://www.r-bloggers.com/?s=trading">trading</a>) and more...
<img height="1" alt="" width="1" src="http://feeds.feedburner.com/~r/RBloggers/~4/ok0vEQ7ZTVk"></a></div>