Skip to content
R for the Rest of Us Logo

U.S.-Specific Datasets (01_10)

This lesson is locked

Get access to all lessons in this course.

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.

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) rstats.wtf 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) %>% 
  mapview()

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?

Ashley James

Ashley James

June 6, 2022

Actually, I figured it out!

Charlie Hadley

Charlie Hadley

June 6, 2022

Glad you figured it out!

Charlie Hadley

Charlie Hadley

June 6, 2022

Actually, Ashley could you share with me how you were able to fix this issue? Thanks, Charlie

Ashley James

Ashley James

June 6, 2022

Yes! First, I instilled "rgdal" before I tried to install "tirgis" and "tidycensus". I also replied "no" when it asked "Do you want to install from sources the package which needs compilation? y/n:" When I was previously replying yes.

Charlie Hadley

Charlie Hadley

June 7, 2022

Awesome - thanks for sharing. Usually CRAN does do a good job of ensuring that package dependencies are correctly integrated into packages but sometimes things like {rgdal} still slip through somehow. However, {rgdal} is being retired from 2023 so in the future this might be less of a big deal.

The second step of changing your response to "install with compilation" is also great problem solving. I'd recommend this article about what installing packages needing compilation means, if you're interested. But it is absolutely not required reading for using R.

Hannah Ridenour LaFrance

Hannah Ridenour LaFrance

July 18, 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?

David Keyes

David Keyes

July 18, 2023

So this is actually a bit more complicated than you were probably anticipating. The tl;dr is that zip codes aren't technically polygons and so you can't actually map with them (see technical explanation here). That said, you can get data on what are known as ZCTAs (discussed in the linked article) using the tigris package. Here's how. Hope this helps!