R Question

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?

Answer

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)

enter image description here

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

enter image description here