This post is intended to demonstrate some basic ways to map data in R. For our example, we will be creating a choropleth map of Michigan’s counties featuring COVID-19 data. The result is something quite similar to the map featured on the state’s dashboard. The data used in this post is from October 4, 2022.
For the sake of practice, we will walk through two different ways to go about this process. First we will use ggplot2. We will use a function called map_data to pull in shape file data easily. In our second example, we will use leaflet to create a better looking version of this map and use a raw shape file.
Ggplot2 map
To start, we will make a base map with ggplot2 and make it interactive with plotly. First, as always, we load in the libraries we will be using.
Code
# Load packages -----------------------------------------------------------library(tidyverse) # really just dplyr but the whole verse can't hurtlibrary(openxlsx) # to read in excel datalibrary(plotly) # for the interactive partlibrary(RColorBrewer) # to set our color palette
Next, we will get our county map. To do this we can simply call the function map_data and specify that we want it at the county level. This will give us data for every county in the US. Because we are only mapping Michigan, we add a second line to subset our first data frame ‘counties’ to only include Michigan.
Code
# Make the base state map -------------------------------------------------counties <-map_data("county")mi_county <-subset(counties, region =="michigan")
For our COVID-19 data, I am importing an older file from state’s website (linked previously). If you want a current version to follow along, you can find it there. Once the file is loaded into R Studio, we need to make a few adjustments. The original file splits the cases into two categories, confirmed and probable. On the state’s dashboard, they combine these numbers into a total for map reporting. We will do the same. This is easily done with the group_by and summarise functions. We will also change the county names to lowercase in preparation for merging.
Code
# Data prep ---------------------------------------------------------------micovid <-read.xlsx("Cases and Deaths by County 2022-10-04.xlsx")micovid <- micovid %>%group_by(COUNTY) %>%summarise(total_cases =sum(Cases),total_deaths =sum(Deaths)) %>%ungroup() %>%mutate(subregion =tolower(COUNTY))cases_and_county <-inner_join(mi_county, micovid, by ="subregion")cases_and_county <- cases_and_county %>%rename(county = COUNTY)