Practice Activity Week 6

Accessing the Practice Activity

Download the materials for the activity here: pa-6.zip

This .zip folder contains:

  1. the Quarto file
  2. the secret data used to make the map
Important

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.

  1. 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 named county_map.
  1. 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() and coord_sf() will be helpful here!
  1. Remove all axes labels and gridlines. Hint: There is a built-in theme that can do this!

  2. 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!

  1. There are a couple of typos in the secret_code column of the secret_map data. Let’s fix these errors!
  1. Any “g” should be replaced with “F” (case matters!)
  2. The secret code should start with the hashtag character (#)
  1. Currently, the county_map dataframe doesn’t have the names of the states, only their state code. Use the fips_codes object from the tigris package to add the state names into the county_map dataframe. Hint 1: You may want to join a dataframe smaller than the original fips_codes! Hint 2: Pay attention to the column names between the two datasets!
  1. Add the secret_code information to the county_map dataframe. Hint: Pay attention to the column names between the two datasets!
  1. Remove the counties with missing values in the secret_code column.
  1. It turns out that the secret code gives you the colors that should be used in your final map. Save a vector called map_colors with the unique values of the secret_code column. Hint: There should be 3 colors.
  1. 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 the secret_code column gives both the variable to fill by in the plot AND the color to use. Hint 2: You will want to use the map_colors vector to use the right colors.

Canvas Submission:

Submit a screenshot of your final plot. What flag is shown?