Skip to content
R for the Rest of Us Logo

Mapping with R

Flow Maps

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:

  1. Use the st_centroid() function to turn the asylum_seekers object into geospatial data with single points (save it as asylum_seekers_centroid).

  2. Use the st_coordinates() function to turn asylum_seekers_centroid into an object with x and y variables for the coordinates (save it as asylum_seekers_centroid_x_y).

  3. Create a united_states_centroid object by filtering the countries object to just include the United States and then use st_centroid() to find its center.

  4. Use the st_coordinates() function to turn united_states_centroid into an object with x and y variables for the coordinates (save it as united_states_centroid_x_y).

  5. Use bind_cols() to bring together the asylum_seekers_centroid_x_y and united_states_centroid_x_y objects into a single object (save it as asylum_seekers_flow).

  6. Make your map, using geom_sf to add all countries as a background layer before using geom_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!

You need to be signed-in to comment on this post. Login.