<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/mapping-to-a-tmap/"></div></div><div></div></div><div></div></div>
<div>
(This article was first published on <strong><a href="http://johnmackintosh.com/2017-09-01-easy-maps-with-tmap/"> HighlandR</a></strong>, and kindly contributed to <a rel="nofollow" href="https://www.r-bloggers.com/">R-bloggers)</a> </div>
<p>	tmap &lt;-Easy &amp; Interactive - <p>More maps of the Highlands?</p>
<p>Yep, same as last time, but no need to install dev versions of anything, we can get awesome maps courtesy of the <a rel="nofollow" href="https://cran.r-project.org/package=tmap" target="_blank">tmap</a> package.</p>
<p>Get the <a rel="nofollow" href="http://sedsh127.sedsh.gov.uk/Atom_data/ScotGov/ZippedShapefiles/SG_SIMD_2016.zip" target="_blank">shapefile</a> from the <a rel="nofollow" href="http://johnmackintosh.com/2017-08-22-simply-mapping/" target="_blank">last post</a></p>
<div>
<pre><code>
<span>library</span><span>(</span><span>tmap</span><span>)</span>
<span>library</span><span>(</span><span>tmaptools</span><span>)</span>
<span>library</span><span>(</span><span>viridis</span><span>)</span>

<span>scot</span> <span>&lt;-</span> <span>read_shape</span><span>(</span><span>&quot;SG_SIMD_2016.shp&quot;</span><span>,</span> <span>as.sf</span> <span>=</span> <span>TRUE</span><span>)</span>
<span>highland</span> <span>&lt;-</span> <span>(</span><span>scot</span><span>[</span><span>scot</span><span>$</span><span>LAName</span><span>==</span><span>&quot;Highland&quot;</span><span>,</span> <span>])</span>


<span>#replicate plot from previous blog post:
</span>
<span>quint</span> <span>&lt;-</span> <span>tm_shape</span><span>(</span><span>highland</span><span>)</span> <span>+</span>
  <span>tm_fill</span><span>(</span><span>col</span> <span>=</span> <span>&quot;Quintile&quot;</span><span>,</span>
          <span>palette</span> <span>=</span> <span>viridis</span><span>(</span><span>n</span><span>=</span><span>5</span><span>,</span> <span>direction</span> <span>=</span> <span>-1</span><span>,</span><span>option</span> <span>=</span> <span>&quot;C&quot;</span><span>),</span>
          <span>fill.title</span> <span>=</span> <span>&quot;Quintile&quot;</span><span>,</span>
          <span>title</span> <span>=</span> <span>&quot;SIMD 2016 - Highland Council Area by Quintile&quot;</span><span>)</span>

<span>quint</span> <span># plot
</span>
<span>ttm</span><span>()</span> <span>#switch between static and interactive - this will use interactive
</span><span>quint</span> <span># or use last_map()
# in R Studio you will find leaflet map in your Viewer tab
</span>
<span>ttm</span><span>()</span> <span># return to plotting
</span>
</code></pre>
</div>
<p>The results:</p>
<p><img alt="tmap-Highland-SIMD-Quintile.png" src="https://i1.wp.com/johnmackintosh.com/img/tmap-Highland-SIMD-Quintile.png?w=456" data-recalc-dims="1"></p>
<p>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.</p>
<p>A static image of the leaflet map:</p>
<p><img alt="leaflet-snapshot.png" src="https://i1.wp.com/johnmackintosh.com/img/leaflet-snapshot.png?w=456" data-recalc-dims="1"></p>
<p>Here I take the rank for all the Highland data zones, and the overall SIMD rank, and create a small multiple</p>
<div>
<pre><code><span>small_mult</span><span>&lt;-</span> <span>tm_shape</span><span>(</span><span>highland</span><span>)</span> <span>+</span>
  <span>tm_fill</span><span>(</span><span>col</span> <span>=</span> <span>c</span><span>(</span><span>&quot;IncRank&quot;</span><span>,</span><span>&quot;EmpRank&quot;</span><span>,</span><span>&quot;HlthRank&quot;</span><span>,</span><span>&quot;EduRank&quot;</span><span>,</span>
                  <span>&quot;GAccRank&quot;</span><span>,</span><span>&quot;CrimeRank&quot;</span><span>,</span><span>&quot;HouseRank&quot;</span><span>,</span><span>&quot;Rank&quot;</span><span>),</span>
          <span>palette</span> <span>=</span> <span>viridis</span><span>(</span><span>n</span><span>=</span><span>5</span><span>,</span> <span>direction</span> <span>=</span> <span>-1</span><span>,</span><span>option</span> <span>=</span> <span>&quot;C&quot;</span><span>),</span>
          <span>title</span><span>=</span><span>c</span><span>(</span><span>&quot;Income Rank&quot;</span><span>,</span> <span>&quot;Employment Rank&quot;</span><span>,</span><span>&quot;Health Rank&quot;</span><span>,</span><span>&quot;Education Rank&quot;</span><span>,</span>
                  <span>&quot;General Access Rank&quot;</span><span>,</span><span>&quot;Crime Rank&quot;</span><span>,</span> <span>&quot;Housing Rank&quot;</span><span>,</span><span>&quot;Overall Rank&quot;</span><span>))</span>
