<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/update-difficult-to-reproduce-choroplethr-bug-isolated-and-fixed/"></div></div><div></div></div><div></div></div>
<div>
(This article was first published on <strong><a href="https://arilamstein.com/blog/2018/02/21/update-difficult-reproduce-choroplethr-bug-isolated-fixed/"> R – AriLamstein.com</a></strong>, and kindly contributed to <a rel="nofollow" href="https://www.r-bloggers.com/">R-bloggers)</a> </div>
<p>Yesterday I <a rel="nofollow" href="https://arilamstein.com/?feed-stats-url=aHR0cHM6Ly9hcmlsYW1zdGVpbi5jb20vYmxvZy8yMDE4LzAyLzIwL2RpZmZpY3VsdC1yZXByb2R1Y2UtY2hvcm9wbGV0aHItYnVnLWNhbi1oZWxwLw%3D%3D&feed-stats-url-post-id=6999" target="_blank">wrote</a> that I’ve received a number of choroplethr bug reports recently that I simply cannot reproduce. Due to a large number of people replying with their system information, along with whether or not they were able to reproduce the bug, I was able to track it down. Thank you to everyone who helped!</p>
<p>It appears that the issue only occurs when choroplethr is used in conjunction with the <strong>development</strong> version of ggplot2. I personally never use the development version of ggplot2, which explains why I was never able to reproduce the bug.</p>
<p>Normally I would not update choroplethr to work better with the development version of another package. But I received a lot of help to isolate this issue, so I decided to submit a fix to github. You can get it by typing the following:</p>
<pre># install the &quot;ggplot2-dev-fix&quot; branch of choroplethr
library(devtools)
install_github(&quot;arilamstein/choroplethr@ggplot2-dev-fix&quot;)

# verify that this version works with your version of ggplot2
library(choroplethr)
library(choroplethrMaps)

data(df_pop_state)
state_choropleth(df_pop_state)</pre>
<p>If you were able to reproduce the bug before, I would appreciate it if you could retest with this version and verify that everything now works for you.</p>
<h3>Technical Details</h3>
<p>In order to create a map of the 50 US States, choroplethr first renders a map of the continental United States. It then individually renders Alaska and Hawaii, and affixes them to the continental US map as annotations. You can view the entire process <a rel="nofollow" href="https://arilamstein.com/?feed-stats-url=aHR0cHM6Ly9naXRodWIuY29tL2FyaWxhbXN0ZWluL2Nob3JvcGxldGhyL2Jsb2IvbWFzdGVyL1IvdXNhLlIjTDIw&feed-stats-url-post-id=6999" target="_blank">here</a>. A key function in that process is this line:</p>
<pre><span>alaska.grob</span> <span>=</span> ggplotGrob(<span>alaska.ggplot</span>)</pre>
<p>With the development version of ggplot2, the call to ggplotGrob fails. It appears that the failure is due to changes in how the development version of ggplot2 handles themes. Choroplethr uses two custom themes:</p>
<ul>
<li>theme_clean hides hides everything except the map and legend.</li>
<li>theme_inset acts like theme_clean but also hides the legend.</li>
</ul>
<p>You can view the choroplethr theme code <a rel="nofollow" href="https://arilamstein.com/?feed-stats-url=aHR0cHM6Ly9naXRodWIuY29tL2FyaWxhbXN0ZWluL2Nob3JvcGxldGhyL2Jsb2IvbWFzdGVyL1IvY2hvcm9wbGV0aC5SI0wyNDc%3D&feed-stats-url-post-id=6999" target="_blank">here</a>.</p>
<p>I’m not an expert on these new changes to ggplot2, but it appears that ggplotGrob now wants a theme with all member variables specified. It also appears that there is a new built-in theme called theme_void that does everything my theme_clean does while also setting all of the theme member variables. (This means that calling ggplotGrob with theme_void does not lead to a crash). I used theme_void as the basis of my fix. You can see the specific code change <a rel="nofollow" href="https://arilamstein.com/?feed-stats-url=aHR0cHM6Ly9naXRodWIuY29tL2FyaWxhbXN0ZWluL2Nob3JvcGxldGhyL2NvbW1pdC8xOTdkNzk0NzIwNjdlM2FiMTJjNGM1OGNlNDM0NGJiYjcxZjA4ZTY1&feed-stats-url-post-id=6999" target="_blank">here</a>.</p>
<p>(As a note to myself, it now appears that using one of the <a rel="nofollow" href="https://arilamstein.com/?feed-stats-url=aHR0cHM6Ly93d3cucmRvY3VtZW50YXRpb24ub3JnL3BhY2thZ2VzL2dncGxvdDIvdmVyc2lvbnMvMi4yLjEvdG9waWNzL3RoZW1lX2dldA%3D%3D&feed-stats-url-post-id=6999" target="_blank">theme_replace</a> functions would simplify the code of my new theme_inset code. I am intentionally holding off on this change, though, until I know whether or not this will even be an issue in the next CRAN version of ggplot2).</p>
<p> <img width="1" src="https://arilamstein.com/?feed-stats-post-id=6999" height="1"></p>
<p>The post <a rel="nofollow" href="https://arilamstein.com/blog/2018/02/21/update-difficult-reproduce-choroplethr-bug-isolated-fixed/" target="_blank">Update: “Difficult to Reproduce Choroplethr Bug” Isolated and Fixed</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/02/21/update-difficult-reproduce-choroplethr-bug-isolated-fixed/"> 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 width="1" alt="" src="http://feeds.feedburner.com/~r/RBloggers/~4/DO9pwOMtf1Q" height="1"></a></div>