Mapping to a ‘t'(map)

[This article was first published on HighlandR, 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.

tmap <- Easy & Interactive -

More maps of the Highlands?

Yep, same as last time, but no need to install dev versions of anything, we can get awesome maps courtesy of the tmap package.

Get the shapefile from the last post

<span class="w">
</span><span class="n">library</span><span class="p">(</span><span class="n">tmap</span><span class="p">)</span><span class="w">
</span><span class="n">library</span><span class="p">(</span><span class="n">tmaptools</span><span class="p">)</span><span class="w">
</span><span class="n">library</span><span class="p">(</span><span class="n">viridis</span><span class="p">)</span><span class="w">

</span><span class="n">scot</span><span class="w"> </span><span class="o"><-</span><span class="w"> </span><span class="n">read_shape</span><span class="p">(</span><span class="s2">"SG_SIMD_2016.shp"</span><span class="p">,</span><span class="w"> </span><span class="n">as.sf</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="kc">TRUE</span><span class="p">)</span><span class="w">
</span><span class="n">highland</span><span class="w"> </span><span class="o"><-</span><span class="w"> </span><span class="p">(</span><span class="n">scot</span><span class="p">[</span><span class="n">scot</span><span class="o">$</span><span class="n">LAName</span><span class="o">==</span><span class="s2">"Highland"</span><span class="p">,</span><span class="w"> </span><span class="p">])</span><span class="w">


</span><span class="c1">#replicate plot from previous blog post:</span><span class="w">

</span><span class="n">quint</span><span class="w"> </span><span class="o"><-</span><span class="w"> </span><span class="n">tm_shape</span><span class="p">(</span><span class="n">highland</span><span class="p">)</span><span class="w"> </span><span class="o">+</span><span class="w">
  </span><span class="n">tm_fill</span><span class="p">(</span><span class="n">col</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="s2">"Quintile"</span><span class="p">,</span><span class="w">
          </span><span class="n">palette</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">viridis</span><span class="p">(</span><span class="n">n</span><span class="o">=</span><span class="m">5</span><span class="p">,</span><span class="w"> </span><span class="n">direction</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="m">-1</span><span class="p">,</span><span class="n">option</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="s2">"C"</span><span class="p">),</span><span class="w">
          </span><span class="n">fill.title</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="s2">"Quintile"</span><span class="p">,</span><span class="w">
          </span><span class="n">title</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="s2">"SIMD 2016 - Highland Council Area by Quintile"</span><span class="p">)</span><span class="w">

</span><span class="n">quint</span><span class="w"> </span><span class="c1"># plot</span><span class="w">

</span><span class="n">ttm</span><span class="p">()</span><span class="w"> </span><span class="c1">#switch between static and interactive - this will use interactive</span><span class="w">
</span><span class="n">quint</span><span class="w"> </span><span class="c1"># or use last_map()</span><span class="w">
</span><span class="c1"># in R Studio you will find leaflet map in your Viewer tab</span><span class="w">

</span><span class="n">ttm</span><span class="p">()</span><span class="w"> </span><span class="c1"># return to plotting</span><span class="w">

</span>

The results:

tmap-Highland-SIMD-Quintile.png

One really nice thing is that because the polygons don’t have outlines, the DataZones that are really densely packed still render nicely – so no ‘missing’ areas.

A static image of the leaflet map:

leaflet-snapshot.png

Here I take the main  domain ranks (each of which summarises a number of other metrics) for all the Highland data zones, and the overall SIMD rank, and create a small multiple. The Highland region ranks very lowly compared to the rest of Scotland for access to services (e.g.average drive times to school, GP etc, and average public transport times to the same). No  surprise.

However, it looks pretty good for Health and Employment in most areas (remember the ranks are assigned to all datazones in Scotland):

<span class="n">small_mult</span><span class="o"><-</span><span class="w"> </span><span class="n">tm_shape</span><span class="p">(</span><span class="n">highland</span><span class="p">)</span><span class="w"> </span><span class="o">+</span><span class="w">
  </span><span class="n">tm_fill</span><span class="p">(</span><span class="n">col</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="nf">c</span><span class="p">(</span><span class="s2">"IncRank"</span><span class="p">,</span><span class="s2">"EmpRank"</span><span class="p">,</span><span class="s2">"HlthRank"</span><span class="p">,</span><span class="s2">"EduRank"</span><span class="p">,</span><span class="w">
                  </span><span class="s2">"GAccRank"</span><span class="p">,</span><span class="s2">"CrimeRank"</span><span class="p">,</span><span class="s2">"HouseRank"</span><span class="p">,</span><span class="s2">"Rank"</span><span class="p">),</span><span class="w">
          </span><span class="n">palette</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">viridis</span><span class="p">(</span><span class="n">n</span><span class="o">=</span><span class="m">5</span><span class="p">,</span><span class="w"> </span><span class="n">direction</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="m">-1</span><span class="p">,</span><span class="n">option</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="s2">"C"</span><span class="p">),</span><span class="w">
          </span><span class="n">title</span><span class="o">=</span><span class="nf">c</span><span class="p">(</span><span class="s2">"Income Rank"</span><span class="p">,</span><span class="w"> </span><span class="s2">"Employment Rank"</span><span class="p">,</span><span class="s2">"Health Rank"</span><span class="p">,</span><span class="s2">"Education Rank"</span><span class="p">,</span><span class="w">
                  </span><span class="s2">"General Access Rank"</span><span class="p">,</span><span class="s2">"Crime Rank"</span><span class="p">,</span><span class="w"> </span><span class="s2">"Housing Rank"</span><span class="p">,</span><span class="s2">"Overall Rank"</span><span class="p">))</span><span class="w">
</span><span class="n">small_mult</span><span class="w">

</span>

2017-08-31-tmap-Highland-SIMD-All-Domains-Ranked.png

Let’s take a look at Scotland as a whole, as I assume everyone’s pretty bored of the Highlands by now:

<span class="c1">#try a map of scotland</span><span class="w">
</span><span class="n">scotplot</span><span class="w"> </span><span class="o"><-</span><span class="w"> </span><span class="n">tm_shape</span><span class="p">(</span><span class="n">scot</span><span class="p">)</span><span class="w"> </span><span class="o">+</span><span class="w">
  </span><span class="n">tm_fill</span><span class="p">(</span><span class="n">col</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="s2">"Rank"</span><span class="p">,</span><span class="w">
          </span><span class="n">palette</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">viridis</span><span class="p">(</span><span class="n">n</span><span class="o">=</span><span class="m">5</span><span class="p">,</span><span class="w"> </span><span class="n">direction</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="m">-1</span><span class="p">,</span><span class="n">option</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="s2">"C"</span><span class="p">),</span><span class="w">
          </span><span class="n">fill.title</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="s2">"Overall Rank"</span><span class="p">,</span><span class="w">
          </span><span class="n">title</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="s2">"Overall-Rank"</span><span class="p">)</span><span class="w">
</span><span class="n">scotplot</span><span class="w"> </span><span class="c1"># bit of a monster</span><span class="w">
         
</span>

2017-08-31ScotSIMD.png

With the interactive plot, we can really appreciate the density of these datazones in the Central belt.

2017-08-31-scotland-leaflet.PNG

Here, I’ve zoomed in a bit on the region around Glasgow, and then zoomed in some more:

2017-08-31-scotland-leaflet-zoom-in1.PNG

2017-08-31-scotland-leaflet-zoom.PNG

I couldn’t figure out how to host the leaflet map within the page (Jekyll / Github / Leaflet experts please feel free to educate me on that 🙂 ) but, given the size of the file, I very much doubt I could have uploaded it to Github anyway.

Thanks to Roger Bivand (@RogerBivand) for getting in touch and pointing me towards the tmap package! It’s really good fun and an easy way to get interactive maps up and running. There are many more refinements that can be made to the plots, but you can look into those yourself ;).

Code and plots can be found here :
SIMD tmap

To leave a comment for the author, please follow the link and comment on their blog: HighlandR.

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)