PA 10: Map it!

Plotting Geographic Data

Download starter .qmd template

Download secret_map.csv data

1 Data

secret_map.csv contains a secret code for each county in the U.S. In this PA, you will figure out the code and then create a map in ggplot showing what you found.

# read in and save secret_map.csv

2 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 map_data() function to load a dataframe of US counties with their latitudes and longitudes. Save this data as county_map.
# code for Q1
  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.
Tip

You will want to reference 10.3 from our “textbook” and the resources linked there to complete this task!

# code for Q2-3
  1. If you haven’t already, remove any axes labels and normal plot grid lines. You can remove the default grey ggplot background as well if you prefer!

  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 Q2-3 and change the coordinate system to this projection (or any conic projection).

Hint

Check out the documentation for coord_map() and the help file for map_data().

# code for Q4

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.

3 Color that Map

  1. First of all, word is just in that there are a couple of typos in the secret_code column:
  1. Any “g” should be replaced with “F” (case matters!)
  2. The secret code should start with the hashtag character (#)
# code for Q5
  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 (after the cleaning in Q5).
# code for Q6
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.
# code for Q7-8
Hint

You will need to join the secret_code information onto the county_map data.

Remember the secret_code column gives both the variable to fill by in the plot and the color to use. You will want to use the map_colors vector to use the right colors.

  1. Finally, also change the outline color for counties to the appropriate color for the county based on the secret_code. Submit a screenshot of this final plot in your Canvas submission!

4 Canvas Submission:

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