Great circles are the shortest journeys between two points on a map, which can be easily computed and manipulated through the use of the excellent sf library, and visualised interactively with leaflet.
R
dataviz
GIS
Published
February 28, 2018
Surprisingly often I’m asked to help folks build maps which visualise the journeys between geographic locations. The shortest distance between two points on a map is a great circle, and you can see these on flight information screens — their exact curvature depends on the projection of the map.
I’m now an sf convert and try to do everything within that package. In my original version of this blogpost I introduced my painfully inefficient way to compute these great circles with sf and asked folks in the R4DS Slack group if they had any suggested improvements, which I’ve now baked in below.
Typically my datasets are collections of journeys, here’s an example (some journeys I took during the 3 years I worked for Wolfram Research):
library("tidyverse")library("sf")raw_journey_data <-tribble(~start.long, ~start.lat, ~end.long, ~end.lat, ~name,-0.118092, 51.509865, -119.698189, 34.420830, "London to Santa Barbara",31.496773, 30.026300, 24.753574, 59.436962, "New Cairo to Tallinn",126.633333, 45.75, 46.738586, 24.774265, "Harbin to Riyadh")raw_journey_data
# A tibble: 3 × 5
start.long start.lat end.long end.lat name
<dbl> <dbl> <dbl> <dbl> <chr>
1 -0.118 51.5 -120. 34.4 London to Santa Barbara
2 31.5 30.0 24.8 59.4 New Cairo to Tallinn
3 127. 45.8 46.7 24.8 Harbin to Riyadh
journeys_to_sf
Here’s my function for converting a data.frame containing journeys to an "sf" "data.frame" using tidyeval: