<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/choroplethr-v3-6-1-is-now-on-cran/"></div></div><div></div></div><div></div></div>
<div>
(This article was first published on <strong><a href="http://www.arilamstein.com/blog/2017/04/24/choroplethr-v3-6-1-now-cran/"> R – AriLamstein.com</a></strong>, and kindly contributed to <a rel="nofollow" href="https://www.r-bloggers.com/">R-bloggers)</a> </div>
<p>Choroplethr version 3.6.1 is now on CRAN. This version makes it easier to</p>
<ol>
<li>Calculate the percent change in demographic datasets</li>
<li>Visualize that percent change on a map</li>
</ol>
<div>
<div>Bonus: Download the code in this post!</div>
<div><a target="_blank" rel="nofollow" href="http://www.arilamstein.com/blog/2017/04/24/choroplethr-v3-6-1-now-cran/#content_upgrade_popup_5640">Click Here</a></div>
</div>
<p>As an example, the following map shows how the median age in US States changed between 2010 and 2015:</p>
<p><a target="_blank" rel="nofollow" href="http://www.arilamstein.com/?feed-stats-url=aHR0cDovL3d3dy5hcmlsYW1zdGVpbi5jb20vd3AtY29udGVudC91cGxvYWRzLzIwMTcvMDQvc3RhdGUtbWVkaWFuLWFnZS1jaGFuZ2UucG5n&feed-stats-url-post-id=5615"><img sizes="(max-width: 642px) 100vw, 642px" data-image-description="" data-orig-file="https://i1.wp.com/www.arilamstein.com/wp-content/uploads/2017/04/state-median-age-change.png?fit=642%2C305" data-orig-size="642,305" data-image-title="state-median-age-change" class="wp-image-5616" data-comments-opened="0" data-recalc-dims="1" data-attachment-id="5616" data-permalink="http://www.arilamstein.com/blog/2017/04/24/choroplethr-v3-6-1-now-cran/state-median-age-change/" alt="" data-medium-file="https://i1.wp.com/www.arilamstein.com/wp-content/uploads/2017/04/state-median-age-change.png?fit=300%2C143" data-large-file="https://i1.wp.com/www.arilamstein.com/wp-content/uploads/2017/04/state-median-age-change.png?fit=642%2C305" data-image-meta="{&quot;aperture&quot;:&quot;0&quot;,&quot;credit&quot;:&quot;&quot;,&quot;camera&quot;:&quot;&quot;,&quot;caption&quot;:&quot;&quot;,&quot;created_timestamp&quot;:&quot;0&quot;,&quot;copyright&quot;:&quot;&quot;,&quot;focal_length&quot;:&quot;0&quot;,&quot;iso&quot;:&quot;0&quot;,&quot;shutter_speed&quot;:&quot;0&quot;,&quot;title&quot;:&quot;&quot;,&quot;orientation&quot;:&quot;0&quot;}" src="https://i1.wp.com/www.arilamstein.com/wp-content/uploads/2017/04/state-median-age-change.png?resize=642%2C305"></a></p>
<p>In the above map, both the extreme positive and negative values stand out.</p>
<p>To get this version, simply type the following from an R console:</p>
<pre>
install.packages(&quot;choroplethr&quot;)
packageVersion(&quot;choroplethr&quot;)
[1] ‘3.6.1’
</pre>
<h2>New Data</h2>
<p>To facilitate demonstrating this functionality I added two new datasets to choroplethr: <strong>df_state_age_2010</strong> and <strong>df_state_age_2015</strong>. You can load them as follows:</p>
<pre>
library(choroplethr)

data(df_state_age_2010)
data(df_state_age_2015)
</pre>
<p>You can inspect and map each dataset like this:</p>
<pre>

head(df_state_age_2010)
  region     value
1 alabama    37.5
2 alaska     33.8
3 arizona    35.5
4 arkansas   37.2
5 california 34.9
6 colorado   35.8

state_choropleth(df_state_age_2010, title=&quot;2010 Median Age Estimates&quot;)

