This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# this uses the R library Lubridate | |
# if you don't have that installed, uncomment and run the line below | |
# install.packages("lubridate") | |
library(lubridate) | |
# formats month-day-year format | |
# October 1, 1980 or 10-1-1980 or 10/1/1980, etc | |
# example - format_mdy("10/1/1980") |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Filter out the tracts with NA in the total column | |
total_map <- subset(total_map, !is.na(total)) | |
tm_ct <- ggplot() + | |
geom_polygon(data = total_map, aes(x=long, y=lat, group=group, fill=total), color = "black", size=0.2) + | |
coord_map() + | |
scale_fill_distiller(type="seq", trans="reverse", palette = "Reds", breaks=pretty_breaks(n=10)) + | |
theme_nothing(legend=TRUE) + | |
labs(title="Where Hamden police conduct traffic stops", fill="") |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
pm_ct <- ggplot() | |
pm_ct <- pm_ct + geom_polygon(data = mapping_disparity, aes(x=long, y=lat, group=group, fill=min_disp/100), color="white", size=.25) | |
pm_ct <- pm_ct + geom_polygon(data = town_borders, aes(x=long, y=lat, group=group), fill=NA, color = "black", size=0.5) | |
pm_ct <- pm_ct + coord_map() | |
pm_ct <- pm_ct + scale_fill_distiller(type="seq", trans="reverse", palette = "PuOr", label=percent, breaks=pretty_breaks(n=10), name="Gap") | |
pm_ct <- pm_ct + theme_nothing(legend=TRUE) | |
pm_ct <- pm_ct + labs(x=NULL, y=NULL, title="Hamden: Minority traffic stops versus population") | |
pm_ct <- pm_ct + theme(text = element_text(size=15)) | |
pm_ct <- pm_ct + theme(plot.title=element_text(face="bold", hjust=.4)) | |
pm_ct <- pm_ct + theme(plot.subtitle=element_text(face="italic", size=9, margin=margin(l=20))) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
mapping_disparity <- left_join(towntracts, joined_tracts) | |
mapping_disparity <- subset(mapping_disparity, !is.na(min_disp)) | |
# A library for color scales | |
pm_ct <- ggplot() | |
pm_ct <- pm_ct + geom_polygon(data = mapping_disparity, aes(x=long, y=lat, group=group, fill=min_disp/100), color="white", size=.25) | |
pm_ct <- pm_ct + geom_polygon(data = town_borders, aes(x=long, y=lat, group=group), fill=NA, color = "black", size=0.5) | |
pm_ct <- pm_ct + coord_map() | |
pm_ct <- pm_ct + scale_fill_distiller(type="seq", trans="reverse", palette = "PuOr", label=percent, breaks=pretty_breaks(n=10), name="Gap") |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Determining the minority population by subtracting the white population from the total | |
race_tracts$minority_pop <- race_tracts$total_pop - race_tracts$white_pop | |
# Now figuring out the percent makeup of each census tract | |
race_tracts$white_pop_p <- round(race_tracts$white_pop/race_tracts$total_pop*100,2) | |
race_tracts$minority_pop_p <- round(race_tracts$minority_pop/race_tracts$total_pop*100,2) | |
kable(head(race_tracts,5)) | |
# Joining the two datframes | |
joined_tracts <- left_join(joined_tracts, race_tracts) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# If you do not yet have the censusapi package installed, uncomment the lines below and run them. | |
#install.packages("devtools") | |
#devtools::install_github("hrecht/censusapi") | |
library("censusapi") | |
# Loading my census key from an external script | |
source("keys.R") | |
# Replace census_key below with "your_own_key_whatever_it_is" |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
coords <- subset(stops, ethnicity=="Minority") | |
coords <- coords[c("InterventionLocationLongitude", "InterventionLocationLatitude")] | |
coords <- coords[complete.cases(coords),] | |
sp <- SpatialPoints(coords) | |
proj4string(sp) <- "+proj=longlat +datum=WGS84 +no_defs +ellps=WGS84 +towgs84=0,0,0" | |
proj4string(sp) | |
m_tract <- over(sp, towntracts_only) | |
m_tract <- m_tract %>% |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
townborders <- readOGR(dsn="shapes", layer="ctgeo") | |
townborders_only <- townborders | |
townborders<- fortify(townborders, region="NAME10") | |
# Subset the town borders to just Hamden since that's the department we're looking at | |
town_borders <- subset(townborders, id=="Hamden") | |
tm_ct <- ggplot() + | |
geom_polygon(data = total_map, aes(x=long, y=lat, group=group, fill=total), color = "black", size=0.2) + | |
geom_polygon(data = town_borders, aes(x=long, y=lat, group=group, fill=total), color = "black", fill=NA, size=0.5) + |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Join the by_tract points to polygon dataframe to the original census tracts dataframe | |
total_map <- left_join(towntracts, by_tract) | |
require(ggmap) | |
require(scales) | |
tm_ct <- ggplot() + | |
geom_polygon(data = total_map, aes(x=long, y=lat, group=group, fill=total), color = "black", size=0.2) + | |
geom_polygon(data = total_map, aes(x=long, y=lat, group=group, fill=total), color = "black", size=0.2) + | |
coord_map() + |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Calculating points in a polygon | |
by_tract <- over(sp, towntracts_only) | |
# What just happened: Every point in the list now has a corresponding census tract | |
kable(head(by_tract, 5)) | |
# Use dplyr to gather up and count how many instances of census tracts there are | |
require(dplyr) |