<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/statebins-reimagined/"></div></div><div></div></div><div></div></div>
<div>
(This article was first published on <strong><a href="https://rud.is/b/2017/11/18/statebins-reimagined/"> R – rud.is</a></strong>, and kindly contributed to <a rel="nofollow" href="https://www.r-bloggers.com/">R-bloggers)</a> </div>
<p>A long time ago, in a github repo far, far away there lived a tiny package that made it possible to create equal area, square U.S. state cartograms in R dubbed <a rel="nofollow" href="https://github.com/hrbrmstr/statebins" target="_blank"><code>statebins</code><img alt="🔗" src="https://i1.wp.com/s.w.org/images/core/emoji/2.3/72x72/1f517.png?w=456&ssl=1" class="wp-smiley" data-recalc-dims="1"></a>. Three years have come and gone and — truth be told — I’ve never been happy with that package. It never felt “right” and that gnawing feeling finally turned into action with a “re-imagining” of the API.</p>
<h3>Previously, on <code>statebins</code>…</h3>
<p>There were three different functions in the old-style package:</p>
<ul>
<li>one for discrete scales (it automated ‘cuts’)</li>
<li>one for continuous scales</li>
<li>one for manual scales</li>
</ul>
<p>It also did some hack-y stuff with <code>grobs</code> to try to get things to look good without putting too much burden on the user.</p>
<p>All that “mostly” worked, but I always ended up doing some painful workaround when I needed more custom charts (not that I have to use this package much given the line of work I’m in).</p>
<h3>Downsizing <code>statebins</code></h3>
<p>Now, there’s just <em>one</em> function for making the cartograms — <code>statebins()</code> — and another for applying a base theme — <code>theme_statebins()</code>. The minimalisation has some advantages that we’ll take a look at now, starting with the most basic example (the one on the manual page):</p>
<pre><code>data(USArrests)

USArrests$state &lt;- rownames(USArrests)
statebins(USArrests, value_col=&quot;Assault&quot;, name = &quot;Assault&quot;) +
  theme_statebins(legend_position=&quot;right&quot;)</code></pre>
<p><a rel="nofollow" href="https://i0.wp.com/rud.is/b/wp-content/uploads/2017/11/plot_zoom_png-3.png?ssl=1" target="_blank"><img alt="" src="https://i0.wp.com/rud.is/b/wp-content/uploads/2017/11/plot_zoom_png-3.png?resize=780%2C468&ssl=1" class="wp-image-7150" data-recalc-dims="1"></a></p>
<p>Two things should stand out there:</p>
<ul>
<li>you got <code>scale_fill_distiller()</code> for free!</li>
<li>labels are dark/light depending on the tile color</li>
</ul>
<p>Before we go into ^^, it may be helpful to show the new function interface:</p>
<pre><code>statebins(state_data, state_col = &quot;state&quot;, value_col = &quot;value&quot;,
  dark_label = &quot;black&quot;, light_label = &quot;white&quot;, font_size = 3,
  state_border_col = &quot;white&quot;, state_border_size = 2,
  ggplot2_scale_function = ggplot2::scale_fill_distiller, ...)</code></pre>
<p>You pass in the state name/abbreviation &amp; value columns like the old interface but also specify colors for the dark &amp; light labels (set hex code color with <code>00</code> ending alpha values if you don’t want labels but Muricans are pretty daft and generally need the abbreviations on the squares). You can set the font size, too (we’ll do that in a bit) and customize the border color (usually to match the background of the target medium). BUT, you <em>also</em> pass in the ggplot2 scale function you want to use and the named parameters for it (that’s what the <code>...</code> is for). </p>
<p>So, <em>yes</em> I’ve placed more of a burden on <em>you</em> if you want discrete cuts, but I’ve also made the package way more flexible <em>and</em> made it possible to keep the labels readable without you having to lift an extra coding finger.</p>
<p>The <code>theme()</code>-ing is also moved out to a separate theme function which makes it easier for you to further customize the final output.</p>
<h3>But that’s not all!</h3>
<p>There are now squares for Puerto Rico, the Virgin Islands and New York City (the latter two were primarily for new features/data in <code>cdcfluview</code> but they are good to have available). Let’s build out a larger example with some of these customizations (we’ll make up some data to do that):</p>
<pre><code>library(statebins)
library(tidyverse)
library(viridis)

data(USArrests)

# make up some data for the example

rownames_to_column(USArrests, &quot;state&quot;) %>%
  bind_rows(
    data_frame(
      state = c(&quot;Virgin Islands&quot;, &quot;Puerto Rico&quot;, &quot;New York City&quot;),
      Murder = rep(mean(max(USArrests$Murder),3)),
      Assault = rep(mean(max(USArrests$Assault),3)),
      Rape = rep(mean(max(USArrests$Rape),3)),
      UrbanPop = c(93, 95, 100)
    )
  ) -> us_arrests

statebins(us_arrests, value_col=&quot;Assault&quot;,
          ggplot2_scale_function = viridis::scale_fill_viridis) +
  labs(title=&quot;USArrests + made up data&quot;) +
  theme_statebins(&quot;right&quot;)</code></pre>
<p><a rel="nofollow" href="https://i0.wp.com/rud.is/b/wp-content/uploads/2017/11/unnamed-chunk-4-1.png?ssl=1" target="_blank"><img alt="" src="https://i0.wp.com/rud.is/b/wp-content/uploads/2017/11/unnamed-chunk-4-1.png?resize=780%2C557&ssl=1" class="wp-image-7154" data-recalc-dims="1"></a></p>
<h3>Cutting to the chase</h3>
<p>I still think it makes more sense to use binned data in these cartograms, and while you no longer get that for “free”, it’s not difficult to do:</p>
<pre><code>adat &lt;- suppressMessages(read_csv(&quot;http://www.washingtonpost.com/wp-srv/special/business/states-most-threatened-by-trade/states.csv?cache=1&quot;))

mutate(
  adat, 
  share = cut(avgshare94_00, breaks = 4, labels = c(&quot;0-1&quot;, &quot;1-2&quot;, &quot;2-3&quot;, &quot;3-4&quot;))
) %>% 
  statebins(
    value_col = &quot;share&quot;, 
    ggplot2_scale_function = scale_fill_brewer,
    name = &quot;Share of workforce with jobs lost or threatened by trade&quot;
  ) +
  labs(title = &quot;1994-2000&quot;) +
  theme_statebins()</code></pre>
<p><a rel="nofollow" href="https://i2.wp.com/rud.is/b/wp-content/uploads/2017/11/unnamed-chunk-3-1.png?ssl=1" target="_blank"><img alt="" src="https://i2.wp.com/rud.is/b/wp-content/uploads/2017/11/unnamed-chunk-3-1.png?resize=780%2C557&ssl=1" class="wp-image-7156" data-recalc-dims="1"></a></p>
<h3>More manual labor</h3>
<p>You can also still use hardcoded colors, but it’s a little more work on your end (but not much!):</p>
<pre><code>election_2012 &lt;- suppressMessages(read_csv(&quot;https://raw.githubusercontent.com/hrbrmstr/statebins/master/tmp/election2012.csv&quot;))

mutate(election_2012, value = ifelse(is.na(Obama), &quot;Romney&quot;, &quot;Obama&quot;)) %>% 
  statebins(
    font_size=4, dark_label = &quot;white&quot;, light_label = &quot;white&quot;,
    ggplot2_scale_function = scale_fill_manual,
    name = &quot;Winner&quot;,
    values = c(Romney = &quot;#2166ac&quot;, Obama = &quot;#b2182b&quot;)
  ) +
  theme_statebins()</code></pre>
<p><a rel="nofollow" href="https://i1.wp.com/rud.is/b/wp-content/uploads/2017/11/unnamed-chunk-3-6.png?ssl=1" target="_blank"><img alt="" src="https://i1.wp.com/rud.is/b/wp-content/uploads/2017/11/unnamed-chunk-3-6.png?resize=780%2C557&ssl=1" class="wp-image-7157" data-recalc-dims="1"></a></p>
<h3>BREAKING NEWS: Rounded corners</h3>
<p>A Twitter request ended up turning into a new feature this afternoon (after I made this post) => rounded corners:</p>
<pre><code>data(USArrests)

USArrests$state &lt;- rownames(USArrests)
statebins(USArrests, value_col=&quot;Assault&quot;, name = &quot;Assault&quot;, round=TRUE) +
  theme_statebins(legend_position=&quot;right&quot;)</code></pre>
<p><a rel="nofollow" href="https://i2.wp.com/rud.is/b/wp-content/uploads/2017/11/rounded-1.png?ssl=1" target="_blank"><img alt="" src="https://i2.wp.com/rud.is/b/wp-content/uploads/2017/11/rounded-1.png?resize=780%2C557&ssl=1" class="wp-image-7164" data-recalc-dims="1"></a></p>
<h3>FIN</h3>
<p>It’ll be a while before this hits CRAN and I’m not really planning on keeping the old interface when the submission happens. So, it’ll be <a rel="nofollow" href="https://github.com/hrbrmstr/statebins" target="_blank">on GitHub</a> for a bit to let folks chime in on what additional features you want and whether you really <em>need</em> to keep the deprecated functions around in the package.</p>
<p>So, kick the tyres and don’t hesitate to shoot over some feedback!</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://rud.is/b/2017/11/18/statebins-reimagined/"> R – rud.is</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/cx6NvzfYluU" height="1"></a></div>