2016-09-27 139 views
0

我使用谷歌地圖圖書館在R中,如何根據兩點在Google Map上繪製線條/路徑?

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

我有下列R-代碼繪製兩點

mapPoints + 
geom_point(aes(x = lon, y = lat, col = "orange"), data = airportD) 

現在我要畫這些點之間的線,我怎麼能得到這樣的結果?

+0

[使用ggplot在地圖連接2點]的可能的複製(http://stackoverflow.com/questions/26572663/connecting-2-points -in-A-MAP-使用-ggplot) – SymbolixAU

回答

2

它不應該與向任何其他ggplot對象添加圖層不同。

airports <- read.csv("https://raw.githubusercontent.com/jpatokal/openflights/master/data/airports.dat", header = TRUE) 
names(airports) <- c("id", "name", "city", "country", "code", 
       "icao", "lat", "lon", "altitude", "timezone", "dst", "tz") 
airportD <- airports[airports$city %in% c("Beijing", "Bangkok", "Shanghai"), ] 

map <- get_map(location = 'Asia', zoom = 4) 
mapPoints <- ggmap(map) 

mapPoints + 
    geom_point(aes(x = lon, y = lat), col = "orange", data = airportD) 

enter image description here

mapPoints + 
    geom_line(aes(x = lon, y = lat), col = "orange", data = airportD) 

enter image description here

相關問題