rshapefiler-sftigris

How to intersect maps using Tigris (and keeping all maps boundaries)?


Sorry for this very basic question but I'm new using Tigris. I would like create a shapefile (and then plot it) of county boundaries + places boundaries for the state of Minnesota.

Here is my code to get the counties:

mn_counties = tigris::counties(cb = T) %>%
  filter(STUSPS == 'MN')

And here is my code to get the intersection between places and counties:

mn_places = tigris::places(cb = T) %>%
  filter(STUSPS == 'MN') %>%
  sf::st_intersection(mn_counties)

However, when I plot the intersection of these maps (counties and places), I just can see the polygons for the places map, but not for the counties.

tm_shape(mn_places) + tm_polygons()

enter image description here

Can anyone please tell me how to get an intersection of counties and places: 1. using tigris and, 2. that I'm able to see both places and county boundaries?

Many thanks in advance!!!


Solution

  • If I am understanding you correctly, you want places and counties in the same dataset. This is accomplished with dplyr::bind_rows():

    library(tigris)
    library(dplyr)
    library(tmap)
    
    mn_counties_and_places <- counties(state = "MN", cb = TRUE) %>%
      bind_rows(
        places(state = "MN", cb = TRUE)
      )
    
    tm_shape(mn_counties_and_places) + 
      tm_polygons()
    

    enter image description here