Simple feature geometry and tidycensus

1. Simple feature geometry and tidycensus

In this lesson, you'll learn how to work jointly with tidycensus and simple feature geometry. For the most common geographies, tidycensus wraps the tigris package to allow R users to request geometry along with Census data. For less common geographies, tigris and tidycensus can be used to join datasets in the way you learned in the previous chapter.

2. The geometry = TRUE argument

With the argument geometry equals TRUE, tidycensus users can request that simple feature geometry be returned along with their demographic data for states, counties, tracts, block groups, blocks, and zip code tabulation areas. This geometry is returned as a simple feature list-column via the tigris and sf packages as you learned about in the previous chapter; however, tidycensus does the joining for you in this case.

3. Getting simple feature geometry

The code shown here illustrates how the geometry argument works in tidycensus. As tract is a supported geography, setting geometry equals TRUE will use tigris to download the appropriate tract dataset for Cook County, Illinois in the Chicago area, load it as simple feature geometry, and join it to a dataset of median home values.

4. Simple feature geometry

The resulting dataset is a simple feature object that includes the columns you'd expect from get_acs() such as estimate and margin of error, but also includes information you'd expect from geographic data, such as a geometry column, a bounding box, and coordinate system information.

5. Plotting tidycensus geometry

This allows you to make a basic map of your data with a single line of R code. By indexing your dataset for the estimate column inside the base R plot() function, you can quickly visualize how median home values vary around Cook County.

6. Joining tigris and tidycensus data

Not all Census geographies are supported with geometry equals TRUE in tidycensus. For those geographies, however, you can use the equivalent function in tigris to get simple feature geometry and do the joining yourself. In this example, we use tigris to get school district geometry for the state of Idaho and join it to a dataset of median household income by school district obtained with tidycensus.

7. Joining tigris and tidycensus data

We then map the result with the plot() function, showing income variations by school district across the state.

8. Shifting Alaska and Hawaii geometry

Maps of the United States commonly shift and re-scale Alaska and Hawaii to the south of Arizona so that all 50 states plus Washington DC can be visible on a single map view. This option is available in tidycensus with the parameter shift_geo for national downloads of state and county data. If you are using this option, it is critical to communicate that Alaska and Hawaii are shifted and not to scale; this makes national visual comparisons much easier than if the states were in their correct locations.

9. Shifting Alaska and Hawaii geometry

Here, we see a map of median housing values by state with Alaska and Hawaii shifted and re-scaled for better cartographic display.

10. Let's practice!

Now it's your turn to work with simple feature geometry and tidycensus in R.