在R中,如何依靠两点在Google Map上绘制线条/路径?

时间:2016-09-27 20:37:40

标签: r google-maps-api-3 line ggmap

我使用谷歌地图库

有以下代码
library(ggmap)
map <- get_map(location = 'Asia', zoom = 4)
mapPoints <- ggmap(map)

我不得不画两点

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

现在我想在这些点之间画一条线,我怎样才能获得这个结果?

1 个答案:

答案 0 :(得分:3)

与向任何其他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