Start with the airports.
airports <- read.csv("airports.dat", header = FALSE)
colnames(airports) <- c("ID",
"name",
"city",
"country",
"IATA_FAA",
"ICAO",
"lat",
"lon",
"altitude",
"timezone",
"DST")
#head(airports)
Load in the routes for start/end.
routes <- read.csv("routes.dat", header=F)
colnames(routes) <- c("airline",
"airlineID",
"sourceAirport",
"sourceAirportID",
"destinationAirport",
"destinationAirportID",
"codeshare",
"stops",
"equipment")
#head(routes)
Basic map setup.
library(rworldmap)
## Loading required package: sp
## ### Welcome to rworldmap ###
## For a short introduction type : vignette('rworldmap')
newmap <- getMap(resolution = "low")
plot(newmap, xlim = c(-20, 59), ylim = c(35, 71), asp = 1)
points(airports$lon, airports$lat, col = "red", cex = .6)
Start setup for a nicer map.
library(plyr)
Count number of departing from and arriving to airports.
departures <- ddply(routes, .(sourceAirportID), "nrow")
names(departures)[2] <- "flights"
arrivals <- ddply(routes, .(destinationAirportID), "nrow")
names(arrivals)[2] <- "flights"
Add meta on departing/arriving flights to airports dataset.
airportD <- merge(airports, departures, by.x = "ID", by.y = "sourceAirportID")
airportA <- merge(airports, arrivals, by.x = "ID", by.y = "destinationAirportID")
Load in google maps for a much prettier map
library(ggmap)
## Loading required package: ggplot2
map <- get_map(location = 'Europe', zoom = 4)
## Map from URL : http://maps.googleapis.com/maps/api/staticmap?center=Europe&zoom=4&size=640x640&scale=2&maptype=terrain&language=en-EN&sensor=false
## Information from URL : http://maps.googleapis.com/maps/api/geocode/json?address=Europe&sensor=false
# geom_point adds the data points layer kind of like ggplot would
# aes = aesthetics (points) are generated
mapPoints <- ggmap(map) +
geom_point(aes(x = lon, y = lat, size = sqrt(flights)), data = airportD, alpha = .5)
mapPointsLegend <- mapPoints +
scale_size_area(breaks = sqrt(c(1, 5, 10, 50, 100, 500)),
labels = c(1, 5, 10, 50, 100, 500),
name = "departing routes")
mapPointsLegend
## Warning: Removed 2727 rows containing missing values (geom_point).
Faceting (couple of panels)
# create the data set containing both departures and arrivals
airportD$type <- "departures"
airportA$type <- "arrivals"
airportDA <- rbind(airportD, airportA)
# map the data
mapPointsDA <- ggmap(map) +
geom_point(aes(x = lon, y = lat, size = sqrt(flights)), data = airportDA, alpha = .5)
# adjust the legend
mapPointsLegendDA <- mapPointsDA +
scale_size_area(breaks = sqrt(c(1, 5, 10, 50, 100, 500)),
labels = c(1, 5, 10, 50, 100, 500),
name = "routes")
# panels according to type (departure/arrival)
mapPointsFacetsDA <- mapPointsLegendDA + facet_grid(. ~ type)
mapPointsFacetsDA
## Warning: Removed 2731 rows containing missing values (geom_point).
## Warning: Removed 2727 rows containing missing values (geom_point).