Make a choropleth map using any data except the state.x77 data set used in the class notes. Ensure the visual elements make it clear what data the choropleth is representing and that there are no warnings, messages or typos in the report.
Initialize the required libraries in order to (a) register our Google Maps API key (results set to hide), (b) load in US map data, (c) obtain today’s current COVID-19 hospitalizations by state (courtesy of the covid19us package) and (d) join the data frames based on state abbreviation.
# Load required libraries
library (knitr)
library (tidyverse)
library (lubridate)
library (ggmap)
library (covid19us)
# Initialize Google Maps API Key
register_google (key = readLines ("google-api.key"))
getOption ("ggmap")
# Load US map data
mapDataUS <- map_data (map = "state")
# Obtain today's US COVID-19 case information
covidDataUS = covid19us::get_states_current ()
# Add state abbreviations to US map data
mapDataUS$state <- state.abb [match (mapDataUS$region, tolower (state.name))]
# Join US map and COVID-19 data by state abbreviated for charting
chartData <- mapDataUS %>%
inner_join (covidDataUS, by = "state")
Chart today’s current COVID-19 hospitalizations by state as a choropleth.
# Build a title based on month, day and year our chart data was pulled
title <- sprintf ("Current COVID-19 Hospitalizations By State: %s %d, %d",
month (chartData$date [1], label=TRUE, abbr=FALSE),
as.numeric (day (chartData$date [1])),
as.numeric (year (chartData$date [1])))
# Plot our choropleth
ggplot (chartData, aes (x = long, y = lat, group = group)) +
geom_polygon (aes (fill = hospitalized_currently), color = "white") +
scale_fill_distiller ("Currently Hospitalized", palette = "Spectral", breaks = c (1000, 3000, 5000, 7000)) +
ggtitle (title) +
theme_void ()