</pre>
<p><a target="_blank" rel="nofollow" href="http://www.arilamstein.com/?feed-stats-url=aHR0cDovL3d3dy5hcmlsYW1zdGVpbi5jb20vd3AtY29udGVudC91cGxvYWRzLzIwMTcvMDQvMjAxMC1zdGF0ZS1hZ2UucG5n&feed-stats-url-post-id=5615"><img sizes="(max-width: 642px) 100vw, 642px" data-image-description="" data-orig-file="https://i0.wp.com/www.arilamstein.com/wp-content/uploads/2017/04/2010-state-age.png?fit=642%2C305" data-orig-size="642,305" data-image-title="2010-state-age" data-comments-opened="0" class="wp-image-5620" data-recalc-dims="1" data-attachment-id="5620" data-permalink="http://www.arilamstein.com/blog/2017/04/24/choroplethr-v3-6-1-now-cran/2010-state-age/" alt="" data-medium-file="https://i0.wp.com/www.arilamstein.com/wp-content/uploads/2017/04/2010-state-age.png?fit=300%2C143" data-large-file="https://i0.wp.com/www.arilamstein.com/wp-content/uploads/2017/04/2010-state-age.png?fit=642%2C305" data-image-meta="{&quot;aperture&quot;:&quot;0&quot;,&quot;credit&quot;:&quot;&quot;,&quot;camera&quot;:&quot;&quot;,&quot;caption&quot;:&quot;&quot;,&quot;created_timestamp&quot;:&quot;0&quot;,&quot;copyright&quot;:&quot;&quot;,&quot;focal_length&quot;:&quot;0&quot;,&quot;iso&quot;:&quot;0&quot;,&quot;shutter_speed&quot;:&quot;0&quot;,&quot;title&quot;:&quot;&quot;,&quot;orientation&quot;:&quot;0&quot;}" src="https://i0.wp.com/www.arilamstein.com/wp-content/uploads/2017/04/2010-state-age.png?resize=642%2C305"></a></p>
<h2>New Function: calculate_percent_change</h2>
<p>To facilitate calculating the percent change between two “choroplethr-style” dataframes I created the function <strong>calculate_percent_change</strong>.</p>
<p>This function assumes that it is given two dataframes, each with a column called <em>region</em> and <em>value</em>. The dataframes are joined by the <em>region</em> column, and the percent change between their <em>value</em> columns is computed. The result is rounded to two decimal places.</p>
<p><strong>calculate_percent_change</strong> itself returns a dataframe with <em>region</em> and <em>value</em> columns. This makes the result easy to map with choroplethr functions.</p>
<pre>
df_age_diff = calculate_percent_change(df_state_age_2010, df_state_age_2015)

head(df_age_diff)
  region     value.x value.y value
1 alabama    37.5    38.4    2.40
2 alaska     33.8    33.4    -1.18
3 arizona    35.5    36.8    3.66
4 arkansas   37.2    37.7    1.34
5 california 34.9    35.8    2.58
6 colorado   35.8    36.3    1.40

state_choropleth(df_age_diff, title=&quot;Percent Change in Median Age, 2010-2015&quot;)
</pre>
<p><a target="_blank" rel="nofollow" href="http://www.arilamstein.com/?feed-stats-url=aHR0cDovL3d3dy5hcmlsYW1zdGVpbi5jb20vd3AtY29udGVudC91cGxvYWRzLzIwMTcvMDQvYWdlLWRpZmYtZGVmYXVsdC5wbmc%3D&feed-stats-url-post-id=5615"><img sizes="(max-width: 642px) 100vw, 642px" data-image-description="" data-orig-file="https://i2.wp.com/www.arilamstein.com/wp-content/uploads/2017/04/age-diff-default.png?fit=642%2C305" data-orig-size="642,305" data-image-title="age-diff-default" data-comments-opened="0" class="wp-image-5624" data-recalc-dims="1" data-attachment-id="5624" data-permalink="http://www.arilamstein.com/blog/2017/04/24/choroplethr-v3-6-1-now-cran/age-diff-default/" alt="" data-medium-file="https://i2.wp.com/www.arilamstein.com/wp-content/uploads/2017/04/age-diff-default.png?fit=300%2C143" data-large-file="https://i2.wp.com/www.arilamstein.com/wp-content/uploads/2017/04/age-diff-default.png?fit=642%2C305" data-image-meta="{&quot;aperture&quot;:&quot;0&quot;,&quot;credit&quot;:&quot;&quot;,&quot;camera&quot;:&quot;&quot;,&quot;caption&quot;:&quot;&quot;,&quot;created_timestamp&quot;:&quot;0&quot;,&quot;copyright&quot;:&quot;&quot;,&quot;focal_length&quot;:&quot;0&quot;,&quot;iso&quot;:&quot;0&quot;,&quot;shutter_speed&quot;:&quot;0&quot;,&quot;title&quot;:&quot;&quot;,&quot;orientation&quot;:&quot;0&quot;}" src="https://i2.wp.com/www.arilamstein.com/wp-content/uploads/2017/04/age-diff-default.png?resize=642%2C305"></a></p>
<h2>New Visualization Option: Divergent Scale</h2>
<p>The above map is not as informative as it could be, because the negative values don’t stand out very much. It’s also not clear which regions are near 0 or near -5%.</p>
<p>Choroplethr 3.6.1 solves this problem by letting uses select a <em>divergent scale</em>. To do this, simply set the parameter <strong>num_colors = 0</strong>.</p>
<pre>

