<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/oh-the-places-youll-go-mapping-packages/"></div></div><div></div></div><div></div></div>
<div>
(This article was first published on <strong><a href="https://www.mango-solutions.com/blog/oh-the-places-you-ll-go-mapping-packages"> Blog</a></strong>, and kindly contributed to <a rel="nofollow" href="https://www.r-bloggers.com/">R-bloggers)</a> </div>
<p> <img sizes="100vw" src="https://i2.wp.com/www.mango-solutions.com/blog/images/3/6/a/5/3/36a53c6dd32705dc91f9361b0b9421f040544b61-1.png?w=456&ssl=1" data-recalc-dims="1"></p>
<h5>by Aimée Gott, Senior Consultant</h5>
<p>If you follow us on <a rel="nofollow" href="https://twitter.com/MangoTheCat" target="_blank">Twitter</a> you might have noticed that Mango has been doing a bit of travelling this summer. He’s been to San Francisco for the EARL conference, Brussels for UseR, as well as a few other places to teach some training courses. He even managed to sneak in a little holiday in LA where he was disappointed to find there are no cats honoured on the Hollywood walk of fame.</p>
<p><img alt="" sizes="100vw" src="https://i2.wp.com/www.mango-solutions.com/blog/user/pages/01.blog/126.oh-the-places-you-ll-go-mapping-packages/1.png?w=456&ssl=1" data-recalc-dims="1"></p>
<p>It was when we landed in LA that we got talking about the largest airports in the world (me and our marketing manager Karis – don’t worry, I am not talking to stuffed cats just yet!). After a little guessing Google came to the rescue, and I was quite surprised by the airports that topped the list. That conversation stuck with me through the many airports I have since passed through and it came back to me again after seeing James Cheshire give another inspiring talk at LondonR recently. </p>
<p>James gave a great talk full of inspiring visualisations —many map based— that made me think it was about time to dust off my notes on mapping tools in R and have a play. Along the way I got slightly distracted by the data itself but it was a great chance to refresh myself on the pros and cons of a couple of my favourite mapping packages – ggmap and leaflet.</p>
<h5>The data </h5>
<p>I didn’t want to spend long collecting and cleaning so I used data available on <a rel="nofollow" href="https://en.wikipedia.org/wiki/List_of_busiest_airports_by_passenger_traffic" target="_blank">Wikipedia</a> showing the top 50 airports in terms of passenger numbers from 2000 to 2016. Frustratingly, for 2000 to 2004 the data for only 30 airports were available. For this reason, I only used the top 30 for all years. </p>
<h5><code>ggmap</code> package</h5>
<p>I started out looking at <code>ggmap</code>. In general, my mapping requirements are not worldwide data but local data —country or region level— so this is a great tool. Because it’s built on top of <code>ggplot2</code> it meant I didn’t have to learn another tool, but simply needed to extend the capability I already have. Unfortunately, it’s not the best tool for worldwide data as you can’t zoom to show the whole world map. Because of this, I decided to focus on Europe. </p>
<p><img alt="" sizes="100vw" src="https://i2.wp.com/www.mango-solutions.com/blog/user/pages/01.blog/126.oh-the-places-you-ll-go-mapping-packages/Map%20image%20-%201.png?w=456&ssl=1" data-recalc-dims="1"></p>
<p>With very little effort I was able to use <code>ggmap</code> to obtain the latitude and longitude of the airports in the data, generate a Google map of Europe and overlay the airports, sized based on the number of passengers that passed through them in the given year. The geocode function was in fact what made all of this task much simpler, as it can find the locations of the airports automatically rather than having to search for additional data. </p>
<p>I didn’t like the Google map though and decided to play with some of the other options. There are quite a few for the base map in <code>ggmap</code> – some from Google, such as satellite, and some completely different ones like ‘watercolor’, which gives a completely different look that I personally like because it takes away a lot of the additional information that we don’t really need in this particular plot i.e. borders – although I would have liked to see some of the major cities.</p>
<p><img alt="" src="https://i1.wp.com/www.mango-solutions.com/blog/user/pages/01.blog/126.oh-the-places-you-ll-go-mapping-packages/Map%20image%20-%202.png?w=456&ssl=1" data-recalc-dims="1"></p>
<h5><code>Leaflet</code> package</h5>
<p>Moving on to <code>leaflet</code> I was reminded that I need to use it more often. It’s (almost too) rich in features and I had to stop myself from making my dots appear as planes. </p>
<p>In terms of the code, it uses <code>magrittr</code> to add layers to visualisations and it wasn’t a lot of work to get the data appearing on a world map with pop-ups to identify the airport, year and passenger numbers. </p>
<p><img alt="" sizes="100vw" src="https://i1.wp.com/www.mango-solutions.com/blog/user/pages/01.blog/126.oh-the-places-you-ll-go-mapping-packages/Map%20image%20-%203.png?w=456&ssl=1" data-recalc-dims="1"></p>
<p>The biggest challenge was setting the relative sizes of the points. Unlike <code>ggmap</code> —which uses <code>ggplot2</code> and handles the sizing for us— we have to be more specific with <code>leaflet</code>. It took a few runs of the plot to get to a size I was happy with. But once I was at this point I could easily repeat the layer to view other years on the same graphic. This is more clunky as you have to manually filter the data and add the layers but the trade-off is that you can add switches to turn layers on and off. </p>
<p><img alt="" sizes="100vw" src="https://i1.wp.com/www.mango-solutions.com/blog/user/pages/01.blog/126.oh-the-places-you-ll-go-mapping-packages/Map%20image%20-%204.png?w=456&ssl=1" data-recalc-dims="1"></p>
<p>This was where I got distracted by the data. By being able to see the global data for multiple years in one graphic it was clear that over the last 16 years there has been a shift in the location of the airports that carry the most passengers from North America to Asia. </p>
<p><img alt="" sizes="100vw" src="https://i0.wp.com/www.mango-solutions.com/blog/user/pages/01.blog/126.oh-the-places-you-ll-go-mapping-packages/Map%20image%20-%205@1x.png?w=456&ssl=1" data-recalc-dims="1"></p>
<p>So, one last graphic took me back to <code>ggplot2</code>. The data has been scaled to account for the fact that air passenger numbers have continued to increase over the last 20 years. Interestingly, there has been a very clear increase in the number of passengers travelling through Asian airports; in fact, in 2016 half of the top 30 airports were located in Asia. </p>
<p><img alt="" sizes="100vw" src="https://i0.wp.com/www.mango-solutions.com/blog/user/pages/01.blog/126.oh-the-places-you-ll-go-mapping-packages/map%20image%20-%206.png?w=456&ssl=1" data-recalc-dims="1"></p>
<h5>Is there a better package?</h5>
<p>To return to the two packages, they both have their strengths and weaknesses and it would really depend on my needs as to which I would chose for a particular visualisation. For interactivity, I would without a doubt go straight to <code>leaflet</code>, for something more suited to local data or intended for static reporting <code>ggmap</code> would be my preference. </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="https://www.mango-solutions.com/blog/oh-the-places-you-ll-go-mapping-packages"> Blog</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/e9hAHCYAqyg" height="1"></a></div>