Get access to all lessons in this course.
- Welcome to Mapping with R (01_01)
-
Geospatial Data
- Making Maps is Complex (01_02)
- mapview for Quick Maps (01_03)
- sf for Simple Features (01_04)
- Turning Data Frames into sf Objects (01_05)
- Importing Shapefiles (01_06)
- Joining Geospatial Datasets (01_07)
- Disambiguating Country Names (01_08)
- Converting Addresses to Coordinates (01_09)
- U.S.-Specific Datasets (01_10)
- Advice on Finding International Datasets (01_11)
- CRS and Projections: Geographic and Projected CRS (01_12)
- CRS and Projections: How to Choose a CRS (01_13)
- Introducing Raster GIS with raster and stars (01_14)
- Basics of Using the raster Package (01_15)
-
Static Maps
- ggplot2 Essentials (02_01)
- Starting a Map in ggplot2 (02_02)
- Labelling ggplot2 Maps (02_03)
- Compare Locations/Events with Geobubble Charts (02_04)
- Highlight a Region in a Country with ggplot2 (02_05)
- Make a Choropleth Map of Discrete Variables with ggplot2 (02_06)
- Make a Choropleth Map of Continuous Variables with ggplot2 (02_07)
- Faceting Choropleth Maps with ggplot2 (02_08)
- Visualize Raster Data with ggplot2 (02_09)
- Adding Scale Bars and North Arrows with ggplot2 (02_10)
-
Interactive Maps
- What is leaflet? (03_01)
- Starting a Map in leaflet (03_02)
- Necessary HTML for Labelling leaflet Maps (03_03)
- Highlight a Region in a Country with leaflet (03_04)
- Compare Locations/Events with Geobubble Charts in leaflet (03_05)
- Make a Choropleth Map of Discrete Variables with leaflet (03_06)
- Make a Choropleth Map of Continuous Variables with leaflet (03_07)
- Visualize Raster Data with leaflet (03_08)
-
Wrapping Up
- You Did It!
Mapping with R
U.S.-Specific Datasets (01_10)
This lesson is locked
This lesson is called U.S.-Specific Datasets (01_10), part of the Mapping with R course. This lesson is called U.S.-Specific Datasets (01_10), part of the Mapping with R 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.
Use
load_variables()
to find the variables in the 2015 American Community Survey.Use
get_acs()
to extract these two variables.Use
pivot_wider()
to prepare the data for joining with the US states shapefiles from{tigris}
.Join the Census and shapefiles datasets with
left_join()
and visualise withmapview()
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.
You need to be signed-in to comment on this post. Login.
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’
Do you know what might be going wrong?
Ashley James
June 6, 2022
Actually, I figured it out!
Charlie Hadley
June 6, 2022
Glad you figured it out!
Charlie Hadley
June 6, 2022
Actually, Ashley could you share with me how you were able to fix this issue? Thanks, Charlie
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
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
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
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!