<p><div><div><div><div data-width="100" data-show-faces="false" data-send="true" data-layout="button_count" data-height="21" data-href="https://www.r-bloggers.com/salaries-by-alma-mater-an-interactive-visualization-with-r-and-plotly/"></div></div><div></div></div><div></div></div>
<div>
(This article was first published on <strong><a href="http://www.alexejgossmann.com/salaries_by_school_plotly_viz/"> Alexej's blog</a></strong>, and kindly contributed to <a rel="nofollow" href="https://www.r-bloggers.com/">R-bloggers)</a> </div>
<p><img alt="Visualization of starting salaries by college" src="https://i1.wp.com/www.alexejgossmann.com/images/salaries_by_college/starting_salary.png?w=456" data-recalc-dims="1"></p>
<p>Based on <a target="_blank" rel="nofollow" href="http://online.wsj.com/public/resources/documents/info-Salaries_for_Colleges_by_Region-sort.html">an interesting dataset from the Wall Street Journal</a> I made the above visualization of the median starting salary for US college graduates from different undergraduate institutions (I have also looked at the mid-career salaries, and the salary increase, but more on that later). However, I thought that it would be a lot more informative, if it were <em>interactive</em>. To the very least I wanted to be able to see the school names when hovering over or clicking on the points with the mouse.</p>
<p>Luckily, this kind of interactivity can be easily achieved in R with the library <a target="_blank" rel="nofollow" href="https://cran.r-project.org/package=plotly"><code>plotly</code></a>, especially due to its excellent integration with <a target="_blank" rel="nofollow" href="https://cran.r-project.org/package=ggplot2"><code>ggplot2</code></a>, which I used to produce the above figure. In the following I describe how exactly this can be done.</p>
<p>Before I show you the interactive visualizations, a few words on the data preprocessing, and on how the map and the points are plotted with <code>ggplot2</code>:</p>
<ul>
<li>I generally use functions from the <a target="_blank" rel="nofollow" href="http://tidyverse.org/">tidyverse</a> R packages.</li>
<li>I save the data in the data frame <code>salaries</code>, and transform the given amounts to proper floating point numbers, stripping the dollar signs and extra whitespaces.</li>
<li>The data provide school names. However, I need to find out the exact geographical coordinates of each school to put it on the map. This can be done in a very convenient way, by using the <code>geocode</code> function from the <a target="_blank" rel="nofollow" href="https://cran.r-project.org/package=ggmap"><code>ggmap</code></a> R package:
<div>
<pre><code><span>school_longlat</span><span> </span><span>&lt;-</span><span> </span><span>geocode</span><span>(</span><span>salaries</span><span>$</span><span>school</span><span>)</span><span>
</span><span>school_longlat</span><span>$</span><span>school</span><span> </span><span>&lt;-</span><span> </span><span>salaries</span><span>$</span><span>school</span><span>
</span><span>salaries</span><span> </span><span>&lt;-</span><span> </span><span>left_join</span><span>(</span><span>salaries</span><span>,</span><span> </span><span>school_longlat</span><span>)</span><span>
</span></code></pre>
</div>
</li>
<li>For the visualization I want to disregard the colleges in Alaska and Hawaii to avoid shrinking the rest of the map. The respective rows of <code>salaries</code> can be easily determined with a <code>grep</code> search:
<div>
<pre><code><span>grep</span><span>(</span><span>&quot;alaska&quot;</span><span>,</span><span> </span><span>salaries</span><span>$</span><span>school</span><span>,</span><span> </span><span>ignore.case</span><span> </span><span>=</span><span> </span><span>1</span><span>)</span><span>
</span><span># [1] 206
</span><span>grep</span><span>(</span><span>&quot;hawaii&quot;</span><span>,</span><span> </span><span>salaries</span><span>$</span><span>school</span><span>,</span><span> </span><span>ignore.case</span><span> </span><span>=</span><span> </span><span>1</span><span>)</span><span>
</span><span># [1] 226
</span></code></pre>
</div>
</li>
<li>A data frame containing geographical data that can be used to plot the outline of all US states can be loaded using the function <code>map_data</code> from the <code>ggplot2</code> package:
<div>
<pre><code><span>states</span><span> </span><span>&lt;-</span><span> </span><span>map_data</span><span>(</span><span>&quot;state&quot;</span><span>)</span><span>
</span></code></pre>
</div>
</li>
<li>And I load a yellow-orange-red palette with the function <code>brewer.pal</code> from the <a target="_blank" rel="nofollow" href="http://colorbrewer2.org/"><code>RColorBrewer</code> library</a>, to use as a scale for the salary amounts:
<div>
<pre><code><span>yor_col</span><span> </span><span>&lt;-</span><span> </span><span>brewer.pal</span><span>(</span><span>6</span><span>,</span><span> </span><span>&quot;YlOrRd&quot;</span><span>)</span><span>
</span></code></pre>
</div>
</li>
<li>Finally the (yet non-interactive) visualization is created with <code>ggplot2</code>:
<div>
<pre><code><span>p</span><span> </span><span>&lt;-</span><span> </span><span>ggplot</span><span>(</span><span>salaries</span><span>[</span><span>-</span><span>c</span><span>(</span><span>206</span><span>,</span><span> </span><span>226</span><span>),</span><span> </span><span>])</span><span> </span><span>+</span><span>
</span><span>geom_polygon</span><span>(</span><span>aes</span><span>(</span><span>x</span><span> </span><span>=</span><span> </span><span>long</span><span>,</span><span> </span><span>y</span><span> </span><span>=</span><span> </span><span>lat</span><span>,</span><span> </span><span>group</span><span> </span><span>=</span><span> </span><span>group</span><span>),</span><span>
</span><span>data</span><span> </span><span>=</span><span> </span><span>states</span><span>,</span><span> </span><span>fill</span><span> </span><span>=</span><span> </span><span>&quot;black&quot;</span><span>,</span><span>
</span><span>color</span><span> </span><span>=</span><span> </span><span>&quot;white&quot;</span><span>)</span><span> </span><span>+</span><span>
</span><span>geom_point</span><span>(</span><span>aes</span><span>(</span><span>x</span><span> </span><span>=</span><span> </span><span>lon</span><span>,</span><span> </span><span>y</span><span> </span><span>=</span><span> </span><span>lat</span><span>,</span><span>
</span><span>color</span><span> </span><span>=</span><span> </span><span>starting</span><span>,</span><span> </span><span>text</span><span> </span><span>=</span><span> </span><span>school</span><span>))</span><span> </span><span>+</span><span>
</span><span>coord_fixed</span><span>(</span><span>1.3</span><span>)</span><span> </span><span>+</span><span>
</span><span>scale_color_gradientn</span><span>(</span><span>name</span><span> </span><span>=</span><span> </span><span>&quot;Starting\nSalary&quot;</span><span>,</span><span>
</span><span>colors</span><span> </span><span>=</span><span> </span><span>rev</span><span>(</span><span>yor_col</span><span>),</span><span>
</span><span>labels</span><span> </span><span>=</span><span> </span><span>comma</span><span>)</span><span> </span><span>+</span><span>
</span><span>guides</span><span>(</span><span>size</span><span> </span><span>=</span><span> </span><span>FALSE</span><span>)</span><span> </span><span>+</span><span>
</span><span>theme_bw</span><span>()</span><span> </span><span>+</span><span>
</span><span>theme</span><span>(</span><span>axis.text</span><span> </span><span>=</span><span> </span><span>element_blank</span><span>(),</span><span>
</span><span>axis.line</span><span> </span><span>=</span><span> </span><span>element_blank</span><span>(),</span><span>
</span><span>axis.ticks</span><span> </span><span>=</span><span> </span><span>element_blank</span><span>(),</span><span>
</span><span>panel.border</span><span> </span><span>=</span><span> </span><span>element_blank</span><span>(),</span><span>
</span><span>panel.grid</span><span> </span><span>=</span><span> </span><span>element_blank</span><span>(),</span><span>
</span><span>axis.title</span><span> </span><span>=</span><span> </span><span>element_blank</span><span>())</span><span>
</span></code></pre>
</div>
</li>
</ul>
<p>Now, entering <code>p</code> into the R console will generate the figure shown at the top of this post.</p>
<p>However, we want to…</p>
<h2>…make it interactive</h2>
<p>The function <code>ggplotly</code> immediately generates a <a target="_blank" rel="nofollow" href="https://plot.ly/">plotly</a> interactive visualization from a <code>ggplot</code> object. It’s that simple! :smiley: (Though I must admit that, more often than I would be okay with, some elements of the ggplot visualization disappear or don’t look as expected. :fearful:)</p>
<p>The function argument <code>tooltip</code> can be used to specify which aesthetic mappings from the <code>ggplot</code> call should be shown in the tooltip. So, the code</p>
<div>
<pre><code><span>ggplotly</span><span>(</span><span>p</span><span>,</span><span> </span><span>tooltip</span><span> </span><span>=</span><span> </span><span>c</span><span>(</span><span>&quot;text&quot;</span><span>,</span><span> </span><span>&quot;starting&quot;</span><span>),</span><span>
</span><span>width</span><span> </span><span>=</span><span> </span><span>800</span><span>,</span><span> </span><span>height</span><span> </span><span>=</span><span> </span><span>500</span><span>)</span><span>
</span></code></pre>
</div>
<p>generates <a target="_blank" rel="nofollow" href="https://plot.ly/~agisga/13.embed">the following interactive visualization</a>.</p>
<p><iframe width="450" src="http://plot.ly/~agisga/13.embed"></iframe></p>
<p>Now, if you want to publish a plotly visualization to <a target="_blank" rel="nofollow" href="https://plot.ly/">https://plot.ly/</a>, you first need to communicate your account info to the plotly R package:</p>
<div>
<pre><code><span>Sys.setenv</span><span>(</span><span>&quot;plotly_username&quot;</span><span> </span><span>=</span><span> </span><span>&quot;??????&quot;</span><span>)</span><span>
</span><span>Sys.setenv</span><span>(</span><span>&quot;plotly_api_key&quot;</span><span> </span><span>=</span><span> </span><span>&quot;????????????&quot;</span><span>)</span><span>
</span></code></pre>
</div>
<p>and after that, posting the visualization to your account at <a target="_blank" rel="nofollow" href="https://plot.ly/">https://plot.ly/</a> is as simple as:</p>
<div>
<pre><code><span>plotly_POST</span><span>(</span><span>filename</span><span> </span><span>=</span><span> </span><span>&quot;Starting&quot;</span><span>,</span><span> </span><span>sharing</span><span> </span><span>=</span><span> </span><span>&quot;public&quot;</span><span>)</span><span>
</span></code></pre>
</div>
<h2>More visualizations</h2>
<p>Finally, based on <a target="_blank" rel="nofollow" href="http://online.wsj.com/public/resources/documents/info-Salaries_for_Colleges_by_Region-sort.html">the same dataset</a> I have generated an interactive visualization of the median mid-career salaries by undergraduate alma mater (the R script is almost identical to the one described above).<br>
<a target="_blank" rel="nofollow" href="https://plot.ly/~agisga/15.embed">The resulting interactive visualization</a> is embedded below.</p>
<p><iframe width="450" src="http://plot.ly/~agisga/15.embed"></iframe></p>
<p>Additionally, it is quite informative to look at a <a target="_blank" rel="nofollow" href="https://plot.ly/~agisga/11.embed">visualization of the salary increase from starting to mid-career</a>.</p>
<p><iframe width="450" src="http://plot.ly/~agisga/11.embed"></iframe></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="http://www.alexejgossmann.com/salaries_by_school_plotly_viz/"> Alexej's blog</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 title="The R Project for Statistical Computing" rel="nofollow" href="https://www.r-project.org/">R</a> news and <a title="R tutorials" rel="nofollow" href="https://www.r-bloggers.com/search/tutorial">tutorials</a> on topics such as: <a title="Data science" rel="nofollow" href="https://www.r-bloggers.com/search/data%20science">Data science</a>, <a title="Big Data" rel="nofollow" href="https://www.r-bloggers.com/search/Big%20Data">Big Data, <a title="R jobs" rel="nofollow" href="https://www.r-users.com/">R jobs</a>, visualization (<a title="ggplot and ggplot2 tutorials" rel="nofollow" href="https://www.r-bloggers.com/search/ggplot2">ggplot2</a>, <a title="Boxplots using lattice and ggplot2 tutorials" rel="nofollow" href="https://www.r-bloggers.com/search/boxplot">Boxplots</a>, <a title="Maps and gis" rel="nofollow" href="https://www.r-bloggers.com/search/map">maps</a>, <a title="Animation in R" rel="nofollow" href="https://www.r-bloggers.com/search/animation">animation</a>), programming (<a title="RStudio IDE for R" rel="nofollow" href="https://www.r-bloggers.com/search/RStudio">RStudio</a>, <a title="Sweave and literate programming" rel="nofollow" href="https://www.r-bloggers.com/search/sweave">Sweave</a>, <a title="LaTeX in R" rel="nofollow" href="https://www.r-bloggers.com/search/LaTeX">LaTeX</a>, <a title="SQL and databases" rel="nofollow" href="https://www.r-bloggers.com/search/SQL">SQL</a>, <a title="Eclipse IDE for R" rel="nofollow" href="https://www.r-bloggers.com/search/eclipse">Eclipse</a>, <a title="git and github, Version Control System" rel="nofollow" href="https://www.r-bloggers.com/search/git">git</a>, <a title="Large data in R using Hadoop" rel="nofollow" href="https://www.r-bloggers.com/search/hadoop">hadoop</a>, <a title="Web Scraping of google, facebook, yahoo, twitter and more using R" rel="nofollow" href="https://www.r-bloggers.com/search/Web+Scraping">Web Scraping</a>) statistics (<a title="Regressions and ANOVA analysis tutorials" rel="nofollow" href="https://www.r-bloggers.com/search/regression">regression</a>, <a title="principal component analysis tutorial" rel="nofollow" href="https://www.r-bloggers.com/search/PCA">PCA</a>, <a title="Time series" rel="nofollow" href="https://www.r-bloggers.com/search/time+series">time series</a>, <a title="finance trading" rel="nofollow" href="https://www.r-bloggers.com/search/trading">trading</a>) and more...
<img height="1" alt="" width="1" src="http://feeds.feedburner.com/~r/RBloggers/~4/eZinr0WrsAw"></a></div>