Skip to content

Instantly share code, notes, and snippets.

@seekr
Last active Feb 16, 2017
Embed
What would you like to do?

source: http://aejaffe.com/summerR_2015/modules/mapping_module.html#1

http://flowingdata.com/2011/05/11/how-to-map-connections-with-great-circles/

install

rworldmap, ggmap

setup:

If necessary

install.packages(c("rworldmap", "rworldxtra", "RColorBrewer", "maptools", "classInt"))

#Load packages

library('rworldmap') library('rworldxtra') library('RColorBrewer') library('maptools') library('classInt') library(ggplot2) library(maps) library(geosphere)

flights <- read.csv("flights.csv", header=TRUE, as.is=TRUE) airports <- read.csv("airports.csv", header=TRUE, as.is = TRUE)

#load map: worldmap <- getMap(resolution = "coarse") dim(worldmap)

#plot South America: par(mar=c(0,0,0,0)) samerica <- worldmap[which(grepl("South America", worldmap$GEO3)),] plot(samerica, col="#FFFFFF", bg="#F2F2F2", border="#00000011", lwd=1)

#plot flights:

fsub <- flights[flights$airport1 == "EPA",] for (j in 1:length(fsub$airport1)) { air1 <- airports[airports$iata == fsub[j,]$airport1,] air2 <- airports[airports$iata == fsub[j,]$airport2,]

points(air1[1,]$long, air1[1,]$lat, pch=16, cex=1, col="cyan")
points(air2[1,]$long, air2[1,]$lat, pch=16, cex=1, col="cyan")
text(air2[1,]$long+sample(-1:+1, 1), air2[1,]$lat+sample(-1:+1, 1), air2[1,]$city, cex = .3)

} for (j in 1:length(fsub$airport1)) { air1 <- airports[airports$iata == fsub[j,]$airport1,] air2 <- airports[airports$iata == fsub[j,]$airport2,]

inter <- gcIntermediate(c(air1[1,]$long, air1[1,]$lat), c(air2[1,]$long, air2[1,]$lat), n=100, addStartEnd=TRUE)


lines(inter, col="#ffde3799", lwd=1.2)

} points(air1[1,]$long, air1[1,]$lat, pch=16, cex=2, col="cyan")

text(air1[1,]$long+10, air1[1,]$lat+0, "FLY BONDI", cex = 1, adj = c(0,0), col="#ffde37") text(air1[1,]$long+10, air1[1,]$lat-3, "Rutas aprobadas por JATA/ANAC",adj = c(0,0), cex = .8) text(air1[1,]$long+10, air1[1,]$lat-5, "Base de operaciones: El Palomar",adj = c(0,0), cex = .8)

// alas del sur

flights <- read.csv("flights.csv", header=TRUE, as.is=TRUE) airports <- read.csv("airports.csv", header=TRUE, as.is = TRUE)

#load map: worldmap <- getMap(resolution = "coarse") dim(worldmap)

#plot South America: par(mar=c(0,0,0,0)) plot(worldmap, col="#FFFFFF", bg="#F2F2F2", border="#00000011", lwd=1)

#plot flights:

fsub <- flights[flights$airline == "ALAS DEL SUR",] for (j in 1:length(fsub$airport1)) { air1 <- airports[airports$iata == fsub[j,]$airport1,] air2 <- airports[airports$iata == fsub[j,]$airport2,]

points(air1[1,]$long, air1[1,]$lat, pch=16, cex=0.5, col="cyan")
points(air2[1,]$long, air2[1,]$lat, pch=16, cex=0.5, col="cyan")

} for (j in 1:length(fsub$airport1)) { air1 <- airports[airports$iata == fsub[j,]$airport1,] air2 <- airports[airports$iata == fsub[j,]$airport2,]

inter <- gcIntermediate(c(air1[1,]$long, air1[1,]$lat), c(air2[1,]$long, air2[1,]$lat), n=50, addStartEnd=TRUE)


lines(inter, col="#9e1f8299", lwd=1.2)

} points(air1[1,]$long, air1[1,]$lat, pch=16, cex=0.5, col="cyan")

text(air1[1,]$long+10, air1[1,]$lat+0, "Alas del Sur", cex = 1, adj = c(0,0), col="#9e1f82") text(air1[1,]$long+10, air1[1,]$lat-5, "Rutas aprobadas por JATA/ANAC",adj = c(0,0), cex = .8) text(air1[1,]$long+10, air1[1,]$lat-10, "Base de operaciones: El Palomar",adj = c(0,0), cex = .8)

// Alternative

library("ggmap") library(maptools) library(maps)

visited <- c("JFK", "El Palomar, Buenos Aires", "JFK", "Miami, FL") ll.visited <- geocode(visited) visit.x <- ll.visited$lon visit.y <- ll.visited$lat

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment