Variable | Description |
---|---|
fips | A 5 digit code that identifies each county |
county | County name |
state | State abbreviation |
year | Corresponding year for data |
number_pills | The number of Oxycodone & Hydrocodone pills purchased by pharmacies in the county |
population | The total number of people living in the county |
Apply and Practice Activity
ARCOS: Explore maps
Introduction
For this activity you will work with the WAPO compiled data repository on opioid pills that you studied in the Module 4 Handout on Data Wrangling. We will work with the county level data for Colorado. Each county in the US has up to nine rows of data in this data frame, one for each year from 2006 to 2014. The table below describes the variables that we will utilize:
Step by step directions
Step 1
Navigate to the apply_and_practice folder in the programs folder of the course project. Open up the file called arcos_maps.qmd.
To ensure you are working in a fresh session, close any other open tabs (save them if needed). Click the down arrow beside the Run button toward the top of your screen then click Restart R and Clear Output.
Once the .qmd file is open, add your name to the author section of the YAML metadata.
Step 2
First we need to load the packages that are needed for this activity. Find the code chunk labeled: Load packages in the .qmd file. Then load these packages:
library(viridis)
library(scales)
library(tigris)
library(here)
library(tidyverse)
Once entered, click run on the Load packages code chunk. Now, the packages are ready for you to use.
Step 3
Under the first level header
# Import data
Import the opioid counties data frame that we worked with in Module 4. Call the data frame opioid_counties.
Step 4
Under the first level header
# Subset the data
Create a new data frame called co — use filter() to include only the Colorado counties.
Step 5
Under the first level header
# Compute the pills per capita for all CO counties
Update the co data frame to compute a variable called pills_per_capita – which is the number of pills per capita in the county for each year. Note that your data frame should still have 9 rows of data for each Colorado county.
Step 6
Under the first level header
# Create a map of pills per capita over time
We’ll use the tigris package to create maps in R. The package is a powerful tool for accessing and working with geographic data from the US Census Bureau. It provides functions to download and work with various geographic data types, such as counties, states, census tracts, and more. The package is particularly useful for obtaining shapefiles, which can be used to create maps and perform spatial analyses.
Copy and paste the code below to create the maps:
## Set the option for shapefiles to load with sf
options(tigris_class = "sf")
## Function to download county shapefiles in Colorado
<- counties(state = "CO", cb = TRUE, progress_bar = FALSE)
co_shape
## Join the county data with the map data -- fips is called GEOID in the shape file
<-
for_map |>
co left_join(co_shape, by = c("fips" = "GEOID"))
|>
for_map ggplot(mapping = aes(geometry = geometry, fill = pills_per_capita)) +
facet_wrap(~year) +
geom_sf() +
coord_sf(crs = 4269) +
::scale_fill_viridis(direction = -1) +
viridistheme_void() +
theme(panel.grid.major = element_line(colour = 'transparent')) +
labs(title = "Opioid pills per capita in Colorado",
caption = "Source: The Washington Post, ARCOS",
fill = "Opioid pills per capita")
Here’s an explanation of the code:
Set an option for the tigris package, to load shapefiles using the sf (Simple Features) class, which is a modern way of handling spatial data in R.
Define an object co_shape that downloads county shapefiles for the state of Colorado with the attribute “cb” (census boundary) set to TRUE. We use the counties() function to download the shapefiles.
Join the county data with the map data using the left_join() function. The join is performed based on the fips column in the co data frame and the GEOID column in the co_shape data frame.
Create a choropleth map using ggplot2 and geom_sf() to plot the spatial data. The geom_sf() function is used to visualize the polygons representing the counties of Colorado. The geometry column from the for_map data frame holds the polygon geometries required for mapping.
The
facet_wrap(~year)
call is used to create multiple maps, one for each year. This allows us to see the temporal variation in opioid pills per capita in Colorado.coord_sf(crs = 4269)
sets the coordinate reference system (CRS) to EPSG 4269, which ensures that the map is properly projected. The specific value 4269 refers to the EPSG code for the North American Datum of 1983 (NAD83). This is a commonly used geographic coordinate system for data in North America. It uses latitude and longitude as coordinates.viridis::scale_fill_viridis(direction = -1)
sets the color scale for the choropleth map. The viridis package is used to create visually appealing color palettes.theme_void() is used to remove any unnecessary elements from the plot, such as background and axes.
theme(panel.grid.major = element_line(colour = 'transparent'))
is used to make the major grid lines transparent, improving the map’s appearance.labs() sets the title, caption, and legend title for the map.
Take a look at the resulting maps, and then write a few sentences to describe what you see and your insight.
Step 7
Now that you’ve completed all tasks, to help ensure reproducibility, click the down arrow beside the Run button toward the top of your screen then click Restart R and Clear Output. Scroll through your notebook and see that all of the output is now gone. Now, click the down arrow beside the Run button again, then click Restart R and Run All Chunks. Scroll through the file and make sure that everything ran as you would expect. You will find a red bar on the side of a code chunk if an error has occurred. Taking this step ensures that all code chunks are running from top to bottom, in the intended sequence, and producing output that will be reproduced the next time you work on this project.
Now that all code chunks are working as you’d like, click Render. This will create an .html output of your report. Scroll through to make sure everything is correct. The .html output file will be saved along side the corresponding .qmd notebook file.
Step 8
Follow the directions on Canvas for the Apply and Practice Assignment entitled “ARCOS Maps Apply and Practice Activity” to get credit for completing this assignment.