<span>small_mult</span>

</code></pre>
</div>
<p><img alt="2017-08-31-tmap-Highland-SIMD-All-Domains-Ranked.png" src="https://i2.wp.com/johnmackintosh.com/img/2017-08-31-tmap-Highland-SIMD-All-Domains-Ranked.png?w=456" data-recalc-dims="1"></p>
<p>Let’s take a look at Scotland as a whole, as I assume everyone’s pretty bored of the Highlands by now:</p>
<div>
<pre><code><span>#try a map of scotland
</span><span>scotplot</span> <span>&lt;-</span> <span>tm_shape</span><span>(</span><span>scot</span><span>)</span> <span>+</span>
  <span>tm_fill</span><span>(</span><span>col</span> <span>=</span> <span>&quot;Rank&quot;</span><span>,</span>
          <span>palette</span> <span>=</span> <span>viridis</span><span>(</span><span>n</span><span>=</span><span>5</span><span>,</span> <span>direction</span> <span>=</span> <span>-1</span><span>,</span><span>option</span> <span>=</span> <span>&quot;C&quot;</span><span>),</span>
          <span>fill.title</span> <span>=</span> <span>&quot;Overall Rank&quot;</span><span>,</span>
          <span>title</span> <span>=</span> <span>&quot;Overall-Rank&quot;</span><span>)</span>
<span>scotplot</span> <span># bit of a monster
</span>         
</code></pre>
</div>
<p><img alt="2017-08-31ScotSIMD.png" src="https://i1.wp.com/johnmackintosh.com/img/2017-08-31ScotSIMD.png?w=456" data-recalc-dims="1"></p>
<p>With the interactive plot, we can really appreciate the density of these datazones in the Central belt.</p>
<p><img alt="2017-08-31-scotland-leaflet.PNG" src="https://i1.wp.com/johnmackintosh.com/img/2017-08-31-scotland-leaflet.PNG?w=456" data-recalc-dims="1"></p>
<p>Here, I’ve zoomed in a bit on the region around Glasgow, and then zoomed in some more:</p>
<p><img alt="2017-08-31-scotland-leaflet-zoom-in1.PNG" src="https://i0.wp.com/johnmackintosh.com/img/2017-08-31-scotland-leaflet-zoom-in1.PNG?w=456" data-recalc-dims="1"></p>
<p><img alt="2017-08-31-scotland-leaflet-zoom.PNG" src="https://i0.wp.com/johnmackintosh.com/img/2017-08-31-scotland-leaflet-zoom.PNG?w=456" data-recalc-dims="1"></p>
<p>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 <img alt="🙂" src="https://s.w.org/images/core/emoji/2/72x72/1f642.png" class="wp-smiley"> ) but, given the size of the file, I very much doubt I could have uploaded it to Github anyway.</p>
<p>Thanks to Roger Bivand <a rel="nofollow" href="https://twitter.com/RogerBivand" target="_blank">(@RogerBivand)</a> 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.</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="http://johnmackintosh.com/2017-09-01-easy-maps-with-tmap/"> HighlandR</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/CItdMJIh3YM" height="1"></a></div>