<?xml version="1.0" encoding="utf-8" standalone="yes" ?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom">
  <channel>
    <title>CALCULUS | Marcin Stępniak</title>
    <link>https://marcinstepniak.eu/tags/calculus/</link>
      <atom:link href="https://marcinstepniak.eu/tags/calculus/index.xml" rel="self" type="application/rss+xml" />
    <description>CALCULUS</description>
    <generator>Source Themes Academic (https://sourcethemes.com/academic/)</generator><language>en-us</language><copyright>© Marcin Stepniak; 2019</copyright><lastBuildDate>Mon, 18 May 2020 00:00:00 +0000</lastBuildDate>
    <image>
      <url>https://marcinstepniak.eu/img/icon-192.png</url>
      <title>CALCULUS</title>
      <link>https://marcinstepniak.eu/tags/calculus/</link>
    </image>
    
    <item>
      <title>CALCULUS project main results</title>
      <link>https://marcinstepniak.eu/projects/calculus/main_results/</link>
      <pubDate>Sun, 09 Sep 2018 00:00:00 +0000</pubDate>
      <guid>https://marcinstepniak.eu/projects/calculus/main_results/</guid>
      <description>&lt;p&gt;&lt;br&gt;&lt;/p&gt;

&lt;p&gt;This section contains main project results. They are divided into three main parts:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;&lt;a href=&#34;temporal_resolution/&#34; target=&#34;_blank&#34;&gt;&lt;strong&gt;Temporal resolution&lt;/strong&gt;&lt;/a&gt;: The study aims to to provide guidance to researchers and practitioners on the selection of appropriate temporal resolutions in accessibility studies.&lt;/p&gt;&lt;/li&gt;

&lt;li&gt;&lt;p&gt;&lt;a href=&#34;gtfs_study/&#34; target=&#34;_blank&#34;&gt;&lt;strong&gt;GTFS study&lt;/strong&gt;&lt;/a&gt;: This research focus on an evaluation of a supply side of public transport using GTFS and other data available in open access.&lt;/p&gt;&lt;/li&gt;

&lt;li&gt;&lt;p&gt;&lt;a href=&#34;policy_support_tool/&#34; target=&#34;_blank&#34;&gt;&lt;strong&gt;Policy support tool&lt;/strong&gt;&lt;/a&gt;: This section contains a brief summary of methodological guidelines for an evaluation of an impact of particular transport related restrictions of accessibility in urban area. The method extensively uses a new, time-sensitive network data (speed profiles and GTFS feeds) and it is based on comparison of accessibility levels and its spatial patterns in order to identify a particular, transport-related accessibility restrictions.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;
</description>
    </item>
    
    <item>
      <title>{DepartureTime} R package</title>
      <link>https://marcinstepniak.eu/post/departuretime-r-package/</link>
      <pubDate>Mon, 18 May 2020 00:00:00 +0000</pubDate>
      <guid>https://marcinstepniak.eu/post/departuretime-r-package/</guid>
      <description>

&lt;p&gt;I have created my first (ever!) #rstats package. It is called &lt;code&gt;{DepartureTime}&lt;/code&gt; and its purpose is to prepare a dataset with several departure times for temporally sensitive accessibility analysis.&lt;/p&gt;

&lt;h3 id=&#34;what-does-departuretime-do&#34;&gt;What does &lt;code&gt;{DepartureTime}&lt;/code&gt; do?&lt;/h3&gt;

&lt;p&gt;The package consists of one function which permits you to generate a series of departure times applying user-defined temporal resolution and one of four different sampling procedures:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Systematic&lt;/strong&gt; sampling method: departure times are selected using a regular interval defined by the frequency&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Simple Random&lt;/strong&gt; sampling method: a specified number of departure times (defined by the frequency) is randomly selected from the time window&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Hybrid&lt;/strong&gt; sampling method: departure times are randomly selected from given time intervals (resulted from applied temporal resolution)&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Constrained Random Walk Sampling&lt;/strong&gt; sampling method: a first departure time is randomly selected from the subset of the length defined by the frequency and beginning of the time window; then, the next departure time is randomly selected from the subset limited by &lt;code&gt;\(Tn+f/2\)&lt;/code&gt;  and  &lt;code&gt;\(Tn+f+f/2\)&lt;/code&gt;.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;An example of the result of sampling procedures for 20-minute temporal resolution and 1-hour-long time window (07:00-08:00) illustrates the following table:&lt;/p&gt;

&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Sampling method&lt;/th&gt;
&lt;th&gt;Departure times&lt;/th&gt;
&lt;th&gt;Comments&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;

&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;&lt;strong&gt;Systematic&lt;/strong&gt;&lt;/td&gt;
&lt;td&gt;07:00; 07:20, 7:40, 08:00&lt;/td&gt;
&lt;td&gt;regular interval of 20 minutes&lt;sup&gt;1&lt;/sup&gt;&lt;/td&gt;
&lt;/tr&gt;

&lt;tr&gt;
&lt;td&gt;&lt;strong&gt;Simple Random&lt;/strong&gt;&lt;/td&gt;
&lt;td&gt;07:18; 07:51; 07:55&lt;/td&gt;
&lt;td&gt;3 randomly selected departure times from the time window&lt;sup&gt;2&lt;/sup&gt;&lt;/td&gt;
&lt;/tr&gt;

&lt;tr&gt;
&lt;td&gt;&lt;strong&gt;Hybrid&lt;/strong&gt;&lt;/td&gt;
&lt;td&gt;07:02; 07:23; 07:50&lt;/td&gt;
&lt;td&gt;One randomly selected departure time from each time interval period&lt;sup&gt;3&lt;/sup&gt;&lt;/td&gt;
&lt;/tr&gt;

&lt;tr&gt;
&lt;td&gt;&lt;strong&gt;Random Walk&lt;/strong&gt;&lt;/td&gt;
&lt;td&gt;07:15; 07:36; 07:49&lt;/td&gt;
&lt;td&gt;on average there should be 20-minute interval between departure times&lt;sup&gt;4&lt;/sup&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;

&lt;p&gt;&lt;sup&gt;1&lt;/sup&gt;  as 20-minute interval fits to 60 minute time window it provides 4 departure times.&lt;br /&gt;
&lt;sup&gt;2&lt;/sup&gt;  i.e. one per each 20 min. in 60-minute time window.&lt;br /&gt;
&lt;sup&gt;3&lt;/sup&gt;  i.e. one from 07:00-07:19, one from 07:20-07:39 and one from 07:40-07:59.&lt;br /&gt;
&lt;sup&gt;4&lt;/sup&gt;  due to the nature of the sampling procedure, the number of departure times might differ.&lt;/p&gt;

&lt;p&gt;For details on sampling procedures, please consult &lt;a href=&#34;https://trid.trb.org/view/1497217&#34; target=&#34;_blank&#34;&gt;Owen &amp;amp; Murphy (2018)&lt;/a&gt;.&lt;/p&gt;

&lt;h3 id=&#34;why-may-you-need-departuretime&#34;&gt;Why may you need &lt;code&gt;{DepartureTime}&lt;/code&gt;?&lt;/h3&gt;

&lt;p&gt;Briefly: because, if you include temporal dimension to your accessibility analysis, the result depends on the applied departure time. In particular, in case of public transport, accessibility level in a given area might be highly affected by the fact whether you are &amp;ldquo;lucky&amp;rdquo; or not in terms of waiting times (for the first connection and/or in case of transfers) and, in result, these values might differ substantially just because you decided on a particular departure time. In order to limit this negative impact on analysis, you need to consider different departure times and than aggregate results in order to better reflect how does one experience level of accessibility.&lt;/p&gt;

&lt;p&gt;Consider this graph presented by &lt;a href=&#34;http://www.sciencedirect.com/science/article/pii/S0965856415000191&#34; target=&#34;_blank&#34;&gt;Owen &amp;amp; Levinson (2015)&lt;/a&gt;:&lt;/p&gt;

&lt;p&gt;&lt;img src=&#34;https://marcinstepniak.eu/img/post/post_20200518/Owen_Levinson_2015.png&#34; alt=&#34;*Source: [Owen &amp;amp; Levinson (2015)](http://www.sciencedirect.com/science/article/pii/S0965856415000191)*&#34; /&gt;{width=400px}&lt;/p&gt;

&lt;p&gt;It shows how accessibility change over time for a selected census block. Depending on the selected departure time, you can get completely different accessibility level, even though the transport system nor the distribution of activities does not change.&lt;/p&gt;

&lt;p&gt;Further, even if the level of availability does not change so drastically, you may still want to simulate different situation e.g. applying free flow, peak or out-of-peak speeds. The graph below, prepared for the Dutch case study, compares daily variation of job accessibility by car and by public transport (walk-and-ride and bike-and-ride models):&lt;/p&gt;

&lt;p&gt;&lt;img src=&#34;https://marcinstepniak.eu/img/post/post_20200518/Pritchard_et_al_2019.png&#34; alt=&#34;*Source: [Pritchard, Stępniak and Geurs (2019)](https://www.sciencedirect.com/science/article/pii/B9780128148181000056)*&#34; /&gt;&lt;/p&gt;

&lt;p&gt;Thus, regardless transport mode, you may need to repeat analysis without changing anything but departure time (even though, a required temporal resolution would change): in case of car accessibility you may need to generate origin-destination matrices couple of times during the day, while in case of public transport you should compute travel times even couple of times per hour (I you need more information on the consequences of applied temporal resolution on precision of accessibility analysis I can shamelessly suggest you the following paper: &lt;a href=&#34;https://www.sciencedirect.com/science/article/pii/S0966692318305209&#34; target=&#34;_blank&#34;&gt;Stepniak, Pritchard, Geurs &amp;amp; Goliszek (2019)&lt;/a&gt;).&lt;/p&gt;

&lt;h3 id=&#34;how-can-you-take-advantage-of-departuretime&#34;&gt;How can you take advantage of &lt;code&gt;{DepartureTime}&lt;/code&gt;?&lt;/h3&gt;

&lt;p&gt;As described in the &lt;a href=&#34;https://www.sciencedirect.com/science/article/pii/S0966692318305209&#34; target=&#34;_blank&#34;&gt;paper&lt;/a&gt;, you can select different approach how to tackle the issue of temporal resolution. Regardless which approach you select, you would need a table which contains all departure times in order to automatize calculations. I used it in ArcGIS Network Analyst (with &lt;a href=&#34;http://esri.github.io/public-transit-tools/AddGTFStoaNetworkDataset.html&#34; target=&#34;_blank&#34;&gt;Add GTFS to a Network Dataset&lt;/a&gt; tool), but as far as I know, &lt;code&gt;{DepartureTime}&lt;/code&gt; may be also useful when working with &lt;a href=&#34;https://www.opentripplanner.org&#34; target=&#34;_blank&#34;&gt;OpenTripPlanner&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;In ArcGIS you can easily prepare an arcpy code or just a ModelBuilder, which permits you to iterate by subsequent departure time. The simple ModelBuilder looks like this:&lt;/p&gt;

&lt;p&gt;&lt;img src=&#34;https://marcinstepniak.eu/img/post/post_20200518/ModelBuilder.png&#34; alt=&#34;*Example of Model Builder in ArcGIS [(Click here for larger picture)](https://marcinstepniak.eu/img/post/post_20200518/ModelBuilder.png)*&#34; /&gt;&lt;/p&gt;

&lt;p&gt;The &lt;code&gt;.dbf&lt;/code&gt; file with the output from &lt;code&gt;{DepartureTime}&lt;/code&gt; you need to locate in &lt;code&gt;Departure Times&lt;/code&gt; (market with the red circle) and iterate it by &lt;em&gt;Field Values&lt;/em&gt; (by &lt;code&gt;Date&lt;/code&gt; field, setting &lt;code&gt;Data type&lt;/code&gt; as &lt;em&gt;Date&lt;/em&gt;)&lt;/p&gt;

&lt;p&gt;&lt;img src=&#34;https://marcinstepniak.eu/img/post/post_20200518/ModelBuilder2.png&#34; alt=&#34;*Iterate Field Values settings*&#34; /&gt;&lt;/p&gt;

&lt;p&gt;In result of the above model, you obtain a set of origin-destination matrices, one for each of a departure time, exported to separate files (e.g. &lt;code&gt;.dbf&lt;/code&gt; files). Then, they can be aggregated in order to obtain more realistic &lt;em&gt;average travel time&lt;/em&gt; (or &lt;em&gt;average accessibility&lt;/em&gt;), e.g. during the morning peak-hours or during the day (night etc.).&lt;/p&gt;

&lt;h3 id=&#34;how-does-departuretime-work&#34;&gt;How does &lt;code&gt;{DepartureTime}&lt;/code&gt; work?&lt;/h3&gt;

&lt;p&gt;The &lt;code&gt;{DepartureTime}&lt;/code&gt; package can be installed in R directly from GitHub (if you don&amp;rsquo;t have &lt;code&gt;{devtools}&lt;/code&gt; installed, you need to install it first):&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;# install.packages(&amp;quot;devtools&amp;quot;)
devtools::install_github(&amp;quot;stmarcin/DepartureTime&amp;quot;)
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;The function has the following syntax and default values:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;DepartureTime &amp;lt;- function(method = &amp;quot;H&amp;quot;,
                          dy = format(Sys.Date(), &amp;quot;%Y&amp;quot;),  
                          dm = format(Sys.Date(), &amp;quot;%m&amp;quot;), 
                          dd = format(Sys.Date(), &amp;quot;%d&amp;quot;),
                          tmin = 0, tmax = 24,
                          res = 5,
                          MMDD = TRUE,
                          ptw = FALSE)
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Function variables:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;code&gt;method&lt;/code&gt; - sampling method; Options:

&lt;ul&gt;
&lt;li&gt;&lt;code&gt;R&lt;/code&gt; OR &lt;code&gt;Random&lt;/code&gt;: Simple random sampling;&lt;/li&gt;
&lt;li&gt;&lt;code&gt;S&lt;/code&gt; OR &lt;code&gt;Systematic&lt;/code&gt;: Systematic sampling;&lt;/li&gt;
&lt;li&gt;&lt;code&gt;H&lt;/code&gt; OR &lt;code&gt;Hybrid&lt;/code&gt;: Hybrid sampling;&lt;/li&gt;
&lt;li&gt;&lt;code&gt;W&lt;/code&gt; OR &lt;code&gt;Walk&lt;/code&gt;: Constrained random walk sampling;&lt;/li&gt;
&lt;/ul&gt;&lt;/li&gt;
&lt;li&gt;&lt;code&gt;dy&lt;/code&gt;, &lt;code&gt;dm&lt;/code&gt; and &lt;code&gt;dd&lt;/code&gt; - date of the analysis (formats: YYYY, MM, DD); &lt;strong&gt;default: system date&lt;/strong&gt;;&lt;/li&gt;
&lt;li&gt;&lt;code&gt;tmin&lt;/code&gt; and &lt;code&gt;tmax&lt;/code&gt; - limits of the time window (format: HH); &lt;strong&gt;default: full day&lt;/strong&gt; (00:00 - 24:00);&lt;/li&gt;
&lt;li&gt;&lt;code&gt;res&lt;/code&gt; - temporal resolution; &lt;strong&gt;default: 5 minutes&lt;/strong&gt;&lt;/li&gt;
&lt;li&gt;&lt;code&gt;MMDD&lt;/code&gt; - date format of the output (TRUE / FALSE) &lt;strong&gt;default: TRUE&lt;/strong&gt;

&lt;ul&gt;
&lt;li&gt;&lt;code&gt;TRUE&lt;/code&gt;: MM/DD/YYYY;&lt;/li&gt;
&lt;li&gt;&lt;code&gt;FALSE&lt;/code&gt;: DD/MM/YYYY;&lt;/li&gt;
&lt;/ul&gt;&lt;/li&gt;
&lt;li&gt;&lt;code&gt;ptw&lt;/code&gt; - print limits of subsetted time-windows; &lt;strong&gt;default: FALSE&lt;/strong&gt;;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;The &lt;code&gt;DepartureTime()&lt;/code&gt; function creates a data frame with generated departure times, already formatted for ArcGIS:&lt;/p&gt;

&lt;table class=&#34;table&#34; style=&#34;width: auto !important; margin-left: auto; margin-right: auto;&#34;&gt;
 &lt;thead&gt;
  &lt;tr&gt;
   &lt;th style=&#34;text-align:left;&#34;&gt; ColumnName &lt;/th&gt;
   &lt;th style=&#34;text-align:left;&#34;&gt; Description &lt;/th&gt;
  &lt;/tr&gt;
 &lt;/thead&gt;
&lt;tbody&gt;
  &lt;tr&gt;
   &lt;td style=&#34;text-align:left;&#34;&gt; ID &lt;/td&gt;
   &lt;td style=&#34;text-align:left;&#34;&gt; rowID (integer), starts with 0 &lt;/td&gt;
  &lt;/tr&gt;
  &lt;tr&gt;
   &lt;td style=&#34;text-align:left;&#34;&gt; Date &lt;/td&gt;
   &lt;td style=&#34;text-align:left;&#34;&gt; Departure date &amp;amp; hour &lt;/td&gt;
  &lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;

&lt;p&gt;Example with selected user-defined parameters:&lt;/p&gt;

&lt;pre&gt;&lt;code class=&#34;language-r&#34;&gt;DepartureTime(method = &amp;quot;S&amp;quot;,    # systematic sampling method
  dm = 5, dd = 15,             # user-defined date: 15th May, 2020 (current year)
  tmin = 7, tmax = 9,          # user-defined time window (07:00 - 09:00)
  res = 20)                    # user-defined temporal resolution (20 minutes)
&lt;/code&gt;&lt;/pre&gt;

&lt;table class=&#34;table&#34; style=&#34;width: auto !important; margin-left: auto; margin-right: auto;&#34;&gt;
 &lt;thead&gt;
  &lt;tr&gt;
   &lt;th style=&#34;text-align:right;&#34;&gt; ID &lt;/th&gt;
   &lt;th style=&#34;text-align:left;&#34;&gt; Date &lt;/th&gt;
  &lt;/tr&gt;
 &lt;/thead&gt;
&lt;tbody&gt;
  &lt;tr&gt;
   &lt;td style=&#34;text-align:right;&#34;&gt; 0 &lt;/td&gt;
   &lt;td style=&#34;text-align:left;&#34;&gt; 05/15/2020  07:00 &lt;/td&gt;
  &lt;/tr&gt;
  &lt;tr&gt;
   &lt;td style=&#34;text-align:right;&#34;&gt; 1 &lt;/td&gt;
   &lt;td style=&#34;text-align:left;&#34;&gt; 05/15/2020  07:20 &lt;/td&gt;
  &lt;/tr&gt;
  &lt;tr&gt;
   &lt;td style=&#34;text-align:right;&#34;&gt; 2 &lt;/td&gt;
   &lt;td style=&#34;text-align:left;&#34;&gt; 05/15/2020  07:40 &lt;/td&gt;
  &lt;/tr&gt;
  &lt;tr&gt;
   &lt;td style=&#34;text-align:right;&#34;&gt; 3 &lt;/td&gt;
   &lt;td style=&#34;text-align:left;&#34;&gt; 05/15/2020  08:00 &lt;/td&gt;
  &lt;/tr&gt;
  &lt;tr&gt;
   &lt;td style=&#34;text-align:right;&#34;&gt; 4 &lt;/td&gt;
   &lt;td style=&#34;text-align:left;&#34;&gt; 05/15/2020  08:20 &lt;/td&gt;
  &lt;/tr&gt;
  &lt;tr&gt;
   &lt;td style=&#34;text-align:right;&#34;&gt; 5 &lt;/td&gt;
   &lt;td style=&#34;text-align:left;&#34;&gt; 05/15/2020  08:40 &lt;/td&gt;
  &lt;/tr&gt;
  &lt;tr&gt;
   &lt;td style=&#34;text-align:right;&#34;&gt; 6 &lt;/td&gt;
   &lt;td style=&#34;text-align:left;&#34;&gt; 05/15/2020  09:00 &lt;/td&gt;
  &lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;

&lt;p&gt;You can easily save &lt;code&gt;{DepartureTime}&lt;/code&gt; output as &lt;code&gt;.dbf&lt;/code&gt; file (you need to have &lt;code&gt;{foreign}&lt;/code&gt; package installed):&lt;/p&gt;

&lt;pre&gt;&lt;code class=&#34;language-r&#34;&gt;library(DepartureTime)
library(foreign)
library(dplyr)

# generate departure times for 8-10am time window 
# with 30-minute temporal resolution applying hybrid sampling model:
DepartureTime(tmin = 8, tmax = 10, res = 30) %&amp;gt;% 
  
  #save output in OD_analysis subfolder as My_Departure_Times.dbf
  write.dbf(&amp;quot;OD_analysis/My_Departure_Times.dbf&amp;quot;)
&lt;/code&gt;&lt;/pre&gt;

&lt;h3 id=&#34;questions&#34;&gt;Questions?&lt;/h3&gt;

&lt;p&gt;If you have any questions, feel free to &lt;a href=&#34;https://marcinstepniak.eu/#contact&#34; target=&#34;_blank&#34;&gt;contact me&lt;/a&gt; or fill an &lt;a href=&#34;https://github.com/stmarcin/DepartureTime/issues&#34; target=&#34;_blank&#34;&gt;issue on github&lt;/a&gt;.&lt;/p&gt;

&lt;h3 id=&#34;further-reading&#34;&gt;Further reading&lt;/h3&gt;

&lt;p&gt;Murphy, B., Owen, A., 2019. &lt;a href=&#34;https://jtlu.org/index.php/jtlu/article/view/1379&#34; target=&#34;_blank&#34;&gt;Temporal sampling and service frequency harmonics in transit accessibility evaluation&lt;/a&gt; Journal of Transport and Land Use 12, 893–913. &lt;a href=&#34;https://doi.org/10.5198/jtlu.2019.1379&#34; target=&#34;_blank&#34;&gt;https://doi.org/10.5198/jtlu.2019.1379&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Owen, A., Levinson, D.M., 2015. &lt;a href=&#34;http://www.sciencedirect.com/science/article/pii/S0965856415000191&#34; target=&#34;_blank&#34;&gt;Modeling the commute mode share of transit using continuous accessibility to jobs&lt;/a&gt; Transportation Research Part A: Policy and Practice 74, 110–122. &lt;a href=&#34;https://doi.org/10.1016/j.tra.2015.02.002&#34; target=&#34;_blank&#34;&gt;https://doi.org/10.1016/j.tra.2015.02.002&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Owen, A., Murphy, B., 2018. &lt;a href=&#34;https://trid.trb.org/view/1497217&#34; target=&#34;_blank&#34;&gt;Temporal Sampling and Service Frequency Harmonics in Transit Accessibility Evaluation&lt;/a&gt;, in: Transportation Research Board 97th Annual Meeting. p. 10.&lt;/p&gt;

&lt;p&gt;Pritchard, J.P., Stępniak, M., Geurs, K.T., 2019. &lt;a href=&#34;https://www.sciencedirect.com/science/article/pii/B9780128148181000056&#34; target=&#34;_blank&#34;&gt;Equity analysis of dynamic bike-and-ride accessibility in the Netherlands&lt;/a&gt;, in: Lucas, K., Martens, K., Ciommo, F. Di, Dupont-Kieffer, A. (Eds.), Measuring Transport Equity. Elsevier, pp. 73–83. &lt;a href=&#34;https://doi.org/10.1016/B978-0-12-814818-1.00005-6&#34; target=&#34;_blank&#34;&gt;https://doi.org/10.1016/B978-0-12-814818-1.00005-6&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Stępniak, M., Pritchard, J.P., Geurs, K.T., Goliszek, S., 2019. &lt;a href=&#34;https://www.sciencedirect.com/science/article/pii/S0966692318305209&#34; target=&#34;_blank&#34;&gt;The impact of temporal resolution on public transport accessibility measurement: Review and case study in Poland&lt;/a&gt; Journal of Transport Geography 75, 8–24. &lt;a href=&#34;https://doi.org/10.1016/j.jtrangeo.2019.01.007&#34; target=&#34;_blank&#34;&gt;https://doi.org/10.1016/j.jtrangeo.2019.01.007&lt;/a&gt;&lt;/p&gt;
</description>
    </item>
    
    <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>
