knitr::opts_chunk$set(echo = TRUE)

Loading libraries

pacman::p_load(tmaptools)
pacman::p_load(tmap)
pacman::p_load(tigris)
pacman::p_load(censusapi)
pacman::p_load(dplyr)
pacman::p_load(shiny)
pacman::p_load(ggplot2)
pacman::p_load(sf)
pacman::p_load(ggpubr)

Reading shape files

stl_shapefile <- "/Users/yab/Downloads/stl_boundary/stl_boundary.shp"
stlgeo <- read_shape(file=stl_shapefile, as.sf = TRUE)
stl_pd_shapefile <- "/Users/yab/Downloads/STL POLICE DISTRICTS/GIS.STL.POLICE_DISTRICTS_2014.shp"
stlpd <- read_shape(file=stl_pd_shapefile, as.sf = TRUE)
stl_pd_pre14_shapefile <- "/Users/yab/Downloads/STL Police Districts - pre-2014/STLPOLICEDISTRICTSPRE2014.shp"
stlpd_pre14 <- read_shape(file=stl_pd_pre14_shapefile, as.sf = TRUE)
current <- qtm(stlpd)
pre14 <- qtm(stlpd_pre14)
#Getting tract boundaries 
tr00 <- tigris::tracts(state = "29", county = "510", year = "2000")
tr_sf00 <- st_as_sf(tr00)
qtm(tr00)

tr10 <- tigris::tracts(state = "29", county = "510", year = "2010")
tr_sf10 <- st_as_sf(tr10)

Getting CT ACS data

temp<- NULL
census<- NULL
for(i in 2010:2016)          
{
temp <- getCensus(name = "acs/acs5", vintage = i, key = key, 
                  vars = c("NAME", "B01001_001E", 
                           "B01001_002E", "B01001A_001E", 
                           "B01001B_001E", "B01001D_001E", 
                           "B01001G_001E", "B01001I_001E", 
                           "B17001_002E", "B19013_001E", "B17005_002E"
                           ), region = "tract:*", regionin = "state:29 + county:510")

#add a year variable 
temp <- mutate(temp, year = i) 

#add the data from the year to the full dataset
census<- rbind(census, temp) 
}

#create geoid and add to dataset
GEOID <- paste0(census$state, census$county, census$tract) 
census <- mutate(census, GEOID) 


#Make labels 
names(census) <- toupper(c("state", "county", "tract", "name", "total_population", "total_rural", "white", "black", "asian", "hispanic", "mixed", "male", "median_hh_income", "poverty", "year", "geoid")) 

census <- tbl_df(census) 

Getting dicennial data

#get sf3 2000 census data
census2000 <- getCensus(name = "sf3", vintage = 2000, 
                        key = key,
                        vars = c("NAME", "P001001", "P005005", 
                                 "P006002", "P006003", 
                                 "P006005", "P007010", "P006008", 
                                 "P008002","P053001", 
                                 "P087002"),
                        region = "tract:*", 
                        regionin = "state:29 + county:510") 
#set year variable
census2000$year <- 2000
#create geoid variable
census2000 <- tbl_df(census2000)
GEOID <- paste0(census2000$state, census2000$county, census2000$tract)
census2000 <- mutate(census2000, GEOID)

#set names
names(census2000) <- toupper(c("state", "county", "tract", "name", "total_population", "total_rural", "white", "black", "asian", "hispanic", "mixed", "male", "median_hh_income", "poverty", "year", "geoid")) 

Merging data

full_frame <- census
names(full_frame) <- tolower(names(full_frame))

#change tract names for later aggregation
full_frame$tract<- as.numeric(full_frame$tract)/100
names(full_frame)[names(full_frame)=="tract"] <- "tract1"
names(full_frame)[names(full_frame)=="geoid"] <- "tract"
full_frame <- arrange(full_frame, year, tract)

#Merge with SF 
mapdata <- merge(tr_sf10, full_frame, by.x = "GEOID10", by.y="tract")
mapdata00 <- merge(tr_sf00, census2000, by.x = "TRACTCE00", by.y = "TRACT")
names(mapdata00) <- tolower(names(mapdata00))
names(mapdata) <- tolower(names(mapdata))


mapdata <- mapdata %>% select(-tract1)
mapdata00 <- mapdata00 %>% rename(tractce10 = tractce00, statefp10 = statefp00, geoid10 = ctidfp00, name10 = name00, namelsad10 = namelsad00, mtfcc10 = mtfcc00, funcstat10 = funcstat00, aland10 = aland00, awater10 = awater00, intptlat10 = intptlat00, intptlon10 = intptlon00, countyfp10 = countyfp00) %>% select(-geoid)
mapdata2 <- rbind(mapdata, mapdata00)

Some static plots

(The black outline shows St.Louis 6 police districts)