Pacific Island Hopping using R and iGraph

Last month I enjoyed a relaxing holiday in the tropical paradise of Vanuatu. 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. View the latest version of this code on GitHub.

Pacific Island hopping
The Pacific Ocean.

Listing all airports

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 the list manually by collecting route maps on Pinterest (if you do know of a fomal data set with international flights, then please leave a comment).

My manual research resulted in a list of international flights from or to island airports. 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.

The first code section reads the list of airline routes and uses the ggmap 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.


## Read flight list and airport list
flights <- read_csv("PacificFlights.csv")
f <- "pacific_airports.csv"
if (file.exists(f)) {
    airports <- read.csv(f)
} else
    airports <- data.frame(airport = NA, lat = NA, lon = NA)

## Lookup coordinates for new airports
all_airports <- unique(c(flights$From, flights$To))
new_airports <- all_airports[!(all_airports %in% airports$airport)]
while (length(new_airports) != 0) {
    coords &lt;- geocode(new_airports)
    temp_airports &lt;- data.frame(airport = new_airports, coords)
    airports &lt;- rbind(airports, temp_airports) %&gt;%
        filter(!, !
    new_airports &lt;- all_airports[!(all_airports %in% airports$airport)]
write_csv(airports, f)

# 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;)

Create the map

To create a map, I modified the code to create flight maps 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 dateline. Longitudes west of the antemeridian are positive, while longitudes east are negative. The world2 data set in the borders function of the ggplot2 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 and defining the antipode.

airports$airport <- as.character(airports$airport)
comma <- regexpr(",", airports$airport) 
airports$airport[which(comma > 0)] <- substr(airports$airport[which(comma > 0)], 1, comma[comma >; 0] - 1)

# Pacific centric
flights$lon.x[flights$lon.x < 0] &lt;- flights$lon.x[flights$lon.x < 0] + 360
flights$lon.y[flights$lon.y < 0] &lt;- flights$lon.y[flights$lon.y &lt; 0] + 360
airports$lon[airports$lon < 0] &lt;- airports$lon[airports$lon &lt; 0] + 360

# Plot flight routes
worldmap &lt;- borders(&quot;world2&quot;, fill = &quot;grey&quot;) # create a layer of borders
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 = 1, curvature = .2) +
    xlim(90, 300) + ylim(-50, 50) + 
ggsave(&quot;pacifc_flights.png&quot;, dpi = 300)
Pacific island hopping
Pacific island hopping – flight route map.

Pacific Island Hopping

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.

The igraph package converts the flight list to a graph that can be analysed and plotted. The shortest_path 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.

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)
shortest_paths(g, &quot;Auckland&quot;, &quot;Saipan&quot;)
Pacific Flight network
Pacific island hopping – network model

6 Replies to “Pacific Island Hopping using R and iGraph”

  1. Pingback: Pacific Island Hopping using R and iGraph – Mubashir Qasim

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.