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")

Steps

Write an Rmd script that:

  • Downloads block-level data on population by race in each census block in Buffalo using 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) 
  • Crop the county-level data to 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).
  • Write a foreach loop that does the following steps for each racial group in the variable column of the erie dataset and rbinds 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().
    • filter the the data to include only one race at time
    • use 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.
    • convert the points from 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)
  • Use the 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.
buffalo <- erie %>% 
   st_crop(c(xmin=-78.9,xmax=-78.85,ymin=42.888,ymax=42.92))



buffalo_dots=
   foreach(r=unique(buffalo$variable),.combine=rbind)%dopar%{
   filter(buffalo,variable==r) %>%  #do one variable at a time
   st_sample(size=.$value) %>%  #generate a point for each person randomly
   st_as_sf() %>%  #convert to a full sf object
   mutate(variable=r) #add the variable onto the points
   }

Your final result should look something like this:

Update the map to include:

  • Other racial groups
  • Adjust colors to match the original
  • Summarize the data in different ways (e.g. plot the polygon data, calculate indices, etc.)