Skip to content
R for the Rest of Us Logo

Mapping with R

Joining Data Frames with sf Objects

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(sf)
library(mapview)

city_councilors <-
  read_csv("data/portland_city_councilors.csv")

city_council_districts <-
  read_sf("https://raw.githubusercontent.com/rfortherestofus/mapping-with-r-v2/refs/heads/main/data/portland_city_council_districts.geojson")

left_join(
  city_councilors,
  city_council_districts,
  join_by(district)
)

left_join(
  city_councilors,
  city_council_districts,
  join_by(district)
) |>
  st_as_sf()

left_join(
  city_councilors,
  city_council_districts,
  join_by(district)
) |>
  st_as_sf() |>
  mapview()

right_join(
  city_councilors,
  city_council_districts,
  join_by(district)
) |>
  st_as_sf()

Your Turn

Import geospatial data on Portland traffic signals using the following code:

library(sf)

portland_traffic_signals <-
  read_sf(
    "https://raw.githubusercontent.com/rfortherestofus/mapping-with-r-v2/refs/heads/main/data/portland_traffic_signals.geojson"
  )

Import data on the power supplier of Portland traffic signals in a CSV file using the following code:

library(tidyverse)

portland_traffic_signals_power_supplier <-
  read_csv(
    "https://raw.githubusercontent.com/rfortherestofus/mapping-with-r-v2/refs/heads/main/data/portland_traffic_signals_power_supplier.csv"
  )

Join the portland_traffic_signals and portland_traffic_signals_power_supplier objects

Try making a map with different power suppliers in different colors to ensure that your join worked

Have any questions? Put them below and we will help you out!

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