A flight map with ggplot2:
We need:
- A cartesian space. x \(\mapsto\) lat
, y \(\mapsto\) lon
- Line segments to represent routes
- Polygons to represent to country borders
We need the following data frame:
# create routes
library(dplyr)
routes <- tribble(
~origin_airport, ~dest_airport, ~origin_airport_lat,~origin_airport_lon, ~dest_airport_lat, ~dest_airport_lon,
'IST', 'TXL', 40.9769, 28.8146, 52.5597, 13.28770,
'TXL', 'TSF', 52.5597, 13.2877, 45.6484, 12.19440,
'TXL', 'ZRH', 52.5597, 13.2877, 47.4647, 8.54917,
'TXL', 'CPH', 52.5597, 13.2877, 55.6179, 12.65600,
'TXL', 'DLM', 52.5597, 13.2877, 36.7131, 28.79250,
'TXL', 'ADA', 52.5597, 13.2877, 36.9822, 35.28040,
'TXL', 'BOJ', 52.5597, 13.2877, 42.5696, 27.51520,
'TXL', 'TFS', 52.5597, 13.2877, 28.0445, -16.57250,
'TXL', 'BOS', 52.5597, 13.2877, 42.3643, -71.00520,
'TXL', 'FAE', 52.5597, 13.2877, 62.0636, -7.27722,
'TXL', 'CUZ', 52.5597, 13.2877, 13.5357, -71.93880,
'TXL', 'HKG', 52.5597, 13.2877, 22.3089, 113.91500)
Draw a basic plot:
# cartesian
library(ggplot2)
ggplot(data = routes) +
geom_segment(aes(x = origin_airport_lon, y = origin_airport_lat,
xend = dest_airport_lon, yend = dest_airport_lat)) +
geom_text(aes(x = dest_airport_lon, y = dest_airport_lat, label = dest_airport)) +
coord_equal() +
xlab("Lon") +
ylab("Lat")
It does not look like a flight map.
We just draw line segments between origin_airport
and destination_airport
. Next step is adding polygons. We can get the polygon data with ggplot2::map_data
function.
We can also change the theme by adding another layer: theme_void
library(ggplot2)
world <- map_data("world") %>% tbl_df()
ggplot(data = routes) +
geom_polygon(data = world, aes(x = long, y = lat, group=group),
size = 0.2, color="white", alpha = 0.9, fill="lightgray") +
geom_segment(aes(x = origin_airport_lon, y = origin_airport_lat,
xend = dest_airport_lon, yend = dest_airport_lat),
color = "brown") +
geom_text(aes(x = dest_airport_lon, y = dest_airport_lat, label = dest_airport), size = 3) +
theme_void()
We will have a more fancy plot by adding a coord_map
layer.
library(maps)
ggplot(data = routes) +
geom_polygon(data = world, aes(x = long, y = lat, group=group),
size = 0.2, color="white", alpha = 0.9, fill="lightgray") +
geom_segment(aes(x = origin_airport_lon, y = origin_airport_lat,
xend = dest_airport_lon, yend = dest_airport_lat),
color = "brown") +
coord_map("azequalarea", orientation = c(0, -10, 0)) +
theme_void()