<p><div><div><div><div data-show-faces="false" data-height="21" data-width="100" data-layout="button_count" data-send="true" data-href="https://www.r-bloggers.com/3d-correspondence-analysis-plots-in-r-using-plotly/"></div></div><div></div></div><div></div></div>
<div>
(This article was first published on <strong><a href="https://www.displayr.com/3d-correspondence-analysis-plots-in-r-using-plotly/?utm_medium=Feed&utm_source=Syndication"> R – Displayr</a></strong>, and kindly contributed to <a rel="nofollow" href="https://www.r-bloggers.com/">R-bloggers)</a> </div>
<p><iframe width="450" src="https://embedded.azureedge.net/75668/292364/49fc64cd-9097-4831-b6e1-44a7071fe2cd.html?v=32192-3c733eabf4-1362753374"></iframe>
</p>
<p><a rel="nofollow" href="https://app.displayr.com/Try/3D%20Correspondence%20Analysis" target="_blank"><span>Explore 3D Correspondence Analysis!</span></a></p>
<p>Back in the “olden days” of the 1970s it was apparently not unknown for statisticians to create 3D visualizations using <em>tinkertoys. </em>For some inexplicable reason, the advent of the PC led to a decline in this practice, with the result that 3D visualizations are now much less common. However, it is possible to create 3D-like visualizations digitally. Although they only show two dimensions at any one time, the user can understand the third dimension by interacting with the visualization. In this post I use Plotly’s excellent plotting package to create an interactive, 3D visualization of a correspondence analysis.</p>
<h2>The data</h2>
<p>The data table that I use in this example shows perceptions of different cola brands. It is a good example for correspondence analysis as the table is relatively large, and correspondence analysis is thus useful at providing a summary.</p>
<p><iframe width="450" src="https://embedded.azureedge.net/75668/292364/72544449-5b51-42cc-b5db-2e142428d3d0.html?v=32192-3c733eabf4-712038982"></iframe>
</p>
<h2>The traditional 2D correspondence analysis map</h2>
<p>The standard 2D “map” from correspondence analysis is shown below. I’ve created this using the <a rel="nofollow" href="https://github.com/Displayr/flipDimensionReduction" target="_blank">Displayr/flipDimensionReduction</a> package on GitHub, which creates maps that automatically arrange the labels so that they do not overlap. I created it using the following code (if you don’t already have this package, you will first need to first <a rel="nofollow" href="https://www.displayr.com/installing-r-packages-from-github/" target="_blank">install it from github</a>).</p>
<pre> 
library(flipDimensionReduction)
my.ca &lt;- CorrespondenceAnalysis(my.data, 
                                normalization = &quot;Column principal (scaled)&quot;)
</pre>
<p>This visualization explains 86% of the variance from the correspondence analysis. This leads to the question: is the 14% that is not explained interesting?</p>
<p><iframe width="450" src="https://embedded.azureedge.net/75668/292364/51cbf612-8413-4aca-be80-4222a3c7dcf8.html?v=32192-3c733eabf4-1401851946"><span data-mce-type="bookmark">﻿</span></iframe>
</p>
<h2>Creating the 3D interactive visualization in Plotly</h2>
<p>The standard visualization plots of correspondence analysis plots the first two dimensions. The code below uses plotly to create a 3D plot of the first three dimensions. In theory you can encode further dimensions (e.g., using color, font size, markers and the like), but I’ve never been smart enough to interpret them myself! You can readily repurpose this code to your own correspondence analysis by replacing <span>my.ca</span> with the name or the object that contains your correspondence analysis. If you use a package other than <span>flipDimensionReduction</span> to create the correspondence analysis you will also need to work out how to extract the coordinates.</p>
<pre> 
rc = my.ca$row.coordinates
cc = my.ca$column.coordinates
library(plotly)
p = plot_ly() 
p = add_trace(p, x = rc[,1], y = rc[,2], z = rc[,3],
              mode = 'text', text = rownames(rc),
              textfont = list(color = &quot;red&quot;), showlegend = FALSE) 
p = add_trace(p, x = cc[,1], y = cc[,2], z = cc[,3], 
              mode = &quot;text&quot;, text = rownames(cc), 
              textfont = list(color = &quot;blue&quot;), showlegend = FALSE) 
