<?xml version="1.0" encoding="utf-8" standalone="yes" ?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom">
  <channel>
    <title>tmap | Marcin Stępniak</title>
    <link>https://marcinstepniak.eu/tags/tmap/</link>
      <atom:link href="https://marcinstepniak.eu/tags/tmap/index.xml" rel="self" type="application/rss+xml" />
    <description>tmap</description>
    <generator>Source Themes Academic (https://sourcethemes.com/academic/)</generator><language>en-us</language><copyright>© Marcin Stepniak; 2019</copyright><lastBuildDate>Wed, 25 Sep 2019 00:00:00 +0000</lastBuildDate>
    <image>
      <url>https://marcinstepniak.eu/img/icon-192.png</url>
      <title>tmap</title>
      <link>https://marcinstepniak.eu/tags/tmap/</link>
    </image>
    
    <item>
      <title>Interactive choropleth maps with R and tmap (part I)</title>
      <link>https://marcinstepniak.eu/post/interactive-choropleth-maps-with-r-and-tmap-part-i/</link>
      <pubDate>Wed, 25 Sep 2019 00:00:00 +0000</pubDate>
      <guid>https://marcinstepniak.eu/post/interactive-choropleth-maps-with-r-and-tmap-part-i/</guid>
      <description>

&lt;p&gt;In this post, I would like to share how to prepare an interactive map using #rstats and {tmap} package. The first part shows how to make a simple (one thematic layer), interactive choropleth map and save an output as a &lt;code&gt;.html&lt;/code&gt; file, which can be then inserted to a website or serve as a stand alone web. A {tmap} provides with a certain interactivity: zoom in/out and map moving, a popup display and a selection of visibility of basemaps and/or thematic layers.&lt;/p&gt;

&lt;iframe src=&#34;https://marcinstepniak.eu/img/post/post_20190924/Madrid_accessibility_map.html&#34; frameborder=0,  height=400, width=&#34;100%&#34;, scrolling=&#34;no&#34;&gt;&lt;/iframe&gt;

&lt;p&gt;The map shows an total number of accessible jobs from a given location (transport zone), applying a typical commuting distance of inhabitants of Metropolitan area of Madrid (more details about used potential accessibility measure, can be found &lt;a href=&#34;https://marcinstepniak.eu/projects/calculus/main_results/policy_support_tool/#data-preparation-and-preliminary-analysis&#34; target=&#34;_blank&#34;&gt;here&lt;/a&gt;).&lt;/p&gt;

&lt;p&gt;Data used for this example can be downloaded from the &lt;a href=&#34;https://github.com/stmarcin/interactive-maps&#34; target=&#34;_blank&#34;&gt;github&lt;/a&gt;. The accessibility values were calculated for the &lt;a href=&#34;https://marcinstepniak.eu/projects/calculus/&#34; target=&#34;_blank&#34;&gt;MSCA CALCULUS project&lt;/a&gt;. The shapefiles of transport zones of Madrid can be downloaded from the &lt;a href=&#34;http://www.madrid.org/nomecalles/DescargaBDTCorte.icm&#34; target=&#34;_blank&#34;&gt;open data portal&lt;/a&gt; of the Comunidad de Madrid (Delimitaciones Territoriales / Zonas de Transporte). In this map I use the zones dated for year 2013 limited to those located within the Municipality of Madrid (584 units).&lt;/p&gt;

&lt;h2 id=&#34;step-1-load-packages-and-get-the-data&#34;&gt;Step 1. Load packages and get the data&lt;/h2&gt;

&lt;pre&gt;&lt;code class=&#34;language-r&#34;&gt;library(readr)
library(dplyr)
library(tmap)
library(rgdal)
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;For the map we need a &lt;em&gt;shapefile&lt;/em&gt; with transport zones of Madrid (&lt;code&gt;Madrid_TAZ.shp&lt;/code&gt;) and &lt;code&gt;csv&lt;/code&gt; file with accessibility values, both stored in the &lt;code&gt;[Data]&lt;/code&gt; subfolder. The code below loads a &lt;code&gt;shapefile&lt;/code&gt; (as SpatialPolygonsDataFrame) and merges it with the &lt;code&gt;.csv&lt;/code&gt;. Additionally, the code recalculates the total number of accessible jobs (an original &lt;code&gt;Ai_tmap.csv&lt;/code&gt; files contains results in thousands) which will be used in the popup.&lt;/p&gt;

&lt;pre&gt;&lt;code class=&#34;language-r&#34;&gt;Free.Flow &amp;lt;- merge(
      
      # read shapefile
      readOGR(&amp;quot;Data/Madrid_TAZ.shp&amp;quot;,
            layer = &amp;quot;Madrid_TAZ&amp;quot;, GDAL1_integer64_policy = TRUE),
      
      # read csv file add a new column to be displayed in popup
      (read_csv(&amp;quot;Data/Ai_tmap.csv&amp;quot;) %&amp;gt;%
            mutate(K.FreeFlow = FreeFlow*1000) ),
      
      # matching columns
      by.x = &amp;quot;TAZ_Madr_1&amp;quot;, by.y = &amp;quot;Or&amp;quot;) 
&lt;/code&gt;&lt;/pre&gt;

&lt;h2 id=&#34;step-2-make-a-t-map&#34;&gt;Step 2. Make a (t)map!&lt;/h2&gt;

&lt;p&gt;First, let&amp;rsquo;s set the tmap &amp;ldquo;view&amp;rdquo; mode:&lt;/p&gt;

&lt;pre&gt;&lt;code class=&#34;language-r&#34;&gt;tmap_mode(&amp;quot;view&amp;quot;)
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;The advantage of using &lt;code&gt;tmap&lt;/code&gt; is that it shares a logic with &lt;code&gt;ggplot&lt;/code&gt;. Thus, first we need to create a tmap object (&lt;code&gt;tm_shape()&lt;/code&gt;) followed by a thematic layer (&lt;code&gt;tm_polygons()&lt;/code&gt;), and then additional parameters (e.g. &lt;code&gt;tm_layout()&lt;/code&gt;) should be specified. The subsequent elements we add using a &lt;code&gt;+&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;Let&amp;rsquo;s start with the first map using all default settings. We need to define the source of thematic layer (&lt;code&gt;Free.Flow&lt;/code&gt; - a SpatialPolygonsDataFrame object) and a variable which defines a thematic layer (&lt;code&gt;FreeFlow&lt;/code&gt;):&lt;/p&gt;

&lt;pre&gt;&lt;code class=&#34;language-r&#34;&gt;tm_shape(Free.Flow) +
    tm_polygons(&amp;quot;FreeFlow&amp;quot;)
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Here it is! A first interactive map. In the top-left corner, we can turn off the visibility of our thematic layer and select one of the three default basemaps.&lt;/p&gt;

&lt;p&gt;&lt;img src=&#34;https://marcinstepniak.eu/img/post/post_20190924/tmap_01.png&#34; width = 400/&gt;&lt;/p&gt;

&lt;p&gt;The popup shows us a value taken from the column used to create a thematic layer (&lt;code&gt;FreeFlow&lt;/code&gt;) and it uses a first column to define area ID. We can change it now, using a &lt;code&gt;popup.vars&lt;/code&gt; parameter of &lt;code&gt;tm_polygons()&lt;/code&gt; function. The content of the popup is defined as: &lt;code&gt;some.text = variable.name&lt;/code&gt; and a &lt;code&gt;variable.name&lt;/code&gt; should be matched to the name of column of a thematic layer (as defined by &lt;code&gt;tm_shape()&lt;/code&gt;). We use two columns: &lt;code&gt;K.FreeFlow&lt;/code&gt; which contains a total number of available jobs, and &lt;code&gt;POP2017&lt;/code&gt; indicating a total number of population:&lt;/p&gt;

&lt;pre&gt;&lt;code class=&#34;language-r&#34;&gt;tm_shape(Free.Flow) +
    tm_polygons(&amp;quot;FreeFlow&amp;quot;,
                # popup definition
                popup.vars=c(
                    &amp;quot;Accessible_jobs: &amp;quot;=&amp;quot;K.FreeFlow&amp;quot;,
                    &amp;quot;Population: &amp;quot; = &amp;quot;POP2017&amp;quot;)  
                )
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;&lt;img src=&#34;https://marcinstepniak.eu/img/post/post_20190924/tmap_02.png&#34; width = 150/&gt;&lt;/p&gt;

&lt;p&gt;One remark: by default the popup uses a first column of our SpatialPolygonsDataFrame to display an area ID. We can use variables from another column by defining an &lt;code&gt;id&lt;/code&gt; parameter:&lt;/p&gt;

&lt;pre&gt;&lt;code class=&#34;language-r&#34;&gt;tm_shape(Free.Flow) +
      tm_polygons(&amp;quot;FreeFlow&amp;quot;,
                    
                    # popup definition
                    popup.vars=c(
                            &amp;quot;Accessible_jobs: &amp;quot;=&amp;quot;K.FreeFlow&amp;quot;,
                            &amp;quot;Population: &amp;quot; = &amp;quot;POP2017&amp;quot;),
                    id = &amp;quot;TAZ_Madrid&amp;quot;
        )
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;&lt;img src=&#34;https://marcinstepniak.eu/img/post/post_20190924/tmap_03.png&#34; width = 150/&gt;&lt;/p&gt;

&lt;p&gt;Now, let&amp;rsquo;s change the color palette and transparency of the thematic layer. I want to use the &lt;em&gt;inferno&lt;/em&gt; palette - a viridis-based palette matched by &lt;a href=&#34;http://colorspace.r-forge.r-project.org/index.html&#34; target=&#34;_blank&#34;&gt;{colorspace}&lt;/a&gt; team. I want to divide my data into as much as 16 classes. Additionally, I want to set the transparency of the thematic layer. In result, I need to define three parameters: &lt;code&gt;palette&lt;/code&gt;, &lt;code&gt;n&lt;/code&gt; (number of classes) and &lt;code&gt;alpha&lt;/code&gt; (transparency):&lt;/p&gt;

&lt;pre&gt;&lt;code class=&#34;language-r&#34;&gt;        alpha = 0.6,
        n = 16,
        palette = hcl.colors(16, palette = &amp;quot;Inferno&amp;quot;)
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;However, the darkest colors of the &lt;em&gt;inferno&lt;/em&gt; palette are a bit too dark for me, so I want to exclude them. In order to do this, first I set more colors from the palette, and then filter out first two of them. Now, our code looks like this:&lt;/p&gt;

&lt;pre&gt;&lt;code class=&#34;language-r&#34;&gt;tm_shape(Free.Flow) +
        tm_polygons(&amp;quot;FreeFlow&amp;quot;,
                    
                    # popup definition
                    popup.vars=c(
                            &amp;quot;Accessible_jobs: &amp;quot;=&amp;quot;K.FreeFlow&amp;quot;,
                            &amp;quot;Population: &amp;quot; = &amp;quot;POP2017&amp;quot;),
                    id = &amp;quot;TAZ_Madrid&amp;quot;,
                    
                    # transparency, number of classes and palette
                    alpha = 0.6,
                    n = 16,
                    palette = hcl.colors(18, palette = &amp;quot;Inferno&amp;quot;)[3:18]
        )
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;&lt;img src=&#34;https://marcinstepniak.eu/img/post/post_20190924/tmap_04.png&#34; width = 400/&gt;&lt;/p&gt;

&lt;p&gt;The zones&amp;rsquo; borders are too visible so we need to increase their transparency and change their color. I&amp;rsquo;ve tested several solutions taking an advantage of this  &lt;a href=&#34;https://www.rapidtables.com/web/color/RGB_Color.html&#34; target=&#34;_blank&#34;&gt;on-line application&lt;/a&gt; and selected the one that fits the best:&lt;/p&gt;

&lt;pre&gt;&lt;code class=&#34;language-r&#34;&gt;tm_shape(Free.Flow) +
        tm_polygons(&amp;quot;FreeFlow&amp;quot;,
                    
                    # popup definition
                    popup.vars=c(
                            &amp;quot;Accessible_jobs: &amp;quot;=&amp;quot;K.FreeFlow&amp;quot;,
                            &amp;quot;Population: &amp;quot; = &amp;quot;POP2017&amp;quot;),
                    id = &amp;quot;TAZ_Madrid&amp;quot;,
                    
                    # transparency, number of classes and palette
                    alpha = 0.6,
                    n = 16,
                    palette = hcl.colors(18, palette = &amp;quot;Inferno&amp;quot;)[3:18],
                    
                    # border definition: color and transparency
                    border.col = &amp;quot;#990099&amp;quot;,
                    border.alpha = 0.1
        )
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;&lt;img src=&#34;https://marcinstepniak.eu/img/post/post_20190924/tmap_05.png&#34; width = 400/&gt;&lt;/p&gt;

&lt;p&gt;The map is almost ready. The last amendments: title of the legend and the map title. The previous is defined by the &lt;code&gt;title&lt;/code&gt; parameter of &lt;code&gt;tm_polygons()&lt;/code&gt; function, while the latter by a parameter of a separate function &lt;code&gt;tm_layout()&lt;/code&gt;:&lt;/p&gt;

&lt;pre&gt;&lt;code class=&#34;language-r&#34;&gt;tm_shape(Free.Flow) +
        tm_polygons(&amp;quot;FreeFlow&amp;quot;,
                    
                    # popup definition
                    popup.vars=c(
                            &amp;quot;Accessible_jobs: &amp;quot;=&amp;quot;K.FreeFlow&amp;quot;,
                            &amp;quot;Population: &amp;quot; = &amp;quot;POP2017&amp;quot;),
                    id = &amp;quot;TAZ_Madrid&amp;quot;,
                    
                    # transparency, number of classes and palette
                    alpha = 0.6,
                    n = 16,
                    palette = hcl.colors(18, palette = &amp;quot;Inferno&amp;quot;)[3:18],
                    
                    # border definition: color and transparency
                    border.col = &amp;quot;#990099&amp;quot;,
                    border.alpha = 0.1, 
                    
                    # title of the legend
                    title = &amp;quot;Accessible jobs&amp;lt;br&amp;gt;(thous.)&amp;quot;
        ) +
    
    # map title 
    tm_layout(title = &amp;quot;Accessibility to jobs&amp;lt;br&amp;gt;Model: Car free flow speeds&amp;quot;)
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Note, that in both cases, a typical html tags (like &lt;code&gt;&amp;lt;br&amp;gt;&lt;/code&gt;) can be used.&lt;/p&gt;

&lt;p&gt;&lt;img src=&#34;https://marcinstepniak.eu/img/post/post_20190924/tmap_06.png&#34; width = 400/&gt;&lt;/p&gt;

&lt;h1 id=&#34;step-3-save-and-re-use-the-output&#34;&gt;Step 3. Save and re-use the output&lt;/h1&gt;

&lt;p&gt;The map is ready!
Now, we only need to save it as a html widget. We can use a native &lt;code&gt;tmap_save()&lt;/code&gt; function:&lt;/p&gt;

&lt;pre&gt;&lt;code class=&#34;language-r&#34;&gt;tmap_last() %&amp;gt;% 
    tmap_save(&amp;quot;Madrid_accessibility_map.html&amp;quot;)
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;It can be then inserted to a web page:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;&amp;lt;iframe src=&amp;quot;/img/Madrid_accessibility_map.html&amp;quot; frameborder=0,  height=400, width=&amp;quot;100%&amp;quot;, scrolling=&amp;quot;no&amp;quot;&amp;gt;&amp;lt;/iframe&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;All the code and the data can be downloaded from my &lt;a href=&#34;https://github.com/stmarcin/interactive-maps/blob/master/R/R_001_tmap_1_layer.R&#34; target=&#34;_blank&#34;&gt;github&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;&lt;br&gt;&lt;/p&gt;

&lt;p&gt;Session info:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;R version 3.6.0 (2019-04-26)&lt;br /&gt;
Platform: x86_64-apple-darwin15.6.0 (64-bit)&lt;br /&gt;
Running under: macOS Mojave 10.14.5&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;&lt;br&gt;&lt;/p&gt;

&lt;p&gt;Some useful links:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;a href=&#34;https://cran.r-project.org/web/packages/tmap/vignettes/tmap-getstarted.html&#34; target=&#34;_blank&#34;&gt;tmap: get started!&lt;/a&gt;&lt;br /&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&#34;https://mran.microsoft.com/snapshot/2017-01-20/web/packages/tmap/vignettes/tmap-nutshell.html&#34; target=&#34;_blank&#34;&gt;tmap in a nutshell&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;The greate on-line course &lt;a href=&#34;https://learn.r-journalism.com/en/mapping/census_maps/census-maps/&#34; target=&#34;_blank&#34;&gt;Using R for Data Journalism&lt;/a&gt; by &lt;strong&gt;Andrew Ba Tran&lt;/strong&gt;&lt;/li&gt;
&lt;li&gt;The chapter in &lt;a href=&#34;https://geocompr.robinlovelace.net/adv-map.html#interactive-maps&#34; target=&#34;_blank&#34;&gt;Geocomputation with R&lt;/a&gt; by &lt;strong&gt;Robin Lovelace&lt;/strong&gt;, &lt;strong&gt;Jakub Nowosad&lt;/strong&gt; and &lt;strong&gt;Jannes Muenchow&lt;/strong&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;br&gt;
&lt;br&gt;&lt;/p&gt;
</description>
    </item>
    
  </channel>
</rss>
