<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/pacific-island-hopping-using-r-and-igraph/"></div></div><div></div></div><div></div></div>
<div>
(This article was first published on <strong><a href="http://r.prevos.net/pacific-island-hopping/"> 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>Last month I enjoyed a relaxing holiday in the <a rel="nofollow" href="https://horizonofreason.com/vanuatu-sand-drawings-sandroings/" target="_blank">tropical paradise of Vanuatu</a>. One rainy day I contemplated how to go island hopping across the Pacific ocean visiting as many island nations as possible. The Pacific ocean is a massive body of water between, Asia and the Americas, which covers almost half the surface of the earth. The southern Pacific is strewn with island nations from Australia to Chile. In this post, I describe how to use R to plan your next Pacific island hopping journey.</p>
<div class="wp-caption"><img alt="Pacific Island hopping" src="https://i1.wp.com/upload.wikimedia.org/wikipedia/commons/thumb/5/54/Oceania_UN_Geoscheme_Regions.svg/800px-Oceania_UN_Geoscheme_Regions.svg.png?resize=584%2C337&ssl=1" data-recalc-dims="1">
<p class="wp-caption-text">The Pacific Ocean.</p>
</div>
<h2>Listing all airports</h2>
<p>My first step was to create a list of flight connections between each of the island nations in the Pacific ocean. I am not aware of a publically available data set of international flights so unfortunately, I created a list manually (if you do know of such data set, then please leave a comment).</p>
<p>My manual research resulted in <a rel="nofollow" href="https://github.com/pprevos/r.prevos.net/blob/master/Geography/PacificFlights.csv" target="_blank">a list of international flights from or to island airports</a>. This list might not be complete, but it is a start. My Pinterest board with Pacific island airline route maps was the information source for this list.</p>
<p><a rel="nofollow" data-pin-do="embedBoard" href="https://au.pinterest.com/pprevos/pacific-airline-route-maps/" target="_blank"></a></p>
<p>The first code section reads the list of airline routes and uses the <code>ggmap</code> package to extract their coordinates from Google maps. The data frame with airport coordinates is saved for future reference to avoid repeatedly pinging Google for the same information.</p>
<pre>
# Init
library(tidyverse)
library(ggmap)
library(ggrepel)
library(geosphere)

# Read flight list and airport list
flights &lt;- read.csv(&quot;Geography/PacificFlights.csv&quot;, stringsAsFactors = FALSE)
f &lt;- &quot;Geography/airports.csv&quot;
if (file.exists(f)) {
    airports &lt;- read.csv(f)
    } else airports &lt;- data.frame(airport = NA, lat = NA, lon = NA)

# Lookup coordinates for new airports
all_airports &lt;- unique(c(flights$From, flights$To))
new_airports &lt;- all_airports[!(all_airports %in% airports$airport)]
if (length(new_airports) != 0) {
    coords &lt;- geocode(new_airports)
    new_airports &lt;- data.frame(airport = new_airports, coords)
    airports &lt;- rbind(airports, new_airports)
    airports &lt;- subset(airports, !is.na(airport))
    write.csv(airports, &quot;Geography/airports.csv&quot;, row.names = FALSE)
}

# Add coordinates to flight list
flights &lt;- merge(flights, airports, by.x=&quot;From&quot;, by.y=&quot;airport&quot;)
flights &lt;- merge(flights, airports, by.x=&quot;To&quot;, by.y=&quot;airport&quot;)
</pre>
<h2>Create the map</h2>
<p>To create a map, I modified the code to create <a rel="nofollow" href="http://r.prevos.net/create-air-travel-route-maps/" target="_blank">flight maps</a> I published in an earlier post. This code had to be changed to centre the map on the Pacific. Mapping the Pacific ocean is problematic because the -180 and +180 degree meridians meet around the date line. Longitudes west of the antemeridian are positive, while longitudes east are negative.</p>
<p>The <code>world2</code> data set in the borders function of the <code>ggplot2</code> package is centred on the Pacific ocean. To enable plotting on this map, all negative longitudes are made positive by adding 360 degrees to them.</p>
<pre>
# Pacific centric
flights$lon.x[flights$lon.x &lt; 0] &lt;- flights$lon.x[flights$lon.x &lt; 0] + 360
flights$lon.y[flights$lon.y &lt; 0] &lt;- flights$lon.y[flights$lon.y &lt; 0] + 360
airports$lon[airports$lon &lt; 0] &lt;- airports$lon[airports$lon &lt; 0] + 360

# Plot flight routes
worldmap &lt;- borders(&quot;world2&quot;, colour=&quot;#efede1&quot;, fill=&quot;#efede1&quot;)
ggplot() + worldmap + 
    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) + 
    geom_curve(data=flights, aes(x = lon.x, y = lat.x, xend = lon.y, 
      yend = lat.y, col = Airline), size = .4, curvature = .2) + 
    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()
          ) + 
    xlim(100, 300) + ylim(-40,40)
</pre>
<h2><a rel="nofollow" href="http://r.prevos.net/?attachment_id=836" target="_blank"><img data-orig-file="https://i2.wp.com/r.prevos.net/wp-content/uploads/sites/11/2017/08/pacifc_flights.png?fit=2271%2C1168" data-orig-size="2271,1168" data-medium-file="https://i2.wp.com/r.prevos.net/wp-content/uploads/sites/11/2017/08/pacifc_flights.png?fit=300%2C154" data-attachment-id="836" data-image-meta="{" data-image-description="" src="https://i2.wp.com/r.prevos.net/wp-content/uploads/sites/11/2017/08/pacifc_flights.png?resize=584%2C300" data-permalink="http://r.prevos.net/pacific-island-hopping/pacifc_flights/" data-large-file="https://i2.wp.com/r.prevos.net/wp-content/uploads/sites/11/2017/08/pacifc_flights.png?fit=584%2C301" alt="Pacific island hopping" data-image-title="Pacific island hopping" sizes="(max-width: 584px) 100vw, 584px" data-comments-opened="1" class="wp-image-836" data-recalc-dims="1"></a></h2>
<h2>Pacific Island Hopping</h2>
<p>This visualisation is aesthetic and full of context, but it is not the best visualisation to solve the travel problem. This map can also be expressed as a graph with nodes (airports) and edges (routes). Once the map is represented mathematically, we can generate travel routes and begin our Pacific Island hopping.</p>
<p>The igraph package converts the flight list to a graph that can be analysed and plotted. The <code>shortest_path</code> function can then be used to plan routes. If I would want to travel from Auckland to Saipan in the Northern Mariana Islands, I have to go through Port Vila, Honiara, Port Moresby, Chuuk, Guam and then to Saipan. I am pretty sure there are quicker ways to get there, but this would be an exciting journey through the Pacific.</p>
<pre>
library(igraph)
g &lt;- graph_from_edgelist(as.matrix(flights[,1:2]), directed = FALSE)
par(mar = rep(0, 4))
plot(g, layout = layout.fruchterman.reingold, vertex.size=0)
V(g)
shortest_paths(g, &quot;Auckland&quot;, &quot;Saipan&quot;)
</pre>
<p>View the latest version of this code on <a rel="nofollow" href="https://github.com/pprevos/r.prevos.net/tree/master/Geography" target="_blank">GitHub</a>.</p>
<p><a rel="nofollow" href="http://r.prevos.net/?attachment_id=982" target="_blank"><img data-orig-file="https://i0.wp.com/r.prevos.net/wp-content/uploads/sites/11/2017/08/PacificNetwork.png?fit=958%2C907" data-orig-size="958,907" data-medium-file="https://i0.wp.com/r.prevos.net/wp-content/uploads/sites/11/2017/08/PacificNetwork.png?fit=300%2C284" data-attachment-id="982" data-image-meta="{" data-image-description="" src="https://i0.wp.com/r.prevos.net/wp-content/uploads/sites/11/2017/08/PacificNetwork.png?resize=584%2C553" data-permalink="http://r.prevos.net/pacific-island-hopping/pacificnetwork/" data-large-file="https://i0.wp.com/r.prevos.net/wp-content/uploads/sites/11/2017/08/PacificNetwork.png?fit=584%2C553" alt="Pacific Flight network" data-image-title="PacificNetwork" sizes="(max-width: 584px) 100vw, 584px" data-comments-opened="1" class="wp-image-982" data-recalc-dims="1"></a></p>
<p>The post <a rel="nofollow" href="http://r.prevos.net/pacific-island-hopping/" target="_blank">Pacific Island Hopping using R and iGraph</a> appeared first on <a rel="nofollow" href="http://r.prevos.net/" target="_blank">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/pacific-island-hopping/"> 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 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 alt="" width="1" src="http://feeds.feedburner.com/~r/RBloggers/~4/lEfaS6Vinx8" height="1"></a></div>