<p><div><div><div><div data-width="100" data-show-faces="false" data-send="true" data-layout="button_count" data-height="21" data-href="https://www.r-bloggers.com/create-air-travel-route-maps-in-ggplot-a-visual-travel-diary/"></div></div><div></div></div><div></div></div>
<div>
(This article was first published on <strong><a href="http://r.prevos.net/create-air-travel-route-maps/"> The Devil is in the Data</a></strong>, and kindly contributed to <a rel="nofollow" href="https://www.r-bloggers.com/">R-bloggers)</a> </div>
<p><img alt="Create Air Travel Route Maps: Emirates Route Map" src="https://i0.wp.com/travelersunited.org/wp-content/uploads/Emirates-routemap.jpg?resize=300%2C201&ssl=1" data-recalc-dims="1">I have been lucky to fly to a few countries around the world. Like any other bored traveller, I thumb through the airline magazines and look at the <a target="_blank" rel="nofollow" href="http://www.airlineroutemaps.com/">air travel route maps</a>. These maps are beautifully stylised depictions of the world with gently curved lines between the many destinations. I always wanted such a map for my own travel adventures.</p>
<h2>Create Air Travel Route Maps using ggplot2</h2>
<p>The first step was to create a list of all the places I have flown between at least once. Paging through my travel photos and diaries, I managed to create a pretty complete list. The structure of this document is simply a <a target="_blank" rel="nofollow" href="https://docs.google.com/spreadsheets/d/1R8Bn6qvvvxH-rSl3lIC3UhSD3iQPOFtQ9JlcLqHhDDk/edit?usp=sharing">list of all routes</a> (From, To) and every flight only gets counted once. The next step finds the spatial coordinates for each airport by searching Google Maps using the geocode function from the <a target="_blank" rel="nofollow" href="https://cran.r-project.org/web/packages/ggmap/index.html">ggmap</a> package. In some instances, I had to add the country name to avoid confusion between places.</p>
<pre>
# Read flight list
flights &lt;- read.csv(&quot;flights.csv&quot;, stringsAsFactors = FALSE)

# Lookup coordinates
library(ggmap)
airports &lt;- unique(c(flights$From, flights$To))
coords &lt;- geocode(airports)
airports &lt;- data.frame(airport=airports, coords)
</pre>
<p>We now we have a data frame of airports with their coordinates and can create air travel route maps. The data frames are merged so that we can create air travel route maps using the <a target="_blank" rel="nofollow" href="http://docs.ggplot2.org/current/geom_segment.html">curve geom</a>. The <a target="_blank" rel="nofollow" href="http://docs.ggplot2.org/current/borders.html">borders</a> function of ggplot2 creates the map data. The <a target="_blank" rel="nofollow" href="https://github.com/slowkow/ggrepel/blob/master/vignettes/ggrepel.md">ggrepel</a> package helps to prevent overplotting of text.</p>
<pre>
# Add coordinates to flight list
flights &lt;- merge(flights, airports, by.x=&quot;To&quot;, by.y=&quot;airport&quot;)
flights &lt;- merge(flights, airports, by.x=&quot;From&quot;, by.y=&quot;airport&quot;)

