<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-0-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/17/choroplethr-v3-6-0-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.0 is now on CRAN. This version adds functionality for getting and mapping demographics of US Census Tracts. You can install it from the R console as follows:</p>
<pre> 
install.packages(&quot;choroplethr&quot;) 
packageVersion(&quot;choroplethr&quot;) 
[1] ‘3.6.0’ 
</pre>
<p>To use this functionality you will need an API key from the US Census Bureau. You can learn more about that <a target="_blank" rel="nofollow" href="http://www.arilamstein.com/?feed-stats-url=aHR0cDovL3d3dy5hcmlsYW1zdGVpbi5jb20vb3Blbi1zb3VyY2UvY2hvcm9wbGV0aHIvbWFwcGluZy11cy1jZW5zdXMtZGF0YS8%3D&feed-stats-url-post-id=5563">here</a>.</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/17/choroplethr-v3-6-0-now-cran/#content_upgrade_popup_5608">Click Here</a></div>
</div>
<h2>Getting Tract Maps</h2>
<p>Historically choroplethr has had limited support for Census Tracts. This is because the US Census Bureau releases tract maps on a per-state basis, and it wasn’t feasible to create a separate package for each state.</p>
<p>Choroplethr now uses the tigris package to download Tract maps from the Census Bureau on demand. The function to retrieve a map is <strong>get_tract_map</strong>. Here’s an example of retrieving and rendering a map of Tracts in New York:</p>
<pre>

library(choroplethr)
library(ggplot2)

?get_tract_map
ny = get_tract_map(&quot;new york&quot;)
ggplot(ny, aes(long, lat, group=group)) + geom_polygon()

</pre>
<p><a target="_blank" rel="nofollow" href="http://www.arilamstein.com/?feed-stats-url=aHR0cDovL3d3dy5hcmlsYW1zdGVpbi5jb20vd3AtY29udGVudC91cGxvYWRzLzIwMTcvMDQvbnktZ2dwbG90LnBuZw%3D%3D&feed-stats-url-post-id=5563"><img sizes="(max-width: 456px) 100vw, 456px" data-image-description="" data-orig-file="https://i2.wp.com/www.arilamstein.com/wp-content/uploads/2017/04/ny-ggplot.png?fit=456%2C361" data-orig-size="456,361" data-image-title="ny-ggplot" class="wp-image-5564" data-comments-opened="0" data-recalc-dims="1" data-attachment-id="5564" data-permalink="http://www.arilamstein.com/blog/2017/04/17/choroplethr-v3-6-0-now-cran/ny-ggplot/" alt="" data-medium-file="https://i2.wp.com/www.arilamstein.com/wp-content/uploads/2017/04/ny-ggplot.png?fit=300%2C238" data-large-file="https://i2.wp.com/www.arilamstein.com/wp-content/uploads/2017/04/ny-ggplot.png?fit=456%2C361" 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/ny-ggplot.png?resize=456%2C361"></a></p>
<h2>Getting Tract-Level Demographics</h2>
<p>Choroplethr contains both maps <em>and </em>interesting data to map. If you want to explore the demographics of US Census Tracts then use <strong>get_tract_demographics</strong>:</p>
<pre>

# see help for extra options
?get_tract_demographics

ny_stats = get_tract_demographics(&quot;new york&quot;)
head(ny_stats)

</pre>
<pre>region      total_population percent_white percent_black percent_asian percent_hispanic per_capita_income median_rent median_age
36001000100 2163             19            57            2             19               19065             596         36.9
36001000200 5335             9             72            0             13               15376             501         27.8
36001000300 6077             35            44            3             17               20804             743         31.0
36001000401 2380             88            7             2             2                39574             1198        65.5
36001000403 4338             65            19            11            5                32397             859         41.9
36001000404 4932             69            12            7             9                2479              NA          19.6</pre>
<p> </p>
<h2>Creating Choropleth Maps</h2>
<p>Now that we have a map and spatial data, we can create a choropleth map with the function <strong>tract_choropleth</strong>.</p>
<p>Recall that all choroplethr functions require a dataframe where one column is called “region” and one column is called “value”. <strong>get_tract_demographics </strong>returns a dataframe with a “region” column and eight demographic values. We still need to create a “value” column. Let’s go with <strong>median_rent</strong>:</p>
<pre>

