── Attaching core tidyverse packages ──────────────────────── tidyverse 2.0.0 ──
✔ dplyr 1.1.4 ✔ readr 2.1.5
✔ forcats 1.0.0 ✔ stringr 1.5.1
✔ ggplot2 3.5.0 ✔ tibble 3.2.1
✔ lubridate 1.9.3 ✔ tidyr 1.3.1
✔ purrr 1.0.2
── Conflicts ────────────────────────────────────────── tidyverse_conflicts() ──
✖ dplyr::filter() masks stats::filter()
✖ dplyr::lag() masks stats::lag()
ℹ Use the conflicted package (<http://conflicted.r-lib.org/>) to force all conflicts to become errors
library(lubridate)library(scales)
Attaching package: 'scales'
The following object is masked from 'package:purrr':
discard
The following object is masked from 'package:readr':
col_factor
library(tidycensus) # gets census data that we can use to create mapslibrary(sf) # helper package for mapping
Linking to GEOS 3.11.0, GDAL 3.5.3, PROJ 9.1.0; sf_use_s2() is TRUE
library(leaflet) # interactive mapping packagelibrary(usdata) # this package has a conversion utility for state abbreviations to full names
Choropleth maps
‘Choropleth’ is an obscure-sounding term, but it is a common type of map where regions are different colors according to some statistic. An example that we have all seen is the US electoral map with red and blue states indicating which candidate or party won each state.
Census data
We’ll start out with census data because it has both the maps and the data (population, income, etc.). Later, we’ll separate these steps so we can learn to map any data.
You’ll need a free api key to get census access. Go here to get a key:
http://api.census.gov/data/key_signup.html
Then when you get the API key, put it into the quotes in census_api_key(““). After you run it, delete it so you don’t publish it to rpubs.
census_api_key("")
To install your API key for use in future sessions, run this function with `install = TRUE`.
This gets maps of the states along with population data from the census bureau:
states <-get_acs(geography ="state", # gets state by state datavariables ="B01003_001", # this is state populationgeometry =TRUE, # gets geometry (the maps)shift_geo = T) # shifts Hawaii and Alaska
Getting data from the 2018-2022 5-year ACS
Warning: The `shift_geo` argument is deprecated and will be removed in a future
release. We recommend using `tigris::shift_geometry()` instead.
Warning: • You have not set a Census API key. Users without a key are limited to 500
queries per day and may experience performance limitations.
ℹ For best results, get a Census API key at
http://api.census.gov/data/key_signup.html and then supply the key to the
`census_api_key()` function to use it throughout your tidycensus session.
This warning is displayed once per session.
Using feature geometry obtained from the albersusa package
Please note: Alaska and Hawaii are being shifted and are not to scale.
old-style crs object detected; please recreate object with a recent sf::st_crs()
Let’s take a look at the data we just got. Click on states in the viewer.
The data look a little different from our usual data. But note that it has state names under NAME, it has the population of each state under estimate, and geometry provides the data to create the map. So it includes both the map information and the data (population) that we’re going to map.
Here’s a really simple choropleth map using the data. All it does is take the state data, send it to ggplot, and fill (or color) the states with their population (called estimate).