2016-11-05 2 views
2

Скрипт внизу почти работает, но, похоже, застрял на этой линии.Попытка создания карт для вылета и прибытия рейса

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

Вот и все.

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 

Сценарий происходит по прямой ссылке. Обратите внимание: наборы данных находятся в другом месте, чем указано в ссылке; Я уже исправил это в скрипте.

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

Я гугл для решения, и я в тупике. Есть идеи?

Вот сообщение, которое я получаю.

enter image description here

+0

'scale_area()' изменила свое название на 'scale_size_area()', (это связано с вашей проблемой?) – cuttlefish44

+0

Спасибо. Это хорошо знать. Это следующая строка кода. Во-первых, мне нужно получить строку кода прямо перед тем, как вы говорили. –

+0

Странно. Я запускаю RTVS version.string R версии 3.2.3 (2015-12-10). Также попробовал тот же скрипт в RStudio version.string R версии 3.2.2 (2015-08-14). –

ответ

1

Ваше окружение немного старый, и я рекомендую вам обновить R и пакетов. Я использую R версию 3.3.2 (2016-10-31) в R studio версии 1.0.44 (рабочий стол) и plyr версии 1.8.4, ggmap версии 2.6.1 и ggplot2 версии 2.1.0. Ниже пример кода работает в моем env. Сообщения от get_map() - это всего лишь отчет, и последнее предупреждение означает, что за пределами Европы имеется 5458 данных.

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

+0

Я обновил свою версию и несколько пакетов, и теперь все работает отлично. Спасибо за помощь! Спасибо, что посмотрели все это до конца! –

+0

cuttlefish44 вы можете отправить мне электронное письмо? вопрос, который, я думаю, вы можете ответить. rms7272 на gmail.com Просто замените это на @ –

Смежные вопросы