Created
March 13, 2016 13:13
-
-
Save infinex/d1675f7d9a38f0ba0707 to your computer and use it in GitHub Desktop.
Visualisation of airport connectivities in R using ggmap/ggplot/igraph
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
library(ggmap) | |
airports <- read.csv(“https://raw.githubusercontent.com/jpatokal/openflights/master/data/airports.dat", header = FALSE) | |
names(airports) <- c(“id”, “name”, “city”, “country”, “code”, | |
“icao”, “lat”, “lon”, “altitude”, “timezone”, | |
“dst”,”tz”) | |
location <- c( 115,0 ) | |
location | |
map <- get_map(location, zoom=3,maptype=’toner-lite’) | |
p <- ggmap(map) | |
p | |
routes = read.csv(“https://raw.githubusercontent.com/jpatokal/openflights/master/data/routes.dat",header=FALSE) | |
names(routes) <- c(“airline”, “airlineID”, “sourceAirport”, “sourceAirportID”, | |
“destinationAirport”, “destinationAirportID”, “codeshare”, | |
“stops”, “equipment”) | |
routes = routes[,c(4,6,1:3,5,7:9)] | |
w = union(which(routes$sourceAirportID==”\\N”),which(routes$destinationAirportID==”\\N”)) | |
routes = routes[-w,] | |
flights = graph.data.frame(routes,vertices=airports) | |
E(flights)$weight = 1 | |
flights = simplify(flights) | |
yrange=ggplot_build(p)$panel$ranges[[1]]$y.range | |
xrange=ggplot_build(p)$panel$ranges[[1]]$x.range | |
w = which(V(flights)$lon>xrange[1] & V(flights)$lon<xrange[2] | |
& V(flights)$lat>yrange[1] & V(flights)$lat<yrange[2]) | |
G=induced_subgraph(flights,w) | |
G$layout = cbind(V(G)$lon,V(G)$lat) | |
E(G)$color = rgb(0,0,0,E(G)$weight/30) | |
plot_vector<- as.data.frame(cbind(V(G)$lon,V(G)$lat)) | |
edgelist <- get.edgelist(G) | |
edgelist[,1]<-as.numeric(match(edgelist[,1],V(G)$name)) | |
edgelist[,2]<-as.numeric(match(edgelist[,2],V(G)$name)) | |
edges <- data.frame(plot_vector[edgelist[,1],], plot_vector[edgelist[,2],],E(G)$color) | |
colnames(edges) <- c(“X1”,”Y1",”X2",”Y2",”Color”) | |
p + geom_segment(aes(x=X1, y=Y1, xend = X2, yend = Y2), data=edges, size = 0.5, colour=edges$Color) + geom_point(aes(V1, V2), data=plot_vector)+ geom_point(aes(x=V1,y=V2),plot_vector,size=0.01,alpha=0.5,color=”yellow”) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
https://medium.com/@infinex/visualisation-of-airport-connectivities-in-r-using-ggmap-ggplot-igraph-d26af3267ca1