p &lt;- config(p, displayModeBar = FALSE)
p &lt;- layout(p, scene = list(xaxis = list(title = colnames(rc)[1]),
           yaxis = list(title = colnames(rc)[2]),
           zaxis = list(title = colnames(rc)[3]),
           aspectmode = &quot;data&quot;),
           margin = list(l = 0, r = 0, b = 0, t = 0))
p$sizingPolicy$browser$padding &lt;- 0
my.3d.plot = p
</pre>
<p>If you are reading this on a desktop, you will be able to interact with the visualization below.<br>
<iframe width="450" src="https://embedded.azureedge.net/75668/292364/49fc64cd-9097-4831-b6e1-44a7071fe2cd.html?v=32192-3c733eabf4-1362753374"></iframe>
</p>
<p><a rel="nofollow" href="https://app.displayr.com/Try/3D%20Correspondence%20Analysis" target="_blank"><span>Explore 3D Correspondence Analysis!</span></a></p>
<p>In addition to creating it in Plotly, I’ve published it online using <a rel="nofollow" href="http://www.displayr.com/" target="_blank">Displayr</a>, which is what allows you to interact with it even though it is in a web page. It’s free to do this; you just click <strong>Insert > R Output</strong>, paste in the R code, press <strong>CALCULATE</strong>, and then either choose <strong>Export > Public Web Page,</strong> or <strong>Export > Embed </strong>to embed it in another document or page. If you <a rel="nofollow" href="https://app.displayr.com/Try/3D%20Correspondence%20Analysis" target="_blank">click here</a> you will go into a Displayr document containing the code used to create the analyses and visualizations in this chart. You can modify it however you like to re-use for your own analyses.</p>
<p><b><a rel="nofollow" href="https://app.displayr.com/Try/3D%20Correspondence%20Analysis" target="_blank">Make your own</a> 3D correspondence analysis, or if you want to explore more, <a rel="nofollow" href="https://app.displayr.com/SignUp?plan_id=0" target="_blank">get started</a> with Displayr!</b></p>
	<p>
<div>
<div>To <strong>leave a comment</strong> for the author, please follow the link and comment on their blog: <strong><a href="https://www.displayr.com/3d-correspondence-analysis-plots-in-r-using-plotly/?utm_medium=Feed&utm_source=Syndication"> R – Displayr</a></strong>.</div>
<hr>
<a rel="nofollow" href="https://www.r-bloggers.com/">R-bloggers.com</a> offers <strong><a rel="nofollow" href="https://feedburner.google.com/fb/a/mailverify?uri=RBloggers">daily e-mail updates</a></strong> about <a rel="nofollow" href="https://www.r-project.org/" title="The R Project for Statistical Computing">R</a> news and <a rel="nofollow" href="https://www.r-bloggers.com/search/tutorial" title="R tutorials">tutorials</a> on topics such as: <a rel="nofollow" href="https://www.r-bloggers.com/search/data%20science" title="Data science">Data science</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/Big%20Data" title="Big Data">Big Data, <a rel="nofollow" href="https://www.r-users.com/" title="R jobs">R jobs</a>, visualization (<a rel="nofollow" href="https://www.r-bloggers.com/search/ggplot2" title="ggplot and ggplot2 tutorials">ggplot2</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/boxplot" title="Boxplots using lattice and ggplot2 tutorials">Boxplots</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/map" title="Maps and gis">maps</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/animation" title="Animation in R">animation</a>), programming (<a rel="nofollow" href="https://www.r-bloggers.com/search/RStudio" title="RStudio IDE for R">RStudio</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/sweave" title="Sweave and literate programming">Sweave</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/LaTeX" title="LaTeX in R">LaTeX</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/SQL" title="SQL and databases">SQL</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/eclipse" title="Eclipse IDE for R">Eclipse</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/git" title="git and github, Version Control System">git</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/hadoop" title="Large data in R using Hadoop">hadoop</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/Web+Scraping" title="Web Scraping of google, facebook, yahoo, twitter and more using R">Web Scraping</a>) statistics (<a rel="nofollow" href="https://www.r-bloggers.com/search/regression" title="Regressions and ANOVA analysis tutorials">regression</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/PCA" title="principal component analysis tutorial">PCA</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/time+series" title="Time series">time series</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/trading" title="finance trading">trading</a>) and more...
<img width="1" alt="" src="http://feeds.feedburner.com/~r/RBloggers/~4/E9YyRgTJKfk" height="1"></a></div>