Create Air Travel Route Maps in ggplot: A Visual Travel Diary

Create Air Travel Route Maps: Emirates Route Map

Emirates flight routes.

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 air travel route maps. 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.

Create Air Travel Route Maps using ggplot2

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 list of all routes (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 ggmap package. In some instances, I had to add the country name to avoid confusion between places.

# Read flight list
flights <- read.csv("flights.csv", stringsAsFactors = FALSE)

# Lookup coordinates
library(ggmap)
airports <- unique(c(flights$From, flights$To))
coords <- geocode(airports)
airports <- data.frame(airport=airports, coords)

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 curve geom. The borders function of ggplot2 creates the map data. The ggrepel package helps to prevent overplotting of text.

# Add coordinates to flight list
flights <- merge(flights, airports, by.x="To", by.y="airport")
flights <- merge(flights, airports, by.x="From", by.y="airport")

# Plot flight routes
library(ggplot2)
library(ggrepel)
worldmap <- borders("world", colour="#efede1", fill="#efede1") # 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 = "#b29e7d", size = 1, curvature = .2) + 
 geom_point(data=airports, aes(x = lon, y = lat), col = "#970027") + 
 geom_text_repel(data=airports, aes(x = lon, y = lat, label = airport), col = "black", size = 2, segment.color = NA) + 
 theme(panel.background = element_rect(fill="white"), 
 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()
 )

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 flat-earthers will see this map because they might use it as prove that the world is flat.

Alternative Visualisation

Another way of visualising this type of data is using a network diagram provided by the igraph package. This visualisation shows the logic between the locations and not their spatial locations.

# Network visualisation
library(igraph)
edgelist <- as.matrix(flights[c("From", "To")])
g <- graph_from_edgelist(edgelist, directed = TRUE)
g <- simplify(g)
par(mar=rep(0,4))
plot.igraph(g, 
 edge.arrow.size=0,
 edge.color="black",
 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
)

12 thoughts on “Create Air Travel Route Maps in ggplot: A Visual Travel Diary

  1. Pingback: Create Air Travel Route Maps in ggplot: A Visual Travel Diary – Mubashir Qasim

    • Hi, I have used some data from a Wikipedia page on airline travel in Australia and visualised it by varying the line thickness. This map visualises domestic passenger numbers from Sydney.

      This is the code:

      # Passegener volumes
      library(rvest)
      url <- "https://en.wikipedia.org/wiki/List_of_the_busiest_air_routes_in_Australia_by_passenger_traffic"
      sydneytraffic <- url %>%
          read_html() %>%
          html_nodes(xpath = '//*[@id="mw-content-text"]/table[1]') %>%
          html_table()
      sydneytraffic <- sydneytraffic[[1]]
      sydneytraffic <- data.frame(From = "Sydney", To = sydneytraffic$`To Country / City`[-11], Passengers = sydneytraffic$`Passengers
      Jun 2012`[-11])
      
      # Lookup coordinates
      library(ggmap)
      airports <- unique(c("Sydney", as.character(sydneytraffic$To)))
      coords <- geocode(airports)
      airports <- data.frame(airport=airports, coords)
      
      # Add coordinates to flight list
      sydneytraffic <- merge(sydneytraffic, airports, by.x="To", by.y="airport")
      sydneytraffic <- merge(sydneytraffic, airports, by.x="From", by.y="airport")
      sydneytraffic$Passengers <- as.numeric(gsub(",", "", as.character(sydneytraffic$Passengers)))
      
      sydneytraffic$linew <- sydneytraffic$Passengers/min(sydneytraffic$Passengers)
      
      # Plot flight routes
      library(ggplot2)
      library(ggrepel)
      worldmap <- borders("world", colour="#efede1", fill="#efede1") # create a layer of borders
      ggplot() + worldmap + 
          geom_curve(data=sydneytraffic, aes(x = lon.x, y = lat.x, xend = lon.y, yend = lat.y), col = "#b29e7d", 
                     size = sydneytraffic$linew/2, curvature = .2) + 
          geom_point(data=airports, aes(x = lon, y = lat), col = "#970027") + 
          geom_text_repel(data=airports, aes(x = lon, y = lat, label = airport), col = "black", size = 2, segment.color = NA) + 
          xlim(110, 155) + ylim(-45, -10) + coord_fixed() + 
          theme(panel.background = element_rect(fill="white"), 
                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()
          )
      ggsave("passengers.png")
      
  2. I’ve been looking for code to do a route map – thank you. I’m having a problem getting the code to work in my environment. I get the following error: Error in eval(expr, envir, enclos) : object ‘lon.x’ not found. Any thoughts?

      • I was able to resolve the problem by moving the geom_curve call after the geom_text_repel call. Everything works great now. Thank you for sharing.

        I’m finding it helpfull to change the line width to 0.2 – to my eye, it looks cleaner.

          • Here is the ggplot2 call that I am using:

            ggplot() + worldmap + 
            #  geom_curve(data=flights, aes(x = lon.x, y = lat.x, xend = lon.y, yend = lat.y), col = "#b29e7d", size = 1, curvature = .2) + 
              geom_point(data=airports, aes(x = lon, y = lat), col = "#970027") + 
              geom_text_repel(data=airports, aes(x = lon, y = lat, label = airport), col = "black", size = 2, segment.color = NA) + 
              geom_curve(data=flights, aes(x = lon.x, y = lat.x, xend = lon.y, yend = lat.y), col = "#b29e7d", size = .2, curvature = .2) +
              theme(panel.background = element_rect(fill="white"), 
                    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()
              )
            

            It is almost identical to your code except:

            a) the geom_curve call is reordered
            b) the size in the geom_curve call has been changed to 0.2

            Thanks again for posting this – I love it.

  3. Pingback: Linkdump #32 | WZB Data Science Blog

  4. Pingback: This Week in Data Science (March 14, 2017) – Be Analytics

Leave a Reply