Practice Activity Week 6
Accessing the Practice Activity
Download the materials for the activity here: pa-6.zip
This .zip folder contains:
- the Quarto file
- the secret data used to make the map
Be sure to save the file inside your Week 6 folder of your STAT 431 (or 541) folder!
Data
secret_map.csv contains a secret code for each county in the U.S. In this Practice Activity, you will figure out the code and then create a map in ggplot showing what you found.
Simple Map of US Counties
Let’s start by just creating a map of all of the counties in the continuous US.
- Use the
counties()function from the tigris package to load a dataframe of US counties with their polygon files. Save this dataframe into an object namedcounty_map.
- Using
ggplot(), generate a map of the US counties. Have the fill color for each county be white and the outline color be a dark grey. Use cartesian coordinates. Hint:geom_sf()andcoord_sf()will be helpful here!
Remove all axes labels and gridlines. Hint: There is a built-in theme that can do this!
While cartesian coordinates are typical, they are actually somewhat visually misleading since the globe is a sphere. A recommended system for showing the US is the “Albers Equal Area Conic Projection”. Copy and paste your code from Questions 2 and 3 and change the coordinate system to this projection (or any conic projection). Hint: Check out the documentation for
coord_sf()for other projection systems.
Great! Now you should have a nice simple map of all of the US counties. Let’s add the information from the secret_map.csv data.
Color that Map
To color the map we will need to join the county_map dataframe with the secret_map dataframe. We will break this up into a few steps since there are some adjustments we need to make to each dataset before we join them together!
- There are a couple of typos in the
secret_codecolumn of thesecret_mapdata. Let’s fix these errors!
- Any “g” should be replaced with “F” (case matters!)
- The secret code should start with the hashtag character (#)
- Currently, the
county_mapdataframe doesn’t have the names of the states, only their state code. Use thefips_codesobject from the tigris package to add the state names into thecounty_mapdataframe. Hint 1: You may want to join a dataframe smaller than the originalfips_codes! Hint 2: Pay attention to the column names between the two datasets!
- Add the
secret_codeinformation to thecounty_mapdataframe. Hint: Pay attention to the column names between the two datasets!
- Remove the counties with missing values in the
secret_codecolumn.
- It turns out that the secret code gives you the colors that should be used in your final map. Save a vector called
map_colorswith the unique values of thesecret_codecolumn. Hint: There should be 3 colors.
- Now map it! Fill the counties as indicated in the secret map data with the appropriate color (as given by
secret_code) in your US map. Hint 1: Remember thesecret_codecolumn gives both the variable to fill by in the plot AND the color to use. Hint 2: You will want to use themap_colorsvector to use the right colors.
Canvas Submission:
Submit a screenshot of your final plot. What flag is shown?