Skip to content

Instantly share code, notes, and snippets.

View andrewbtran's full-sized avatar

Andrew Tran andrewbtran

View GitHub Profile
@andrewbtran
andrewbtran / Formatting dates into AP style
Created August 22, 2016 14:57
Formatting dates into AP style strings (Rare since most people want to take strings and turn them into dates)
# 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")
@andrewbtran
andrewbtran / Choropleth 1.5
Created August 7, 2016 00:31
Mapping only tracts with data
# 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="")
@andrewbtran
andrewbtran / Visualizing geographic disparity 2
Created August 7, 2016 00:16
Visualizing geographic disparity 2
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)))
@andrewbtran
andrewbtran / Visualizing geographic disparity 1
Created August 7, 2016 00:15
Visualizing geographic disparity 1
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")
@andrewbtran
andrewbtran / Calculating disparity
Created August 7, 2016 00:14
Calculating gap between percent stops and percent population for minorities
# 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)
@andrewbtran
andrewbtran / Importing Census data
Created August 7, 2016 00:13
Importing Census data
# 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"
@andrewbtran
andrewbtran / Analyzing the data
Created August 7, 2016 00:11
Calculating the minority percent of traffic stops by census tract
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 %>%
@andrewbtran
andrewbtran / Choropleth 2
Created August 7, 2016 00:09
Making another choropleth with an additional layer
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) +
@andrewbtran
andrewbtran / Choropleth 1
Last active August 7, 2016 00:31
Making a choropleth of stops in Hamden
# 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() +
@andrewbtran
andrewbtran / points in a polygon
Created August 7, 2016 00:06
Analyzing points in a polygon in R
# 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)