ny_stats$value = ny_stats$median_rent

?tract_choropleth
tract_choropleth(ny_stats, &quot;new york&quot;, title = &quot;2013 Median Rent\nCensus Tracts&quot;, legend=&quot;Dollars&quot;)

</pre>
<p><a target="_blank" rel="nofollow" href="http://www.arilamstein.com/?feed-stats-url=aHR0cDovL3d3dy5hcmlsYW1zdGVpbi5jb20vd3AtY29udGVudC91cGxvYWRzLzIwMTcvMDQvbnktZnVsbC5wbmc%3D&feed-stats-url-post-id=5563"><img sizes="(max-width: 482px) 100vw, 482px" data-image-description="" data-orig-file="https://i1.wp.com/www.arilamstein.com/wp-content/uploads/2017/04/ny-full.png?fit=482%2C361" data-orig-size="482,361" data-image-title="ny-full" data-comments-opened="0" class="wp-image-5566" data-recalc-dims="1" data-attachment-id="5566" data-permalink="http://www.arilamstein.com/blog/2017/04/17/choroplethr-v3-6-0-now-cran/ny-full/" alt="" data-medium-file="https://i1.wp.com/www.arilamstein.com/wp-content/uploads/2017/04/ny-full.png?fit=300%2C225" data-large-file="https://i1.wp.com/www.arilamstein.com/wp-content/uploads/2017/04/ny-full.png?fit=482%2C361" 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/ny-full.png?resize=482%2C361"></a></p>
<p> </p>
<p>People not familiar with New York might see the above map and not know where major landmarks are. To solve this problem, all choroplethr functions have a “reference_map” parameter, which puts a google map underneath the choropleth:</p>
<pre>

tract_choropleth(ny_stats, &quot;new york&quot;, title = &quot;2013 Median Rent\nCensus Tracts&quot;, legend=&quot;Dollars&quot;, reference_map = TRUE)

</pre>
<p><a target="_blank" rel="nofollow" href="http://www.arilamstein.com/?feed-stats-url=aHR0cDovL3d3dy5hcmlsYW1zdGVpbi5jb20vd3AtY29udGVudC91cGxvYWRzLzIwMTcvMDQvbnktdHJhY3QtZnVsbC1yZWYucG5n&feed-stats-url-post-id=5563"><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/ny-tract-full-ref.png?fit=642%2C371" data-orig-size="642,371" data-image-title="ny-tract-full-ref" data-comments-opened="0" class="wp-image-5589" data-recalc-dims="1" data-attachment-id="5589" data-permalink="http://www.arilamstein.com/blog/2017/04/17/choroplethr-v3-6-0-now-cran/ny-tract-full-ref/" alt="" data-medium-file="https://i0.wp.com/www.arilamstein.com/wp-content/uploads/2017/04/ny-tract-full-ref.png?fit=300%2C173" data-large-file="https://i0.wp.com/www.arilamstein.com/wp-content/uploads/2017/04/ny-tract-full-ref.png?fit=642%2C371" 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/ny-tract-full-ref.png?resize=642%2C371"></a></p>
<p> </p>
<h2>Zooming in</h2>
<p>Tract maps of an entire state are hard to view because the tracts are so small. This is why all tract-related functions in choroplethr allow you to zoom by county.</p>
<p>In addition to being useful for viewing maps, the county-zoom option is useful for <strong>get_tract_demographics</strong> because getting tract-level demographics for an entire state is slow.</p>
<p>Note that counties must be specified by their county FIPS code. Here’s an example of zooming in on Manhattan (FIPS code 36061):</p>
<pre>

# 36061 is the FIPS code for New York county (i.e. Manhattan)
manhattan_2010 = get_tract_demographics(&quot;new york&quot;, county_fips=36061, endyear = 2010, span = 5)
manhattan_2010$value = manhattan_2010$median_rent

m1 = tract_choropleth(manhattan_2010, &quot;new york&quot;, legend = &quot;Dollars&quot;, county_zoom = 36061)
m2 = tract_choropleth(manhattan_2010, &quot;new york&quot;, legend = &quot;Dollars&quot;, county_zoom = 36061, reference_map = TRUE)

?double_map
double_map(m1, m2, &quot;2010 Median Rent\nManhattan Census Tracts&quot;)

</pre>
<p><a target="_blank" rel="nofollow" href="http://www.arilamstein.com/?feed-stats-url=aHR0cDovL3d3dy5hcmlsYW1zdGVpbi5jb20vd3AtY29udGVudC91cGxvYWRzLzIwMTcvMDQvbWFuaGF0dGFuLWRvdWJsZS5wbmc%3D&feed-stats-url-post-id=5563"><img sizes="(max-width: 600px) 100vw, 600px" data-image-description="" data-orig-file="https://i0.wp.com/www.arilamstein.com/wp-content/uploads/2017/04/manhattan-double.png?fit=600%2C408" data-orig-size="600,408" data-image-title="manhattan-double" data-comments-opened="0" class="wp-image-5568" data-recalc-dims="1" data-attachment-id="5568" data-permalink="http://www.arilamstein.com/blog/2017/04/17/choroplethr-v3-6-0-now-cran/manhattan-double/" alt="" data-medium-file="https://i0.wp.com/www.arilamstein.com/wp-content/uploads/2017/04/manhattan-double.png?fit=300%2C204" data-large-file="https://i0.wp.com/www.arilamstein.com/wp-content/uploads/2017/04/manhattan-double.png?fit=600%2C408" 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/manhattan-double.png?resize=600%2C408"></a></p>
<h2>New function: double_map</h2>
<p>As the above example shows, v3.6.0 introduces a new function: <strong>double_map</strong>.</p>
<p>In my own work I find that frequently switch between viewing a pure choropleth (where the color contrast is strong) and a choropleth + reference map (where the reference map helps you understand what you’re looking at). <strong>double_map </strong>simply encapsulates some code for putting two maps side-by-side</p>
<pre></pre>
<h2>Caveats</h2>
<p>There are a few things to keep in mind when using this functionality:</p>
<ol>
<li><strong>get_tract_demographics</strong> returns data from the American Community Survey (ACS). The ACS provides estimates with a margin of error (MOE). In the case of tracts, the MOE can be quite large. <strong>tract_choropleth</strong> does not attempt to visualize the MOE.</li>
<li><strong>get_tract_demographics </strong>sometimes produces the warning “NAs introduced by coercion”. I assume that this occurs when the ACS simply does not return an estimated value for a tract. (E.g. a sparsely populated, or unpopulated tract). However, I have not investigated this.</li>
<li><strong>tract_choropleth</strong> sometimes produces the warning “Your data.frame contains the following regions which are not mappable”. This is a bit of a mystery for me. It indicates that <strong>get_tract_demographics</strong> (which gets data from the Census API) contains regions which are not part of a map (that also comes from the Census API).</li>
<li><strong>tract_choropleth</strong> sometimes produces the warning “The following regions were missing and are being set to NA”. Presumably these regions are the same regions as in (2).</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/17/choroplethr-v3-6-0-now-cran/#content_upgrade_popup_5608">Click Here</a></div>
</div>
<p> <img height="1" width="1" src="http://www.arilamstein.com/?feed-stats-post-id=5563">
<p>The post <a target="_blank" rel="nofollow" href="http://www.arilamstein.com/blog/2017/04/17/choroplethr-v3-6-0-now-cran/">Choroplethr v3.6.0 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/17/choroplethr-v3-6-0-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/f8Pf_XpDyKA"></a></div>