foreach()
loop to generate a point
representing each person in each census polygon (block/tract)foreach()
funtion to return a
spatial (sf
) objectThe census data do not include specific addresses (the finest spatial information is the census block), so it’s common to see chloropleths representing the aggregate statistics of the underlying polygon. This is accurate, but not so personal. Folks at the University of Virginia developed a simple yet effective visualization approach, called the ‘Racial Dot Map’ which conveys a simple idea - one dot equals one person. Here’s how it looks for Buffalo, NY.
The idea is really simple. One just randomly generates a point for each person of each racial identity within each polygon.
Can you do it? Can you do it using multiple cores on your computer?
library(tidyverse)
library(spData)
library(sf)
## New Packages
library(mapview) # new package that makes easy leaflet maps
library(foreach)
library(doParallel)
registerDoParallel(4)
getDoParWorkers() # check registered cores
To use the tidycensus package, you will need to load the package and set your Census API key. A key can be obtained from http://api.census.gov/data/key_signup.html. You will only need to do that once (unless you delete your .Renviron file or move to a different computer).
# go to http://api.census.gov/data/key_signup.html and get a key, then run the line below with your key. Don't push your key to github!
library(tidycensus)
census_api_key("YOUR API KEY GOES HERE")
Write an qmd script that:
get_dicennial()
function of the
tidycensus
package. You can use the following code:library(tidycensus)
race_vars <- c(
"Total Population" = "P1_001N",
"White alone" = "P1_003N",
"Black or African American alone" = "P1_004N",
"American Indian and Alaska Native alone" = "P1_005N",
"Asian alone" = "P1_006N",
"Native Hawaiian and Other Pacific Islander alone" = "P1_007N",
"Some Other Race alone" = "P1_008N",
"Two or More Races" = "P1_009N"
)
options(tigris_use_cache = TRUE)
erie <- get_decennial(geography = "block", variables = race_vars, year=2020,
state = "NY", county = "Erie County", geometry = TRUE,
sumfile = "pl", cache_table=T)
## Getting data from the 2020 decennial Census
## Using the PL 94-171 Redistricting Data Summary File
## Note: 2020 decennial Census data use differential privacy, a technique that
## introduces errors into data to preserve respondent confidentiality.
## ℹ Small counts should be interpreted with caution.
## ℹ See https://www.census.gov/library/fact-sheets/2021/protecting-the-confidentiality-of-the-2020-census-redistricting-data.html for additional guidance.
## This message is displayed once per session.
c(xmin=-78.9,xmax=-78.85,ymin=42.888,ymax=42.92)
to reduce
the computational burdern. Feel free to enlarge this area if your
computer is fast (or you are patient).variable
column of the
erie
dataset and rbind
s the results
(e.g. .combine=rbind
) into a single sf
object.
You may want to convert the variable column into a factor and use
levels()
or use unique()
.
st_sample()
to generate random points for each
person that resided within each polygon. If you use a pipe
(%>%
), you will have to set size=.$value
.
The .
indicates that the column comes from the dataset that
was passed to the function. See here for
details on how to use the .
in a pipe.st_sample()
to spatial features
with st_as_sf()
mutate
to add a column named variable
that
is set to the current racial group (from the foreach loop)mapview()
function in the mapview
package to make a leaflet map of the dataset and set the
zcol
to the racial identity of each point. You can adjust
any of the visualization parameters (such as cex
for size).
Read more about mapview
here. It’s a new and really easy way to make leaflet maps from many
types of spatial data.Your final result should look something like this: