queens_living queens_living - 1 month ago 11
R Question

Trying to generate maps for flight departures and arrivals

The script below almost works, but seems to get stuck on this line.

mapPoints <- ggmap(map) +
geom_point(aes(x = lon, y = lat, size = sqrt(flights)), data = airportD, alpha = .5)


Here is the entire thing.

airports <- read.csv("https://raw.githubusercontent.com/jpatokal/openflights/master/data/airports.dat", header = FALSE)
colnames(airports) <- c("ID", "name", "city", "country", "IATA_FAA", "ICAO", "lat", "lon", "altitude", "timezone", "DST")
# head(airports)

library(rworldmap)
newmap <- getMap(resolution = "low")
par(mar = rep(2, 4))
plot(newmap, xlim = c(-20, 59), ylim = c(35, 71), asp = 1)
points(airports$lon, airports$lat, col = "red", cex = .6)


routes <- read.csv("https://raw.githubusercontent.com/jpatokal/openflights/master/data/routes.dat", header=F)
colnames(routes) <- c("airline", "airlineID", "sourceAirport", "sourceAirportID", "destinationAirport", "destinationAirportID", "codeshare", "stops", "equipment")
# head(routes)


library(plyr)
departures <- ddply(routes, .(sourceAirportID), "nrow")
names(departures)[2] <- "flights"
arrivals <- ddply(routes, .(destinationAirportID), "nrow")
names(arrivals)[2] <- "flights"


airportD <- merge(airports, departures, by.x = "ID", by.y = "sourceAirportID")
airportA <- merge(airports, arrivals, by.x = "ID", by.y = "destinationAirportID")

library(ggmap)
map <- get_map(location = 'Europe', zoom = 4)

mapPoints <- ggmap(map) + geom_point(aes(x = lon, y = lat, size = sqrt(flights)), data = airportD, alpha = .5)

mapPointsLegend <- mapPoints +
scale_area(breaks = sqrt(c(1, 5, 10, 50, 100, 500)), labels = c(1, 5, 10, 50, 100, 500), name = "departing routes")

mapPointsLegend



# create the data set containing both departures and arrivals
airportD$type <- "departures"
airportA$type <- "arrivals"
airportDA <- rbind(airportD, airportA)

# map the data
# map + data points
mapPointsDA <- ggmap(map) + geom_point(aes(x = lon, y = lat, size = sqrt(flights)), data = airportDA, alpha = .5)
# adjust the legend
mapPointsLegendDA <- mapPointsDA + scale_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)
# plot the map
mapPointsFacetsDA


The script comes from the link directly below. Notice: the data sets are in a different place than what is suggested in the link; I already corrected for this in the script.

http://www.milanor.net/blog/maps-in-r-plotting-data-points-on-a-map/

I googled for a solution and I'm stumped. Any ideas?

Here is the message that I'm getting.

enter image description here

Answer

Your environment is a little old and I recommend you to update R and packages. I use R version 3.3.2 (2016-10-31) in R studio Version 1.0.44 (desktop) and plyr version 1.8.4, ggmap version 2.6.1, and ggplot2 version 2.1.0. Below example code works in my env. The messages by get_map() are just report and last warning means there are 5458 data outside of Europe.

library(ggplot2); library(ggmap); library(plyr)

airports <- read.csv("https://raw.githubusercontent.com/jpatokal/openflights/master/data/airports.dat", header = FALSE)
routes <- read.csv("https://raw.githubusercontent.com/jpatokal/openflights/master/data/routes.dat", header=F)

colnames(airports) <- c("ID", "name", "city", "country", "IATA_FAA", "ICAO", "lat", "lon", "altitude", "timezone", "DST")
colnames(routes) <- c("airline", "airlineID", "sourceAirport", "sourceAirportID", "destinationAirport", "destinationAirportID", "codeshare", "stops", "equipment")


departures <- ddply(routes, .(sourceAirportID), "nrow")
names(departures)[2] <- "flights"
arrivals <- ddply(routes, .(destinationAirportID), "nrow")
names(arrivals)[2] <- "flights"

airportD <- merge(airports, departures, by.x = "ID", by.y = "sourceAirportID")
airportA <- merge(airports, arrivals, by.x = "ID", by.y = "destinationAirportID")

airportD$type <- "departures"
airportA$type <- "arrivals"
airportDA <- rbind(airportD, airportA)

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

mapPointsDA <- ggmap(map) + geom_point(aes(x = lon, y = lat, size = sqrt(flights)), data = airportDA, alpha = .5) 
mapPointsLegendDA <- mapPointsDA + scale_size_area(breaks = sqrt(c(1, 5, 10, 50, 100, 500)), labels = c(1, 5, 10, 50, 100, 500), name = "routes")
mapPointsFacetsDA <- mapPointsLegendDA + facet_grid(. ~ type)
mapPointsFacetsDA
# Warning message: Removed 5458 rows containing missing values (geom_point). 

enter image description here