state_choropleth(df_age_diff,
    title      = &quot;Percent Change in Median Age, 2010-2015&quot;,
    legend     = &quot;Percent Change&quot;,
    num_colors = 0)

</pre>
<p><a target="_blank" rel="nofollow" href="http://www.arilamstein.com/?feed-stats-url=aHR0cDovL3d3dy5hcmlsYW1zdGVpbi5jb20vd3AtY29udGVudC91cGxvYWRzLzIwMTcvMDQvc3RhdGUtbWVkaWFuLWFnZS1jaGFuZ2UucG5n&feed-stats-url-post-id=5615"><img sizes="(max-width: 642px) 100vw, 642px" data-image-description="" data-orig-file="https://i1.wp.com/www.arilamstein.com/wp-content/uploads/2017/04/state-median-age-change.png?fit=642%2C305" data-orig-size="642,305" data-image-title="state-median-age-change" data-comments-opened="0" class="wp-image-5616" data-recalc-dims="1" data-attachment-id="5616" data-permalink="http://www.arilamstein.com/blog/2017/04/24/choroplethr-v3-6-1-now-cran/state-median-age-change/" alt="" data-medium-file="https://i1.wp.com/www.arilamstein.com/wp-content/uploads/2017/04/state-median-age-change.png?fit=300%2C143" data-large-file="https://i1.wp.com/www.arilamstein.com/wp-content/uploads/2017/04/state-median-age-change.png?fit=642%2C305" data-image-meta="{&quot;aperture&quot;:&quot;0&quot;,&quot;credit&quot;:&quot;&quot;,&quot;camera&quot;:&quot;&quot;,&quot;caption&quot;:&quot;&quot;,&quot;created_timestamp&quot;:&quot;0&quot;,&quot;copyright&quot;:&quot;&quot;,&quot;focal_length&quot;:&quot;0&quot;,&quot;iso&quot;:&quot;0&quot;,&quot;shutter_speed&quot;:&quot;0&quot;,&quot;title&quot;:&quot;&quot;,&quot;orientation&quot;:&quot;0&quot;}" src="https://i1.wp.com/www.arilamstein.com/wp-content/uploads/2017/04/state-median-age-change.png?resize=642%2C305"></a></p>
<p>With the divergent scale, it’s clear where both the negative and positive outliers are.</p>
<h2>Closing Notes</h2>
<p>It has always been possible to use a divergent scale via choroplethr’s object-oriented system. Functions like <strong>state_choropleth</strong> exist to hide those details. That, in turn, allows beginners to create maps with minimal work. Adding support for divergent scales with the <strong>num_colors </strong>parameter is another step in that direction.</p>
<p>The only drawback is that the <strong>num_colors</strong> parameter is now overloaded:</p>
<ol>
<li><strong>num_colors=0 </strong>will use a divergent scale. As we’ve seen above, this is useful when the values are both positive and negative.</li>
<li><strong>num_colors=1</strong> will use a continuous scale. This is useful for detecting outliers.</li>
<li>Setting num_colors to values in <strong>[2, 7] </strong>will use that many quantiles. This is a useful catch-all for detecting patterns within the data.</li>
</ol>
<p>At some point it might make sense for me to rethink choroplethr’s API for setting scales. But at this point I wanted to add the new functionality without breaking any existing code.</p>
<div>
<div>Bonus: Download the code in this post!</div>
<div><a target="_blank" rel="nofollow" href="http://www.arilamstein.com/blog/2017/04/24/choroplethr-v3-6-1-now-cran/#content_upgrade_popup_5640">Click Here</a></div>
</div>
<p> <img height="1" width="1" src="http://www.arilamstein.com/?feed-stats-post-id=5615">
<p>The post <a target="_blank" rel="nofollow" href="http://www.arilamstein.com/blog/2017/04/24/choroplethr-v3-6-1-now-cran/">Choroplethr v3.6.1 is now on CRAN</a> appeared first on <a target="_blank" rel="nofollow" href="http://www.arilamstein.com/">AriLamstein.com</a>.</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.arilamstein.com/blog/2017/04/24/choroplethr-v3-6-1-now-cran/"> R – AriLamstein.com</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/Ba3n4o25Mqg"></a></div>