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 Rmd script that:
get_dicennial()
function of the
tidycensus
package. You can use the following code:library(tidycensus)
racevars <- c(White = "P005003",
Black = "P005004",
Asian = "P005006",
Hispanic = "P004003")
options(tigris_use_cache = TRUE)
erie <- get_decennial(geography = "block", variables = racevars,
state = "NY", county = "Erie County", geometry = TRUE,
summary_var = "P001001", cache_table=T)
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: