Flow Maps
This lesson is called Flow Maps, part of the Mapping with R course. This lesson is called Flow Maps, part of the Mapping with R course.
Transcript
Click on the transcript to go to that point in the video. Please note that transcripts are auto generated and may contain minor inaccuracies.
View code shown in video
library(tidyverse)
library(tidycensus)
library(tigris)
library(janitor)
library(sf)
inflow <-
get_flows(
geography = "county",
state = "OR",
county = "Deschutes",
geometry = TRUE
) |>
clean_names() |>
filter(variable == "MOVEDIN") |>
slice_max(order_by = estimate, n = 10) |>
drop_na(geoid2) # To get rid of Asia
inflow_origins <-
inflow |>
st_drop_geometry() |>
select(geoid2, full2_name, estimate, centroid2) |>
st_set_geometry("centroid2")
inflow_origins |>
mapview::mapview()
inflow_origins_centroids <-
inflow_origins |>
st_coordinates("centroid2") |>
as_tibble() |>
set_names("origin_x", "origin_y")
inflow_desination <-
inflow |>
select(geoid1, full1_name, estimate)
inflow_desination_centroid <-
inflow_desination |>
st_coordinates("centroid1") |>
as_tibble() |>
set_names("desination_x", "desination_y")
inflow_x_y <-
inflow_origins_centroids |>
bind_cols(inflow_desination_centroid)
origin_states <-
inflow |>
st_drop_geometry() |>
select(full2_name) |>
separate_wider_delim(
full2_name,
delim = ", ",
names = c("county", "state")
) |>
distinct(state) |>
pull(state)
origin_states_sf <-
states() |>
clean_names() |>
select(name) |>
filter(name %in% origin_states)
ggplot() +
geom_sf(data = origin_states_sf) +
geom_curve(
data = inflow_x_y,
aes(
x = origin_x,
xend = desination_x,
y = origin_y,
yend = desination_y
)
) +
geom_sf(data = inflow_origins) +
geom_sf(data = inflow_desination,
color = "orange") +
theme_void()
Your Turn
Your job is to create a flow map showing the top 5 countries of origin for asylum seekers in the United States in 2023.
Download data on the top 5 countries of origin for asylum seekers:
library(sf)
asylum_seekers <-
read_sf(
"https://raw.githubusercontent.com/rfortherestofus/mapping-with-r-v2/refs/heads/main/data/asylum_seekers.geojson"
)
Import geospatial data on all countries:
countries <-
read_sf(
"https://raw.githubusercontent.com/rfortherestofus/mapping-with-r-v2/refs/heads/main/data/countries.geojson"
)
Adapt the sample code from the lesson to make a map that shows the flows of asylum seekers from the top 5 countries to the United States. You’ll need to:
Use the
st_centroid()
function to turn theasylum_seekers
object into geospatial data with single points (save it asasylum_seekers_centroid
).Use the
st_coordinates()
function to turnasylum_seekers_centroid
into an object with x and y variables for the coordinates (save it asasylum_seekers_centroid_x_y
).Create a
united_states_centroid
object by filtering thecountries
object to just include the United States and then usest_centroid()
to find its center.Use the
st_coordinates()
function to turnunited_states_centroid
into an object with x and y variables for the coordinates (save it asunited_states_centroid_x_y
).Use
bind_cols()
to bring together theasylum_seekers_centroid_x_y
andunited_states_centroid_x_y
objects into a single object (save it asasylum_seekers_flow
).Make your map, using
geom_sf
to add all countries as a background layer before usinggeom_curve()
to show the flow of asylum seekers from the top 5 countries.
Learn More
I found map showing the evolution of the word kitchen on Bluesky. The route map came from Reddit.
In this lesson, I showed you how to use the st_coordinates()
function to get coordinates from an sf object and the st_centroid()
function to get the center of a polygon.
I also showed using st_set_geometry()
to use a different geometry column. This function, as opposed to st_as_sf()
, is useful when there are multiple potential geometry columns while st_as_sf()
converts non-geospatial data into an sf object.
Have any questions? Put them below and we will help you out!
Course Content
23 Lessons
You need to be signed-in to comment on this post. Login.