<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/choroplethr-v3-6-2-is-now-on-cran/"></div></div><div></div></div><div></div></div>
<div>
(This article was first published on <strong><a href="https://arilamstein.com/blog/2018/06/27/choroplethr-v3-6-2-is-now-on-cran/"> R – AriLamstein.com</a></strong>, and kindly contributed to <a rel="nofollow" href="https://www.r-bloggers.com/">R-bloggers)</a> </div>
<p>A new version of Choroplethr is now on CRAN! You can get it by typing the following from the R Console:</p>

<pre>install.packages(&quot;choroplethr&quot;)</pre>

<p>I encourage all Choroplethr users to install this new version!</p>
<h1>Overview</h1>
<p>A new version of ggplot2 will be released shortly. This new version of ggplot2 introduces many new features. These new features, however, come at a cost: some packages which use ggplot2 (such as Choroplethr) are not compatible with this new version.</p>
<p>In fact, if you try to draw a map with the new version of ggplot2 and the old version of Choroplethr, you might get an error. This new version of Chorplethr (v3.6.2) fixes those issues.</p>
<h1>Technical Details</h1>
<p>Choroplethr was one of (if not the) first R package to create maps of the US where Alaska and Hawaii are rendered as insets. Here’s an example map, along with code:</p>

<pre>library(choroplethr)

data(df_pop_state)

state_choropleth(df_pop_state)</pre>

<p><a rel="nofollow" href="https://arilamstein.com/?feed-stats-url=aHR0cHM6Ly9hcmlsYW1zdGVpbi5jb20vd3AtY29udGVudC91cGxvYWRzLzIwMTgvMDYvUnBsb3QucG5n&feed-stats-url-post-id=7169" target="_blank"><img data-orig-file="https://i1.wp.com/arilamstein.com/wp-content/uploads/2018/06/Rplot.png?fit=492%2C295&amp;ssl=1" data-orig-size="492,295" data-medium-file="https://i1.wp.com/arilamstein.com/wp-content/uploads/2018/06/Rplot.png?fit=300%2C180&amp;ssl=1" data-attachment-id="7170" data-image-meta="{" data-image-description="" src="https://i1.wp.com/arilamstein.com/wp-content/uploads/2018/06/Rplot.png?resize=492%2C295&ssl=1" data-permalink="https://arilamstein.com/blog/2018/06/27/choroplethr-v3-6-2-is-now-on-cran/rplot-2/" data-large-file="https://i1.wp.com/arilamstein.com/wp-content/uploads/2018/06/Rplot.png?fit=492%2C295&amp;ssl=1" alt="" data-image-title="Rplot" sizes="(max-width: 492px) 100vw, 492px" width="450" data-comments-opened="0" class="wp-image-7170" data-recalc-dims="1"></a></p>
<p>The new version of ggplot2 was causing Choroplethr to crash whenever it attempted to render the above map. In order to understand the underlying issue, it is helpful to first understand how Choroplethr creates the above map.</p>
<h1>Insets and ?annotation_custom</h1>
<p>The most important thing to notice about the above map is this: it is inaccurate.</p>
<p>Alaska and Hawaii are not <em>really </em>located where they appear. And they are not drawn to scale, either.</p>
<p>Here is how the 50 US states “really” look:</p>

<pre>library(choroplethrMaps)
library(ggplot2)

data(state.map)
ggplot(state.map, aes(long, lat, group=group)) + geom_polygon()</pre>