# Plot flight routes
library(ggplot2)
library(ggrepel)
worldmap &lt;- borders(&quot;world&quot;, colour=&quot;#efede1&quot;, fill=&quot;#efede1&quot;) # create a layer of borders
ggplot() + worldmap + 
 geom_curve(data=flights, aes(x = lon.x, y = lat.x, xend = lon.y, yend = lat.y), col = &quot;#b29e7d&quot;, size = 1, curvature = .2) + 
 geom_point(data=airports, aes(x = lon, y = lat), col = &quot;#970027&quot;) + 
 geom_text_repel(data=airports, aes(x = lon, y = lat, label = airport), col = &quot;black&quot;, size = 2, segment.color = NA) + 
 theme(panel.background = element_rect(fill=&quot;white&quot;), 
 axis.line = element_blank(),
 axis.text.x = element_blank(),
 axis.text.y = element_blank(),
 axis.ticks = element_blank(),
 axis.title.x = element_blank(),
 axis.title.y = element_blank()
 )
</pre>
<p>I also tried to use ggmap package to display the maps to get a satellite image background. This did not work because the curve geom struggles with the map projection methods used in ggmap. Another problem is that the flight from Auckland to Los Angeles is drawn the wrong way. I hope no <a target="_blank" rel="nofollow" href="https://www.theflatearthsociety.org/home/">flat-earthers</a> will see this map because they might use it as prove that the world is flat.</p>
<p><a target="_blank" rel="nofollow" href="http://r.prevos.net/create-air-travel-route-maps/flights/"><img sizes="(max-width: 584px) 100vw, 584px" data-image-description="" data-orig-file="https://i0.wp.com/r.prevos.net/wp-content/uploads/sites/11/2017/03/flights.png?fit=2178%2C1287" data-orig-size="2178,1287" data-image-title="flights" class="wp-image-466" data-comments-opened="1" data-recalc-dims="1" data-attachment-id="466" data-permalink="http://r.prevos.net/create-air-travel-route-maps/flights/" alt="" data-medium-file="https://i0.wp.com/r.prevos.net/wp-content/uploads/sites/11/2017/03/flights.png?fit=300%2C177" data-large-file="https://i0.wp.com/r.prevos.net/wp-content/uploads/sites/11/2017/03/flights.png?fit=584%2C345" data-image-meta="{&quot;aperture&quot;:&quot;0&quot;,&quot;credit&quot;:&quot;&quot;,&quot;camera&quot;:&quot;&quot;,&quot;caption&quot;:&quot;&quot;,&quot;created_timestamp&quot;:&quot;0&quot;,&quot;copyright&quot;:&quot;&quot;,&quot;focal_length&quot;:&quot;0&quot;,&quot;iso&quot;:&quot;0&quot;,&quot;shutter_speed&quot;:&quot;0&quot;,&quot;title&quot;:&quot;&quot;,&quot;orientation&quot;:&quot;0&quot;}" src="https://i0.wp.com/r.prevos.net/wp-content/uploads/sites/11/2017/03/flights.png?resize=584%2C345"></a></p>
<h2>Alternative Visualisation</h2>
<p>Another way of visualising this type of data is using a network diagram provided by the <a target="_blank" rel="nofollow" href="http://igraph.org/r/">igraph</a> package. This visualisation shows the logic between the locations and not their spatial locations.</p>
<pre>
# Network visualisation
library(igraph)
edgelist &lt;- as.matrix(flights[c(&quot;From&quot;, &quot;To&quot;)])
g &lt;- graph_from_edgelist(edgelist, directed = TRUE)
g &lt;- simplify(g)
par(mar=rep(0,4))
plot.igraph(g, 
 edge.arrow.size=0,
 edge.color=&quot;black&quot;,
 edge.curved=TRUE,
 edge.width=2,
 vertex.size=3,
 vertex.color=NA, 
 vertex.frame.color=NA, 
 vertex.label=V(g)$name,
 vertex.label.cex=3,
 layout=layout.fruchterman.reingold
)
</pre>
<p><a target="_blank" rel="nofollow" href="http://r.prevos.net/create-air-travel-route-maps/flight_network/"><img sizes="(max-width: 584px) 100vw, 584px" data-image-description="" data-orig-file="https://i2.wp.com/r.prevos.net/wp-content/uploads/sites/11/2017/03/flight_network.png?fit=2048%2C2048" data-orig-size="2048,2048" data-image-title="flight_network" data-comments-opened="1" class="wp-image-462" data-recalc-dims="1" data-attachment-id="462" data-permalink="http://r.prevos.net/create-air-travel-route-maps/flight_network/" alt="" data-medium-file="https://i2.wp.com/r.prevos.net/wp-content/uploads/sites/11/2017/03/flight_network.png?fit=300%2C300" data-large-file="https://i2.wp.com/r.prevos.net/wp-content/uploads/sites/11/2017/03/flight_network.png?fit=584%2C584" data-image-meta="{&quot;aperture&quot;:&quot;0&quot;,&quot;credit&quot;:&quot;&quot;,&quot;camera&quot;:&quot;&quot;,&quot;caption&quot;:&quot;&quot;,&quot;created_timestamp&quot;:&quot;0&quot;,&quot;copyright&quot;:&quot;&quot;,&quot;focal_length&quot;:&quot;0&quot;,&quot;iso&quot;:&quot;0&quot;,&quot;shutter_speed&quot;:&quot;0&quot;,&quot;title&quot;:&quot;&quot;,&quot;orientation&quot;:&quot;0&quot;}" src="https://i2.wp.com/r.prevos.net/wp-content/uploads/sites/11/2017/03/flight_network.png?resize=584%2C584"></a></p>
<p>The post <a target="_blank" rel="nofollow" href="http://r.prevos.net/create-air-travel-route-maps/">Create Air Travel Route Maps in ggplot: A Visual Travel Diary</a> appeared first on <a target="_blank" rel="nofollow" href="http://r.prevos.net/">The Devil is in the Data</a>.</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://r.prevos.net/create-air-travel-route-maps/"> The Devil is in the Data</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 title="The R Project for Statistical Computing" rel="nofollow" href="https://www.r-project.org/">R</a> news and <a title="R tutorials" rel="nofollow" href="https://www.r-bloggers.com/search/tutorial">tutorials</a> on topics such as: <a title="Data science" rel="nofollow" href="https://www.r-bloggers.com/search/data%20science">Data science</a>, <a title="Big Data" rel="nofollow" href="https://www.r-bloggers.com/search/Big%20Data">Big Data, <a title="R jobs" rel="nofollow" href="https://www.r-users.com/">R jobs</a>, visualization (<a title="ggplot and ggplot2 tutorials" rel="nofollow" href="https://www.r-bloggers.com/search/ggplot2">ggplot2</a>, <a title="Boxplots using lattice and ggplot2 tutorials" rel="nofollow" href="https://www.r-bloggers.com/search/boxplot">Boxplots</a>, <a title="Maps and gis" rel="nofollow" href="https://www.r-bloggers.com/search/map">maps</a>, <a title="Animation in R" rel="nofollow" href="https://www.r-bloggers.com/search/animation">animation</a>), programming (<a title="RStudio IDE for R" rel="nofollow" href="https://www.r-bloggers.com/search/RStudio">RStudio</a>, <a title="Sweave and literate programming" rel="nofollow" href="https://www.r-bloggers.com/search/sweave">Sweave</a>, <a title="LaTeX in R" rel="nofollow" href="https://www.r-bloggers.com/search/LaTeX">LaTeX</a>, <a title="SQL and databases" rel="nofollow" href="https://www.r-bloggers.com/search/SQL">SQL</a>, <a title="Eclipse IDE for R" rel="nofollow" href="https://www.r-bloggers.com/search/eclipse">Eclipse</a>, <a title="git and github, Version Control System" rel="nofollow" href="https://www.r-bloggers.com/search/git">git</a>, <a title="Large data in R using Hadoop" rel="nofollow" href="https://www.r-bloggers.com/search/hadoop">hadoop</a>, <a title="Web Scraping of google, facebook, yahoo, twitter and more using R" rel="nofollow" href="https://www.r-bloggers.com/search/Web+Scraping">Web Scraping</a>) statistics (<a title="Regressions and ANOVA analysis tutorials" rel="nofollow" href="https://www.r-bloggers.com/search/regression">regression</a>, <a title="principal component analysis tutorial" rel="nofollow" href="https://www.r-bloggers.com/search/PCA">PCA</a>, <a title="Time series" rel="nofollow" href="https://www.r-bloggers.com/search/time+series">time series</a>, <a title="finance trading" rel="nofollow" href="https://www.r-bloggers.com/search/trading">trading</a>) and more...
<img height="1" alt="" width="1" src="http://feeds.feedburner.com/~r/RBloggers/~4/0Gv3psknWV0"></a></div>