<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/r-ladies-global-tour/"></div></div><div></div></div><div></div></div>
<div>
(This article was first published on <strong><a href="http://www.masalmon.eu/2017/10/06/globalrladiestour/"> Maëlle</a></strong>, and kindly contributed to <a rel="nofollow" href="https://www.r-bloggers.com/">R-bloggers)</a> </div>
<p>It was recently brought to my attention by <a rel="nofollow" href="https://twitter.com/hfcfrick" target="_blank">Hannah Frick</a> that there are now sooo many R-Ladies chapters around the world! <a rel="nofollow" href="http://rladies.org/" target="_blank">R-Ladies</a> is a world-wide organization to promote gender diversity in the R community, and I’m very grateful to be part of this community through which I met so many awesome ladies! Since we’re all connected, it has now happened quite a few times that R-Ladies gave talks at chapters outside of their hometowns. An R-Lady from Taiwan giving a talk in Madrid while on a trip in Europe and another one doing the same in Lisbon, an R-Lady from San Francisco presenting at the London and Barcelona chapters thanks to a conference on the continent, an R-Lady from Uruguay sharing her experience for the New York City and San Francisco chapters… It’s like rockstars tours!</p>
<p>Therefore we R-Ladies often joke about doing an exhaustive global tour. Hannah made me think about this tour again… If someone were to really visit all of the chapters, what would be the shortest itinerary? And could we do a cool gif with the results? These are the problems we solve here.</p>
<p><span></span></p>
<h1>Getting the chapters</h1>
<p>To find all chapters, I’ll use Meetup information about meetups whose topics include “r-ladies”, although it means forgetting a few chapters that maybe haven’t updated their topics yet. Thus, I’ll scrape <a rel="nofollow" href="https://www.meetup.com/topics/r-ladies/all/" target="_blank">this webpage</a> because I’m too impatient to wait for the cool <a rel="nofollow" href="https://github.com/rladies/meetupr/" target="_blank"><code>meetupr</code> package</a> to include the Meetup API topic endpoint and because I’m too lazy to include it myself. I did open <a rel="nofollow" href="https://github.com/rladies/meetupr/issues/13" target="_blank">an issue</a> though. Besides, I was allowed to scrape the page:</p>
<div>
<pre><code><span>robotstxt</span><span>::</span><span>paths_allowed</span><span>(</span><span>&quot;https://www.meetup.com/topics/&quot;</span><span>)</span><span>
</span></code></pre>
</div>
<div>
<pre><code>## [1] TRUE
</code></pre>
</div>
<p>Yesss. So let’s scrape!</p>
<div>
<pre><code><span>library</span><span>(</span><span>&quot;rvest&quot;</span><span>)</span><span>
</span><span>link</span><span> </span><span>&lt;-</span><span> </span><span>&quot;https://www.meetup.com/topics/r-ladies/all/&quot;</span><span>
</span><span>page_content</span><span> </span><span>&lt;-</span><span> </span><span>read_html</span><span>(</span><span>link</span><span>)</span><span>
</span><span>css</span><span> </span><span>&lt;-</span><span> </span><span>'span[class=&quot;text--secondary text--small chunk&quot;]'</span><span>
</span><span>chapters</span><span> </span><span>&lt;-</span><span> </span><span>html_nodes</span><span>(</span><span>page_content</span><span>,</span><span> </span><span>css</span><span>)</span><span> </span><span>%>%</span><span> </span><span>html_text</span><span>(</span><span>trim</span><span> </span><span>=</span><span> </span><span>TRUE</span><span>)</span><span>
</span><span>chapters</span><span> </span><span>&lt;-</span><span> </span><span>stringr</span><span>::</span><span>str_replace</span><span>(</span><span>chapters</span><span>,</span><span> </span><span>&quot;.*\\|&quot;</span><span>,</span><span> </span><span>&quot;&quot;</span><span>)</span><span>
</span><span>chapters</span><span> </span><span>&lt;-</span><span> </span><span>trimws</span><span>(</span><span>chapters</span><span>)</span><span>
</span><span>head</span><span>(</span><span>chapters</span><span>)</span><span>
</span></code></pre>
</div>
<div>
<pre><code>## [1] &quot;London, United Kingdom&quot; &quot;San Francisco, CA&quot;     
## [3] &quot;Istanbul, Turkey&quot;       &quot;Melbourne, Australia&quot;  
## [5] &quot;New York, NY&quot;           &quot;Madrid, Spain&quot;
</code></pre>
</div>
<div>
<pre><code><span># Montenegro
</span><span>chapters</span><span>[</span><span>chapters</span><span> </span><span>==</span><span> </span><span>&quot;HN\\, Montenegro&quot;</span><span>]</span><span> </span><span>&lt;-</span><span> </span><span>&quot;Herceg Novi, Montenegro&quot;</span><span>
</span></code></pre>
</div>
<h1>Geolocating the chapters</h1>
<p>Here I decided to use a <a rel="nofollow" href="https://github.com/ropensci/opencage" target="_blank">nifty package</a> to the awesome OpenCage API. Ok, this is my own package. But hey it’s really a good geocoding API. And the package was <a rel="nofollow" href="https://github.com/ropensci/onboarding/issues/36" target="_blank">reviewed for rOpenSci by Julia Silge</a>! In the docs of the package you’ll see how to save your API key in order not to have to input it as a function parameter every time.</p>
<p>Given that there are many chapters but not that many (41 to be exact), I could inspect the results and check them.</p>
<div>
<pre><code><span>geolocate_chapter</span><span> </span><span>&lt;-</span><span> </span><span>function</span><span>(</span><span>chapter</span><span>){</span><span>
</span><span># query the API
</span><span> </span><span>results</span><span> </span><span>&lt;-</span><span> </span><span>opencage</span><span>::</span><span>opencage_forward</span><span>(</span><span>chapter</span><span>)</span><span>$</span><span>results</span><span>
</span><span># deal with Strasbourg
</span><span> </span><span>if</span><span>(</span><span>chapter</span><span> </span><span>==</span><span> </span><span>&quot;Strasbourg, France&quot;</span><span>){</span><span>
</span><span>results</span><span> </span><span>&lt;-</span><span> </span><span>dplyr</span><span>::</span><span>filter</span><span>(</span><span>results</span><span>,</span><span> </span><span>components.city</span><span> </span><span>==</span><span> </span><span>&quot;Strasbourg&quot;</span><span>)</span><span>
</span><span>}</span><span>
</span><span># get a CITY
</span><span> </span><span>results</span><span> </span><span>&lt;-</span><span> </span><span>dplyr</span><span>::</span><span>filter</span><span>(</span><span>results</span><span>,</span><span> </span><span>components._type</span><span> </span><span>==</span><span> </span><span>&quot;city&quot;</span><span>)</span><span>
</span><span># sort the results by confidence score </span><span> </span><span>results</span><span> </span><span>&lt;-</span><span> </span><span>dplyr</span><span>::</span><span>arrange</span><span>(</span><span>results</span><span>,</span><span> </span><span>desc</span><span>(</span><span>confidence</span><span>))</span><span>
</span><span># choose the first line among those with highest confidence score
</span><span> </span><span>results</span><span> </span><span>&lt;-</span><span> </span><span>results</span><span>[</span><span>1</span><span>,]</span><span>
</span><span># return only long and lat
</span><span> </span><span>tibble</span><span>::</span><span>tibble</span><span>(</span><span>long</span><span> </span><span>=</span><span> </span><span>results</span><span>$</span><span>geometry.lng</span><span>,</span><span>
</span><span>lat</span><span> </span><span>=</span><span> </span><span>results</span><span>$</span><span>geometry.lat</span><span>,</span><span>
</span><span>chapter</span><span> </span><span>=</span><span> </span><span>chapter</span><span>,</span><span> </span><span>formatted</span><span> </span><span>=</span><span> </span><span>results</span><span>$</span><span>formatted</span><span>)</span><span>
</span><span>}</span><span>
</span><span>chapters_df</span><span> </span><span>&lt;-</span><span> </span><span>purrr</span><span>::</span><span>map_df</span><span>(</span><span>chapters</span><span>,</span><span> </span><span>geolocate_chapter</span><span>)</span><span>
</span><span># add an index variable
</span><span>chapters_df</span><span> </span><span>&lt;-</span><span> </span><span>dplyr</span><span>::</span><span>mutate</span><span>(</span><span>chapters_df</span><span>,</span><span> </span><span>id</span><span> </span><span>=</span><span> </span><span>1</span><span>:</span><span>nrow</span><span>(</span><span>chapters_df</span><span>))</span><span>
</span><span>knitr</span><span>::</span><span>kable</span><span>(</span><span>chapters_df</span><span>[</span><span>1</span><span>:</span><span>10</span><span>,])</span><span>
</span></code></pre>
</div>
<table>
<thead>
<tr>
<th>long</th>
<th>lat</th>
<th>chapter</th>
<th>formatted</th>
<th>id</th>
</tr>
</thead>
<tbody>
<tr>
<td>-0.1276473</td>
<td>51.50732</td>
<td>London, United Kingdom</td>
<td>London, United Kingdom</td>
<td>1</td>
</tr>
<tr>
<td>-122.4192362</td>
<td>37.77928</td>
<td>San Francisco, CA</td>
<td>San Francisco, San Francisco City and County, California, United States of America</td>
<td>2</td>
</tr>
<tr>
<td>28.9651646</td>
<td>41.00963</td>
<td>Istanbul, Turkey</td>
<td>Istanbul, Fatih, Turkey</td>
<td>3</td>
</tr>
<tr>
<td>144.9631608</td>
<td>-37.81422</td>
<td>Melbourne, Australia</td>
<td>Melbourne VIC 3000, Australia</td>
<td>4</td>
</tr>
<tr>
<td>-73.9865811</td>
<td>40.73060</td>
<td>New York, NY</td>
<td>New York City, United States of America</td>
<td>5</td>
</tr>
<tr>
<td>-3.7652699</td>
<td>40.32819</td>
<td>Madrid, Spain</td>
<td>Leganés, Community of Madrid, Spain</td>
<td>6</td>
</tr>
<tr>
<td>-77.0366455</td>
<td>38.89495</td>
<td>Washington, DC</td>
<td>Washington, District of Columbia, United States of America</td>
<td>7</td>
</tr>
<tr>
<td>-83.0007064</td>
<td>39.96226</td>
<td>Columbus, OH</td>
<td>Columbus, Franklin County, Ohio, United States of America</td>
<td>8</td>
</tr>
<tr>
<td>-71.0595677</td>
<td>42.36048</td>
<td>Boston, MA</td>
<td>Boston, Suffolk, Massachusetts, United States of America</td>
<td>9</td>
</tr>
<tr>
<td>-79.0232050</td>
<td>35.85030</td>
<td>Durham, NC</td>
<td>Durham, NC, United States of America</td>
<td>10</td>
</tr>
</tbody>
</table>
<h1>Planning the trip</h1>
<p>I wanted to use the <a rel="nofollow" href="https://github.com/dirkschumacher/ompr" target="_blank"><code>ompr</code> package</a> inspired by this fantastic use case, <a rel="nofollow" href="https://rstudio-pubs-static.s3.amazonaws.com/199542_7f23d4edf6094d89b386e9c875d09a1c.html" target="_blank">“Boris Johnson’s fully global itinerary of apology”</a> – be careful, the code of this use case is slightly outdated but is up-to-date <a rel="nofollow" href="https://dirkschumacher.github.io/ompr/articles/problem-tsp.html" target="_blank">in the traveling salesperson vignette</a>. The <code>ompr</code> package supports modeling and solving <a rel="nofollow" href="https://en.wikipedia.org/wiki/Integer_programming" target="_blank">Mixed Integer Linear Programs</a>. I got a not so bad notion of what this means by looking <a rel="nofollow" href="https://dirkschumacher.github.io/ompr/articles/index.html" target="_blank">at this collection of use cases</a>. Sadly, the traveling salesperson problem is a complicated problem and its solving time exponentially increases with the number of stops… in that case, it became really too long for plain mixed integer linear programming, as in “more than 24 hours later not done” too long.</p>
<p>Therefore, I decided to use a <a rel="nofollow" href="https://github.com/mhahsler/TSP" target="_blank">specific R package for traveling salesperson problems <code>TSP</code></a>. Dirk, <code>ompr</code>’s maintainer, actually used it once as seen <a rel="nofollow" href="https://gist.github.com/berlinermorgenpost/027e9c2cd7cd54f36a4033121012252e" target="_blank">in this gist</a> and then <a rel="nofollow" href="https://interaktiv.morgenpost.de/lange-nacht-der-museen/" target="_blank">in this newspaper piece</a> about how to go to all 78 Berlin museums during the night of the museums. Quite cool!</p>
<p>We first need to compute the distance between chapters. In kilometers and rounded since it’s enough precision.</p>
<div>
<pre><code><span>convert_to_km</span><span> </span><span>&lt;-</span><span> </span><span>function</span><span>(</span><span>x</span><span>){</span><span>
</span><span>round</span><span>(</span><span>x</span><span>/</span><span>1000</span><span>)</span><span>
</span><span>}</span><span>
</span><span>distance</span><span> </span><span>&lt;-</span><span> </span><span>geosphere</span><span>::</span><span>distm</span><span>(</span><span>as.matrix</span><span>(</span><span>dplyr</span><span>::</span><span>select</span><span>(</span><span>chapters_df</span><span>,</span><span> </span><span>long</span><span>,</span><span> </span><span>lat</span><span>)),</span><span> </span><span>fun</span><span> </span><span>=</span><span> </span><span>geosphere</span><span>::</span><span>distGeo</span><span>)</span><span> </span><span>%>%</span><span> </span><span>convert_to_km</span><span>()</span><span>
</span></code></pre>
</div>
<p>I used methods that do not find the optimal tour. This means that probably my solution isn’t the best one, but let’s say it’s ok for this time. Otherwise, the best thing is to ask Concorde’s maintainer if one can use their algorithm which is the best one out there, see <a rel="nofollow" href="http://www.tsp.gatech.edu/concorde/" target="_blank">its terms of use here</a>.</p>
<div>
<pre><code><span>library</span><span>(</span><span>&quot;TSP&quot;</span><span>)</span><span>
</span><span>set.seed</span><span>(</span><span>42</span><span>)</span><span>
</span><span>result0</span><span> </span><span>&lt;-</span><span> </span><span>solve_TSP</span><span>(</span><span>TSP</span><span>(</span><span>distance</span><span>),</span><span> </span><span>method</span><span> </span><span>=</span><span> </span><span>&quot;nearest_insertion&quot;</span><span>)</span><span>
</span><span>result</span><span> </span><span>&lt;-</span><span> </span><span>solve_TSP</span><span>(</span><span>TSP</span><span>(</span><span>distance</span><span>),</span><span> </span><span>method</span><span> </span><span>=</span><span> </span><span>&quot;two_opt&quot;</span><span>,</span><span>
</span><span>control</span><span> </span><span>=</span><span> </span><span>list</span><span>(</span><span>tour</span><span> </span><span>=</span><span> </span><span>result0</span><span>))</span><span>
</span></code></pre>
</div>
<p>And here is how to link the solution to our initial chapters <code>data.frame</code>.</p>
<div>
<pre><code><span>paths</span><span> </span><span>&lt;-</span><span> </span><span>tibble</span><span>::</span><span>tibble</span><span>(</span><span>from</span><span> </span><span>=</span><span> </span><span>chapters_df</span><span>$</span><span>chapter</span><span>[</span><span>as.integer</span><span>(</span><span>result</span><span>)],</span><span>
</span><span>to</span><span> </span><span>=</span><span> </span><span>chapters_df</span><span>$</span><span>chapter</span><span>[</span><span>c</span><span>(</span><span>as.integer</span><span>(</span><span>result</span><span>)[</span><span>2</span><span>:</span><span>41</span><span>],</span><span> </span><span>as.integer</span><span>(</span><span>result</span><span>)[</span><span>1</span><span>])],</span><span> </span><span>trip_id</span><span> </span><span>=</span><span> </span><span>1</span><span>:</span><span>41</span><span>)</span><span>
</span><span>paths</span><span> </span><span>&lt;-</span><span> </span><span>tidyr</span><span>::</span><span>gather</span><span>(</span><span>paths</span><span>,</span><span> </span><span>&quot;property&quot;</span><span>,</span><span> </span><span>&quot;chapter&quot;</span><span>,</span><span> </span><span>1</span><span>:</span><span>2</span><span>)</span><span>
</span><span>paths</span><span> </span><span>&lt;-</span><span> </span><span>dplyr</span><span>::</span><span>left_join</span><span>(</span><span>paths</span><span>,</span><span> </span><span>chapters_df</span><span>,</span><span> </span><span>by</span><span> </span><span>=</span><span> </span><span>&quot;chapter&quot;</span><span>)</span><span>
</span><span>knitr</span><span>::</span><span>kable</span><span>(</span><span>paths</span><span>[</span><span>1</span><span>:</span><span>3</span><span>,])</span><span>
</span></code></pre>
</div>
<table>
<thead>
<tr>
<th>trip_id</th>
<th>property</th>
<th>chapter</th>
<th>long</th>
<th>lat</th>
<th>formatted</th>
<th>id</th>
</tr>
</thead>
<tbody>
<tr>
<td>1</td>
<td>from</td>
<td>Charlottesville, VA</td>
<td>-78.55676</td>
<td>38.08766</td>
<td>Charlottesville, VA, United States of America</td>
<td>38</td>
</tr>
<tr>
<td>2</td>
<td>from</td>
<td>Washington, DC</td>
<td>-77.03665</td>
<td>38.89495</td>
<td>Washington, District of Columbia, United States of America</td>
<td>7</td>
</tr>
<tr>
<td>3</td>
<td>from</td>
<td>New York, NY</td>
<td>-73.98658</td>
<td>40.73060</td>
<td>New York City, United States of America</td>
<td>5</td>
</tr>
</tbody>
</table>
<h1>Plotting the tour, boring version</h1>
<p>I’ll start by plotting the trips as it is done in the vignette, i.e. in a static way. Note: I used Dirk’s code in the Boris Johnson use case for the map, and <a rel="nofollow" href="https://github.com/hrbrmstr/ggalt/issues/33" target="_blank">had to use a particular branch of <code>ggalt</code></a> to get <code>coord_proj</code> working.</p>
<div>
<pre><code><span>library</span><span>(</span><span>&quot;ggplot2&quot;</span><span>)</span><span>
</span><span>library</span><span>(</span><span>&quot;ggalt&quot;</span><span>)</span><span>
</span><span>library</span><span>(</span><span>&quot;ggthemes&quot;</span><span>)</span><span>
</span><span>library</span><span>(</span><span>&quot;ggmap&quot;</span><span>)</span><span>
</span><span>world</span><span> </span><span>&lt;-</span><span> </span><span>map_data</span><span>(</span><span>&quot;world&quot;</span><span>)</span><span> </span><span>%>%</span><span> </span><span>dplyr</span><span>::</span><span>filter</span><span>(</span><span>region</span><span> </span><span>!=</span><span> </span><span>&quot;Antarctica&quot;</span><span>)</span><span>
</span><span>ggplot</span><span>(</span><span>data</span><span> </span><span>=</span><span> </span><span>paths</span><span>,</span><span> </span><span>aes</span><span>(</span><span>long</span><span>,</span><span> </span><span>lat</span><span>))</span><span> </span><span>+</span><span> </span><span>geom_map</span><span>(</span><span>data</span><span> </span><span>=</span><span> </span><span>world</span><span>,</span><span> </span><span>map</span><span> </span><span>=</span><span> </span><span>world</span><span>,</span><span> </span><span>aes</span><span>(</span><span>long</span><span>,</span><span> </span><span>lat</span><span>,</span><span> </span><span>map_id</span><span> </span><span>=</span><span> </span><span>region</span><span>),</span><span> </span><span>fill</span><span> </span><span>=</span><span> </span><span>&quot;white&quot;</span><span>,</span><span> </span><span>color</span><span> </span><span>=</span><span> </span><span>&quot;darkgrey&quot;</span><span>,</span><span> </span><span>alpha</span><span> </span><span>=</span><span> </span><span>0.8</span><span>,</span><span> </span><span>size</span><span> </span><span>=</span><span> </span><span>0.2</span><span>)</span><span> </span><span>+</span><span> </span><span>geom_path</span><span>(</span><span>aes</span><span>(</span><span>group</span><span> </span><span>=</span><span> </span><span>trip_id</span><span>),</span><span> </span><span>color</span><span> </span><span>=</span><span> </span><span>&quot;#88398A&quot;</span><span>)</span><span> </span><span>+</span><span> </span><span>geom_point</span><span>(</span><span>data</span><span> </span><span>=</span><span> </span><span>chapters_df</span><span>,</span><span> </span><span>color</span><span> </span><span>=</span><span> </span><span>&quot;#88398A&quot;</span><span>,</span><span> </span><span>size</span><span> </span><span>=</span><span> </span><span>0.8</span><span>)</span><span> </span><span>+</span><span> </span><span>theme_map</span><span>(</span><span>base_size</span><span> </span><span>=</span><span>20</span><span>)</span><span> </span><span>+</span><span> </span><span>coord_proj</span><span>(</span><span>&quot;+proj=robin +lon_0=0 +x_0=0 +y_0=0 +ellps=WGS84 +datum=WGS84 +units=m +no_defs&quot;</span><span>)</span><span> </span><span>+</span><span> </span><span>ggtitle</span><span>(</span><span>&quot;R-Ladies global tour&quot;</span><span>,</span><span> </span><span>subtitle</span><span> </span><span>=</span><span> </span><span>paste0</span><span>(</span><span>tour_length</span><span>(</span><span>result</span><span>),</span><span> </span><span>&quot; km&quot;</span><span>))</span><span>
</span></code></pre>
</div>
<p><img alt="plot of chunk unnamed-chunk-8" src="https://i1.wp.com/www.masalmon.eu/figure/source/2017-10-06-globalrladiestour/unnamed-chunk-8-1.png?w=456" data-recalc-dims="1"></p>
<p>Dirk told me the map would look better with great circles instead of straight lines so I googled a bit around, asked for help <a rel="nofollow" href="https://twitter.com/ma_salmon/status/915515028702466048" target="_blank">on Twitter</a> before finding <a rel="nofollow" href="http://strimas.com/spatial/long-flights/" target="_blank">this post</a>.</p>
<div>
<pre><code><span>library</span><span>(</span><span>&quot;geosphere&quot;</span><span>)</span><span>
</span><span># find points on great circles between chapters
</span><span>gc_routes</span><span> </span><span>&lt;-</span><span> </span><span>gcIntermediate</span><span>(</span><span>paths</span><span>[</span><span>1</span><span>:</span><span>length</span><span>(</span><span>chapters</span><span>),</span><span> </span><span>c</span><span>(</span><span>&quot;long&quot;</span><span>,</span><span> </span><span>&quot;lat&quot;</span><span>)],</span><span>
</span><span>paths</span><span>[(</span><span>length</span><span>(</span><span>chapters</span><span>)</span><span>+1</span><span>)</span><span>:</span><span>(</span><span>2</span><span>*</span><span>length</span><span>(</span><span>chapters</span><span>)),</span><span> </span><span>c</span><span>(</span><span>&quot;long&quot;</span><span>,</span><span> </span><span>&quot;lat&quot;</span><span>)],</span><span>
</span><span>n</span><span> </span><span>=</span><span> </span><span>360</span><span>,</span><span> </span><span>addStartEnd</span><span> </span><span>=</span><span> </span><span>TRUE</span><span>,</span><span> </span><span>sp</span><span> </span><span>=</span><span> </span><span>TRUE</span><span>,</span><span> </span><span>breakAtDateLine</span><span> </span><span>=</span><span> </span><span>TRUE</span><span>)</span><span>
</span><span>gc_routes</span><span> </span><span>&lt;-</span><span> </span><span>SpatialLinesDataFrame</span><span>(</span><span>gc_routes</span><span>,</span><span> </span><span>data.frame</span><span>(</span><span>id</span><span> </span><span>=</span><span> </span><span>paths</span><span>$</span><span>id</span><span>,</span><span>
</span><span>stringsAsFactors</span><span> </span><span>=</span><span> </span><span>FALSE</span><span>))</span><span>
</span><span>gc_routes_df</span><span> </span><span>&lt;-</span><span> </span><span>fortify</span><span>(</span><span>gc_routes</span><span>)</span><span>
</span></code></pre>
</div>
<div>
<pre><code><span>p</span><span> </span><span>&lt;-</span><span> </span><span>ggplot</span><span>()</span><span> </span><span>+</span><span> </span><span>geom_map</span><span>(</span><span>data</span><span> </span><span>=</span><span> </span><span>world</span><span>,</span><span> </span><span>map</span><span> </span><span>=</span><span> </span><span>world</span><span>,</span><span> </span><span>aes</span><span>(</span><span>long</span><span>,</span><span> </span><span>lat</span><span>,</span><span> </span><span>map_id</span><span> </span><span>=</span><span> </span><span>region</span><span>),</span><span> </span><span>fill</span><span> </span><span>=</span><span> </span><span>&quot;white&quot;</span><span>,</span><span> </span><span>color</span><span> </span><span>=</span><span> </span><span>&quot;darkgrey&quot;</span><span>,</span><span> </span><span>alpha</span><span> </span><span>=</span><span> </span><span>0.8</span><span>,</span><span> </span><span>size</span><span> </span><span>=</span><span> </span><span>0.2</span><span>)</span><span> </span><span>+</span><span> </span><span>geom_path</span><span>(</span><span>data</span><span> </span><span>=</span><span> </span><span>gc_routes_df</span><span>,</span><span> </span><span>aes</span><span>(</span><span>long</span><span>,</span><span> </span><span>lat</span><span>,</span><span> </span><span>group</span><span> </span><span>=</span><span> </span><span>group</span><span>),</span><span> </span><span>alpha</span><span> </span><span>=</span><span> </span><span>0.5</span><span>,</span><span> </span><span>color</span><span> </span><span>=</span><span> </span><span>&quot;#88398A&quot;</span><span>)</span><span> </span><span>+</span><span> </span><span>geom_point</span><span>(</span><span>data</span><span> </span><span>=</span><span> </span><span>chapters_df</span><span>,</span><span> </span><span>color</span><span> </span><span>=</span><span> </span><span>&quot;#88398A&quot;</span><span>,</span><span> </span><span>size</span><span> </span><span>=</span><span> </span><span>0.8</span><span>,</span><span>
</span><span>aes</span><span>(</span><span>long</span><span>,</span><span> </span><span>lat</span><span>))</span><span> </span><span>+</span><span> </span><span>theme_map</span><span>(</span><span>base_size</span><span> </span><span>=</span><span>20</span><span>)</span><span>+</span><span> </span><span>coord_proj</span><span>(</span><span>&quot;+proj=robin +lon_0=0 +x_0=0 +y_0=0 +ellps=WGS84 +datum=WGS84 +units=m +no_defs&quot;</span><span>)</span><span>
</span><span>p</span><span> </span><span>+</span><span> </span><span>ggtitle</span><span>(</span><span>&quot;R-Ladies global tour&quot;</span><span>,</span><span> </span><span>subtitle</span><span> </span><span>=</span><span> </span><span>paste0</span><span>(</span><span>tour_length</span><span>(</span><span>result</span><span>),</span><span> </span><span>&quot; km&quot;</span><span>))</span><span>
</span></code></pre>
</div>
<p><img alt="plot of chunk unnamed-chunk-10" src="https://i2.wp.com/www.masalmon.eu/figure/source/2017-10-06-globalrladiestour/unnamed-chunk-10-1.png?w=456" data-recalc-dims="1"></p>
<p>Ok this is nicer, it was worth the search.</p>
<h1>Plotting the tour, magical version</h1>
<p>And now I’ll use <code>magick</code> because I want to add a small star flying around the world. By the way if this global tour were to happen I reckon that one would need to donate a lot of money to rainforest charities or the like, because it’d have a huge carbon footprint! Too bad really, I don’t want my gif to promote planet destroying behaviours.</p>
<p>To make the gif I used code similar to the one <a rel="nofollow" href="http://www.masalmon.eu/2017/02/18/complot/" target="_blank">shared in this post</a> but in a better version thanks to Jeroen who told me to <a rel="nofollow" href="https://cran.r-project.org/web/packages/magick/vignettes/intro.html#animated_graphics" target="_blank">read the vignette again</a>. Not saving PNGs saves time!</p>
<p>I first wanted to really show the emoji flying along the route and even created data for that, with a number of rows between chapters proportional to the distance between them. It’d have looked nice and smooth. But making a gif with hundreds of frames ended up being too long for me at the moment. So I came up with another idea, I’ll have to hope you like it!</p>
<div>
<pre><code><span>library</span><span>(</span><span>&quot;emojifont&quot;</span><span>)</span><span>
</span><span>load.emojifont</span><span>(</span><span>'OpenSansEmoji.ttf'</span><span>)</span><span>
</span><span>library</span><span>(</span><span>&quot;magick&quot;</span><span>)</span><span>
</span><span>plot_one_moment</span><span> </span><span>&lt;-</span><span> </span><span>function</span><span>(</span><span>chapter</span><span>,</span><span> </span><span>size</span><span>,</span><span> </span><span>p</span><span>,</span><span>
</span><span>chapters_df</span><span>){</span><span>
</span><span>print</span><span>(</span><span>p</span><span> </span><span>+</span><span> </span><span>ggtitle</span><span>(</span><span>paste0</span><span>(</span><span>&quot;R-Ladies global tour, &quot;</span><span>,</span><span>
</span><span>chapters_df</span><span>[</span><span>chapters_df</span><span>$</span><span>chapter</span><span> </span><span>==</span><span> </span><span>chapter</span><span>,]</span><span>$</span><span>chapter</span><span>),</span><span> </span><span>subtitle</span><span> </span><span>=</span><span> </span><span>paste0</span><span>(</span><span>tour_length</span><span>(</span><span>result</span><span>),</span><span> </span><span>&quot; km&quot;</span><span>))</span><span>+</span><span> </span><span>geom_text</span><span>(</span><span>data</span><span> </span><span>=</span><span> </span><span>chapters_df</span><span>[</span><span>chapters_df</span><span>$</span><span>chapter</span><span> </span><span>==</span><span> </span><span>chapter</span><span>,],</span><span> </span><span>aes</span><span>(</span><span>x</span><span> </span><span>=</span><span> </span><span>long</span><span>,</span><span> </span><span>y</span><span> </span><span>=</span><span> </span><span>lat</span><span>,</span><span>
</span><span>label</span><span> </span><span>=</span><span> </span><span>emoji</span><span>(</span><span>&quot;star2&quot;</span><span>)),</span><span>
</span><span>family</span><span>=</span><span>&quot;OpenSansEmoji&quot;</span><span>,</span><span>
</span><span>size</span><span> </span><span>=</span><span> </span><span>size</span><span>))</span><span>
</span><span>}</span><span>
</span><span>img</span><span> </span><span>&lt;-</span><span> </span><span>image_graph</span><span>(</span><span>1000</span><span>,</span><span> </span><span>800</span><span>,</span><span> </span><span>res</span><span> </span><span>=</span><span> </span><span>96</span><span>)</span><span>
</span><span>out</span><span> </span><span>&lt;-</span><span> </span><span>purrr</span><span>::</span><span>walk2</span><span>(</span><span>rep</span><span>(</span><span>chapters</span><span>[</span><span>as.integer</span><span>(</span><span>result</span><span>)],</span><span> </span><span>each</span><span> </span><span>=</span><span> </span><span>2</span><span>),</span><span>
</span><span>rep</span><span>(</span><span>c</span><span>(</span><span>5</span><span>,</span><span> </span><span>10</span><span>),</span><span> </span><span>length</span><span> </span><span>=</span><span> </span><span>length</span><span>(</span><span>chapters</span><span>)</span><span>*</span><span>2</span><span>),</span><span>
</span><span>p</span><span> </span><span>=</span><span> </span><span>p</span><span>,</span><span>
</span><span>plot_one_moment</span><span>,</span><span>
</span><span>chapters_df</span><span> </span><span>=</span><span> </span><span>chapters_df</span><span>)</span><span> </span><span>dev.off</span><span>()</span><span>
</span></code></pre>
</div>
<div>
<pre><code>## png 
##   2
</code></pre>
</div>
<div>
<pre><code><span>image_animate</span><span>(</span><span>img</span><span>,</span><span> </span><span>fps</span><span>=</span><span>1</span><span>)</span><span> </span><span>%>%</span><span>
</span><span>image_write</span><span>(</span><span>&quot;rladiesglobal.gif&quot;</span><span>)</span><span>
</span></code></pre>
</div>
<p><img alt="" src="https://i0.wp.com/github.com/masalmon/masalmon.github.io/blob/master/_source/rladiesglobal.gif?w=456&ssl=1" data-recalc-dims="1"></p>
<p>At least I made a twinkling star. I hope Hannah will be happy with the gif, because now I’d like to just dream of potential future trips! Or learn a bit of geography by looking at the gif.</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://www.masalmon.eu/2017/10/06/globalrladiestour/"> Maëlle</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/I32UUznb5eg" height="1"></a></div>