Skip to content
R for the Rest of Us Logo

Mapping with R

Spatial Filters

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

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

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

st_join(
  improved_corners,
  city_council_districts
) |>
  filter(district == "District 1")

district_1_boundaries <-
  city_council_districts |>
  filter(district == "District 1")

improved_corners |>
  st_filter(district_1_boundaries) |>
  mapview::mapview()

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

district_1_corners <-
  improved_corners_polygons |>
  st_filter(district_1_boundaries)

mapview(district_1_boundaries) +
  mapview(district_1_corners)

district_1_corners <-
  improved_corners_polygons |>
  st_filter(district_1_boundaries, .predicate = st_intersects)

district_1_corners_within <-
  improved_corners_polygons |>
  st_filter(district_1_boundaries, .predicate = st_within)

mapview(district_1_boundaries) +
  mapview(district_1_corners_within)

Your Turn

Use the st_filter() function to only keep the traffic signals within the Southwest sextant

Learn More

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

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