Skip to content
R for the Rest of Us Logo

Mapping with R

34 lessons

U.S.-Specific Datasets (01_10)

This lesson is locked

Get access to all lessons in this course.

If the video is not playing correctly, you can watch it in a new window


Click on the transcript to go to that point in the video. Please note that transcripts are auto generated and may contain minor inaccuracies.

Your Turn

Open the 01_10 project. In the your-turn.R file do the following.

Produce the same choropleth as I did but obtain data from the 2015 American Community Survey (ACS) dataset.

  1. Use load_variables() to find the variables in the 2015 American Community Survey.

  2. Use get_acs() to extract these two variables.

  3. Use pivot_wider() to prepare the data for joining with the US states shapefiles from {tigris}.

  4. Join the Census and shapefiles datasets with left_join() and visualise with mapview()

Learn More

The tidycensus package uses the .Renviron file to store your API key so it’s readily available for future R sessions. For a brief overview of what this is I highly recommend this section of Jennifer Bryan and Jim Hester’s excellent (free to read) book. If you need to manually edit your .Renviron try using usethis::edit_r_environ().

It’s recommended to use load_variables(dataset = “sf1”) for the Decennial Census and load_variables(dataset = "acs5") for the ACS surveys. I’ve copied a section of Sara Altman and Bill Behrman’s course book that provides content for these choices:

For many tidycensus functions, you specify the different surveys in the following way:

  • “acs5”: 5-year ACS

  • “acs1”: 1-year ACS

  • “sf1”: Decennial census

sf stands for Summary File. Summary File 1 (“sf1”) corresponds to the short form described earlier, while Summary File 3 (“sf3”) corresponds to the long form. As explained earlier, the ACS took the place of the long form in 2001, so “sf3” is only available for censuses from 2000 or earlier.

It’s worthwhile pointing out that the get_decennial() and get_acs() functions can both download shapefiles themselves by using the geometry argument, eg:

get_decennial(geography = "state",
              variables = c("H004001", "H004002", "H004004"),
              year = 2010,
              geometry = TRUE) %>% 

This can save you time instead of separately downloading shapefiles with {tigris} and joining them together with left_join(). However, this will give you less control over which shapefiles are downloaded.

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

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

Ashley James

Ashley James

June 6, 2022

Hi Charlie,

Every time I try to install the tigris and tidycensus packages I get the following errors:

configure: error: gdal-config not found or not executable. ERROR: configuration failed for package ‘rgdal’

  • removing ‘/Library/Frameworks/R.framework/Versions/4.2/Resources/library/rgdal’

Do you know what might be going wrong?

Hannah Ridenour LaFrance

Hannah Ridenour LaFrance

July 19, 2023

My primary data visualization needs with maps are at the U.S. zip code level, which doesn't appear to be covered in this course (if it's coming in a later video, I'll be patient ha!). I found the zipcodeR package but any advice on adapting this lesson for that package?