代码之家  ›  专栏  ›  技术社区  ›  user7075507

尝试生成航班出发和到达的地图

  •  2
  • user7075507  · 技术社区  · 8 年前

    下面的脚本几乎可以工作,但似乎在这一行中卡住了。

    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

    1 回复  |  直到 8 年前
        1
  •  1
  •   cuttlefish44    8 年前

    您的环境有点旧,我建议您更新R和包。我在R studio版本1.0.44(桌面)中使用R版本3.3.2(2016-10-31),并且 plyr 版本1.8.4, ggmap 版本2.6.1,以及 ggplot2 版本2.1.0。下面的示例代码在我的环境中工作。消息 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