library(dplyr)
data("state")
# find pairs of US states whose centers are within
# 200 miles of each other
states <- tibble::tibble(state = state.name,
longitude = state.center$x,
latitude = state.center$y)
s1 <- rename(states, state1 = state)
s2 <- rename(states, state2 = state)
pairs <- s1 %>%
geo_inner_join(s2, max_dist = 200) %>%
filter(state1 != state2)
pairs
# plot them
if (requireNamespace("ggplot2", quietly = TRUE)) {
ggplot2::ggplot(pairs, ggplot2::aes(x = longitude.x, y = latitude.x,
xend = longitude.y, yend = latitude.y)) +
ggplot2::geom_segment(color = "red") +
ggplot2::theme_void()
}
# also get distances
s1 %>%
geo_inner_join(s2, max_dist = 200, distance_col = "distance")
Run the code above in your browser using DataLab