Adding motion to choropleths

[This article was first published on Jkunst - R category, and kindly contributed to R-bloggers]. (You can report issue about the content on this page here)
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.

Time ago @hrbrmstr show how to replicate a visualization made by
New York Times
with R.

The result we hope is like this:

I really like small multiples and this is a good example of usage. However if the
multiples means a lot mini plots maybe you can try add animation.

Let’s start using the script made by Bob:

<span class="n">URL</span> <span class="o"><-</span> <span class="s2">"http://graphics8.nytimes.com/newsgraphics/2016/01/15/drug-deaths/c23ba79c9c9599a103a8d60e2329be1a9b7d6994/data.json"</span>

<span class="n">data</span><span class="p">(</span><span class="s2">"uscountygeojson"</span><span class="p">)</span>
<span class="n">data</span><span class="p">(</span><span class="s2">"unemployment"</span><span class="p">)</span>

<span class="n">data</span> <span class="o"><-</span>  <span class="n">fromJSON</span><span class="p">(</span><span class="n">URL</span><span class="p">)</span> <span class="o">%>%</span> 
  <span class="n">tbl_df</span><span class="p">()</span> <span class="o">%>%</span> 
  <span class="n">gather</span><span class="p">(</span><span class="n">year</span><span class="p">,</span> <span class="n">value</span><span class="p">,</span> <span class="o">-</span><span class="n">fips</span><span class="p">)</span> <span class="o">%>%</span> 
  <span class="n">mutate</span><span class="p">(</span><span class="n">year</span> <span class="o">=</span> <span class="n">sub</span><span class="p">(</span><span class="s2">"^y"</span><span class="p">,</span> <span class="s2">""</span><span class="p">,</span> <span class="n">year</span><span class="p">),</span>
         <span class="n">value</span> <span class="o">=</span> <span class="n">ifelse</span><span class="p">(</span><span class="n">is.na</span><span class="p">(</span><span class="n">value</span><span class="p">),</span> <span class="m">0</span><span class="p">,</span> <span class="n">value</span><span class="p">))</span>

<span class="n">head</span><span class="p">(</span><span class="n">data</span><span class="p">)</span>
fips year value
01001 2002 1
01003 2002 2
01005 2002 0
01007 2002 1
01009 2002 2
01011 2002 0

Now we’ll prepare the data as the motion plugin
require the data.

<span class="n">ds</span> <span class="o"><-</span> <span class="n">data</span> <span class="o">%>%</span> 
  <span class="n">group_by</span><span class="p">(</span><span class="n">fips</span><span class="p">)</span> <span class="o">%>%</span> 
  <span class="n">do</span><span class="p">(</span><span class="n">item</span> <span class="o">=</span> <span class="n">list</span><span class="p">(</span>
    <span class="n">fips</span> <span class="o">=</span> <span class="n">first</span><span class="p">(</span><span class="err">.</span><span class="o">$</span><span class="n">fips</span><span class="p">),</span>
    <span class="n">sequence</span> <span class="o">=</span> <span class="err">.</span><span class="o">$</span><span class="n">value</span><span class="p">,</span>
    <span class="n">value</span> <span class="o">=</span> <span class="n">first</span><span class="p">(</span><span class="err">.</span><span class="o">$</span><span class="n">value</span><span class="p">)))</span> <span class="o">%>%</span> 
  <span class="err">.</span><span class="o">$</span><span class="n">item</span>

<span class="n">hc</span> <span class="o"><-</span> <span class="n">highchart</span><span class="p">(</span><span class="n">type</span> <span class="o">=</span> <span class="s2">"map"</span><span class="p">)</span> <span class="o">%>%</span> 
  <span class="n">hc_add_series</span><span class="p">(</span><span class="n">data</span> <span class="o">=</span> <span class="n">ds</span><span class="p">,</span>
                <span class="n">name</span> <span class="o">=</span> <span class="s2">"drug deaths per 100,000"</span><span class="p">,</span>
                <span class="n">mapData</span> <span class="o">=</span> <span class="n">uscountygeojson</span><span class="p">,</span>
                <span class="n">joinBy</span> <span class="o">=</span> <span class="s2">"fips"</span><span class="p">,</span>
                <span class="n">borderWidth</span> <span class="o">=</span> <span class="m">0.01</span><span class="p">)</span> <span class="o">%>%</span> 
  <span class="n">hc_colorAxis</span><span class="p">(</span><span class="n">stops</span> <span class="o">=</span> <span class="n">color_stops</span><span class="p">())</span> <span class="o">%>%</span>  
  <span class="n">hc_title</span><span class="p">(</span><span class="n">text</span> <span class="o">=</span> <span class="s2">"How the Epidemic of Drug Overdose Deaths Ripples"</span><span class="p">)</span> <span class="o">%>%</span> 
  <span class="n">hc_legend</span><span class="p">(</span><span class="n">layout</span> <span class="o">=</span> <span class="s2">"vertical"</span><span class="p">,</span> <span class="n">reversed</span> <span class="o">=</span> <span class="n">TRUE</span><span class="p">,</span>
            <span class="n">floating</span> <span class="o">=</span> <span class="n">TRUE</span><span class="p">,</span> <span class="n">align</span> <span class="o">=</span> <span class="s2">"right"</span><span class="p">)</span> <span class="o">%>%</span> 
  <span class="n">hc_add_theme</span><span class="p">(</span><span class="n">hc_theme_smpl</span><span class="p">())</span> <span class="o">%>%</span> 
  <span class="n">hc_motion</span><span class="p">(</span>
    <span class="n">enabled</span> <span class="o">=</span> <span class="n">TRUE</span><span class="p">,</span>
    <span class="n">axisLabel</span> <span class="o">=</span> <span class="s2">"year"</span><span class="p">,</span>
    <span class="n">labels</span> <span class="o">=</span> <span class="n">sort</span><span class="p">(</span><span class="n">unique</span><span class="p">(</span><span class="n">data</span><span class="o">$</span><span class="n">year</span><span class="p">)),</span>
    <span class="n">series</span> <span class="o">=</span> <span class="m">0</span><span class="p">,</span>
    <span class="n">updateIterval</span> <span class="o">=</span> <span class="m">50</span><span class="p">,</span>
    <span class="n">magnet</span> <span class="o">=</span> <span class="n">list</span><span class="p">(</span>
      <span class="n">round</span> <span class="o">=</span> <span class="s2">"floor"</span><span class="p">,</span>
      <span class="n">step</span> <span class="o">=</span> <span class="m">0.1</span>
    <span class="p">)</span>
  <span class="p">)</span>

And the result:

<span class="n">hc</span>

open

I like it!

Note this using the development version of highcarter
using the motion plugin.

To leave a comment for the author, please follow the link and comment on their blog: Jkunst - R category.

R-bloggers.com offers daily e-mail updates about R news and tutorials about learning R and many other topics. Click here if you're looking to post or find an R/data-science job.
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.

Never miss an update!
Subscribe to R-bloggers to receive
e-mails with the latest R posts.
(You will not see this message again.)

Click here to close (This popup will not appear again)