<p><a rel="nofollow" href="https://arilamstein.com/?feed-stats-url=aHR0cHM6Ly9hcmlsYW1zdGVpbi5jb20vd3AtY29udGVudC91cGxvYWRzLzIwMTgvMDYvUnBsb3QtMS5wbmc%3D&feed-stats-url-post-id=7169" target="_blank"><img data-orig-file="https://i0.wp.com/arilamstein.com/wp-content/uploads/2018/06/Rplot-1.png?fit=419%2C303&amp;ssl=1" data-orig-size="419,303" data-medium-file="https://i0.wp.com/arilamstein.com/wp-content/uploads/2018/06/Rplot-1.png?fit=300%2C217&amp;ssl=1" data-attachment-id="7174" data-image-meta="{" data-image-description="" src="https://i0.wp.com/arilamstein.com/wp-content/uploads/2018/06/Rplot-1.png?resize=419%2C303&ssl=1" data-permalink="https://arilamstein.com/blog/2018/06/27/choroplethr-v3-6-2-is-now-on-cran/rplot-3/" data-large-file="https://i0.wp.com/arilamstein.com/wp-content/uploads/2018/06/Rplot-1.png?fit=419%2C303&amp;ssl=1" alt="" data-image-title="Rplot" sizes="(max-width: 419px) 100vw, 419px" width="419" data-comments-opened="0" class="wp-image-7174" data-recalc-dims="1" height="303"></a></p>
<p>In order to render Alaska and Hawaii as insets, Choroplethr:</p>
<ol>
<li>First pre-renders the Continental US, Alaska and Hawaii as separate ggplot objects.</li>
<li>Then affixes the Alaska and Hawaii ggplot objects to the continental US object using ggplot2’s <strong>ggplotGrob </strong>and <strong>annotation_custom </strong>functions.</li>
</ol>
<h1>Insets and Themes</h1>
<p>The two images differ in more than just the placement of Alaska and Hawaii, though. The first map also has a clear background, whereas the second map has a grey background and tick marks for longitude and latitude. Also, Choroplethr uses a single legend for all three maps (the legends for Alaska and Hawaii are suppressed).</p>
<p>Traditionally Choroplethr has implemented these aesthetic tweaks in the functions <strong>theme_clean</strong> (for the Continental US) and <strong>theme_inset </strong>(for Alaska and Hawaii). The code for these functions originally came from Section 13.19 (“Making a Map with a Clean Background”) of Winston Chang’s <em>R Graphics Cookbook. </em>Here’s what that code used to look like:</p>

<pre>theme_clean = function()
    {
      theme(
        axis.title        = element_blank(),
        axis.text         = element_blank(),
        panel.background  = element_blank(),
        panel.grid        = element_blank(),
        axis.ticks.length = unit(0, &quot;cm&quot;),
        panel.spacing     = unit(0, &quot;lines&quot;),
        plot.margin       = unit(c(0, 0, 0, 0), &quot;lines&quot;),
        complete          = TRUE
      )
    },
    
    theme_inset = function()
    {
      theme(
        legend.position   = &quot;none&quot;,
        axis.title        = element_blank(),
        axis.text         = element_blank(),
        panel.background  = element_blank(),
        panel.grid        = element_blank(),
        axis.ticks.length = unit(0, &quot;cm&quot;),
        panel.spacing     = unit(0, &quot;lines&quot;),
        plot.margin       = unit(c(0, 0, 0, 0), &quot;lines&quot;),
        complete          = TRUE
      )
    }</pre>

<h1>The Bug</h1>
<p>The bug was happening when Choroplethr was attempting to affix the map of Alaska, with <strong>theme_inset</strong> applied to it, to the map of the Continental US:</p>

<pre># omit code for creating continental.ggplot
ret = continental.ggplot

# omit code for creating alaska.ggplot

# add alaska.ggplot to ret using ggplotGrob and annotation_custom
alaska.grob = ggplotGrob(alaska.ggplot)
ret = ret + annotation_custom(grobTree(alaska.grob), xmin=-125, xmax=-110, ymin=22.5, ymax=30)</pre>

<p>The last line in the above snippet was causing Choroplethr to crash. It appears that the intersection of <strong>theme_inset</strong>, <strong>ggplotGrob</strong> and <strong>annotation_custom</strong> was causing a problem.</p>
<h1>The Fix</h1>
<p>It appears that ggplot2 has a new function for creating a clear background on a map that does not cause these problems. That function is <strong>theme_void</strong>. Modifying <strong>theme_clean</strong> and <strong>theme_inset </strong>to use <strong>theme_void</strong> solved the problem. Here is the new version of those functions:</p>

<pre>theme_clean = function()
{
    ggplot2::theme_void()
}

theme_inset = function()
{
    ggplot2::theme_void() %+replace%
        ggplot2::theme(legend.position = &quot;none&quot;)
}</pre>

<p> <img width="1" src="https://arilamstein.com/?feed-stats-post-id=7169" height="1"></p>
<p>The post <a rel="nofollow" href="https://arilamstein.com/blog/2018/06/27/choroplethr-v3-6-2-is-now-on-cran/" target="_blank">Choroplethr v3.6.2 is now on CRAN</a> appeared first on <a rel="nofollow" href="https://arilamstein.com/" target="_blank">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="https://arilamstein.com/blog/2018/06/27/choroplethr-v3-6-2-is-now-on-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 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 alt="" width="1" src="http://feeds.feedburner.com/~r/RBloggers/~4/1MB-UNIMM5c" height="1"></a></div>