library(ggplot2)
# visualize flights from
# Halifax -> Anchorage -> Berlin -> Halifax
cities <- data.frame(
lon = c(-63.58595, 116.41214, 13.50, -149.75),
lat = c(44.64862, 40.19063, 52.51, 61.20),
city = c("Halifax", "Beijing", "Berlin", "Anchorage"),
city_to = c("Anchorage", "Beijing", "Berlin", "Halifax")
)
cities$lon_end <- cities$lon[c(4, 3, 1, 2)]
cities$lat_end <- cities$lat[c(4, 3, 1, 2)]
p <- ggplot(cities, aes(lon, lat, xend = lon_end, yend = lat_end)) +
geom_spatial_point(crs = 4326)
# by default, geom_spatial_segment() connects points
# using the shortest distance along the face of the earth
# wrapping at the date line
p +
geom_spatial_segment(crs = 4326) +
coord_sf(crs = 3857)
# to let the projection handle the dateline,
# use `wrap_dateline = FALSE` (most useful for
# when using `arrow`)
p +
geom_spatial_segment(
wrap_dateline = FALSE,
arrow = grid::arrow(),
crs = 4326
) +
coord_sf(crs = 3995)
# to ignore the roundness of the earth, use
# `great_circle = FALSE`
p +
geom_spatial_segment(
great_circle = FALSE,
arrow = grid::arrow(),
crs = 4326
) +
coord_sf(crs = 3995)
Run the code above in your browser using DataLab