简体   繁体   中英

In R, how to draw a line/path on Google Map relying two points?

I have the following r code using the Google Map library

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

I had to plot two points

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

Now I want to draw a line between these points, how can I obtain this result?

It shouldn't be any different than adding a layer to any other ggplot object.

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)

在此处输入图片说明

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

在此处输入图片说明

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM