Dummy text
library(lubridate)
##
## Attaching package: 'lubridate'
## The following object is masked from 'package:base':
##
## date
library(dplyr)
##
## Attaching package: 'dplyr'
## The following objects are masked from 'package:lubridate':
##
## intersect, setdiff, union
## The following objects are masked from 'package:stats':
##
## filter, lag
## The following objects are masked from 'package:base':
##
## intersect, setdiff, setequal, union
library(maptools)
## Loading required package: sp
## Checking rgeos availability: TRUE
library(stringr)
library(tidyr)
library(ggalt)
## Loading required package: ggplot2
library(ggplot2)
stops_sub <- read.csv("stops_sub2.csv")
stops_sub$Department.Name <- as.character(stops_sub$Department.Name)
stops_sub$Department.Name <- ifelse(stops_sub$OrganizationIdentificationID=="CTCSP0000", "State Police: Headquarters", stops_sub$Department.Name)
stops_sub$Department.Name <- ifelse(stops_sub$OrganizationIdentificationID=="CTCSP0023", "State Police: Headquarters", stops_sub$Department.Name)
stops_sub$Department.Name <- ifelse(stops_sub$OrganizationIdentificationID=="CTCSP0029", "State Police: Headquarters", stops_sub$Department.Name)
stops_sub$Department.Name <- ifelse(stops_sub$OrganizationIdentificationID=="CTCSP1900", "State Police: Headquarters", stops_sub$Department.Name)
stops_sub$Department.Name <- ifelse(stops_sub$OrganizationIdentificationID=="CTCSP2900", "State Police: Headquarters", stops_sub$Department.Name)
stops_sub$Department.Name <- ifelse(stops_sub$OrganizationIdentificationID=="CTCSP3800", "State Police: Headquarters", stops_sub$Department.Name)
stops_sub$Department.Name <- ifelse(stops_sub$OrganizationIdentificationID=="CTCSP0200", "State Police: Troop A", stops_sub$Department.Name)
stops_sub$Department.Name <- ifelse(stops_sub$OrganizationIdentificationID=="CTCSP0300", "State Police: Troop B", stops_sub$Department.Name)
stops_sub$Department.Name <- ifelse(stops_sub$OrganizationIdentificationID=="CTCSP0400", "State Police: Troop C", stops_sub$Department.Name)
stops_sub$Department.Name <- ifelse(stops_sub$OrganizationIdentificationID=="CTCSP0500", "State Police: Troop D", stops_sub$Department.Name)
stops_sub$Department.Name <- ifelse(stops_sub$OrganizationIdentificationID=="CTCSP0600", "State Police: Troop E", stops_sub$Department.Name)
stops_sub$Department.Name <- ifelse(stops_sub$OrganizationIdentificationID=="CTCSP0700", "State Police: Troop F", stops_sub$Department.Name)
stops_sub$Department.Name <- ifelse(stops_sub$OrganizationIdentificationID=="CTCSP0800", "State Police: Troop G", stops_sub$Department.Name)
stops_sub$Department.Name <- ifelse(stops_sub$OrganizationIdentificationID=="CTCSP0900", "State Police: Troop H", stops_sub$Department.Name)
stops_sub$Department.Name <- ifelse(stops_sub$OrganizationIdentificationID=="CTCSP1000", "State Police: Troop I", stops_sub$Department.Name)
stops_sub$Department.Name <- ifelse(stops_sub$OrganizationIdentificationID=="CTCSP1100", "State Police: Troop J", stops_sub$Department.Name)
stops_sub$Department.Name <- ifelse(stops_sub$OrganizationIdentificationID=="CTCSP1200", "State Police: Troop K", stops_sub$Department.Name)
#stops_sub <- read.csv("stops_fixed.csv")
town_list <- c("Bloomfield",
"New Milford",
"Norwalk",
"West Hartford",
"Wethersfield",
"Stratford",
"Meriden",
"New Britain",
"Newington",
"Trumbull",
"Waterbury",
"State Police: Troop H")
for (i in 1:length(town_list)) {
stops_sub2 <- subset(stops_sub, Department.Name==town_list[i])
stops_sub2 <- subset(stops_sub2, light_dark!="neither")
stops_sub2$RealTime2 <- ymd_hms(stops_sub2$RealTime2, tz="America/New_York")
#stops_sub2 <- subset(stops, Department.Name=="Bloomfield")
stops_sub3 <- stops_sub2
stops_sub3$RealTime2 <- NULL
stops_sub3$sunrise <- NULL
stops_sub3$solarnoon <- NULL
stops_sub3$sunset <- NULL
light_dark_df1 <- stops_sub3 %>%
group_by(ethnicity_wm, light_dark) %>%
summarise(count=n()) %>%
spread(light_dark, count) %>%
mutate(dark_percent=(round(dark/(dark+light)*100,2)),light_percent=(round(light/(dark+light)*100,2)))
light_dark_df2 <- stops_sub3 %>%
group_by(ethnicity_wm, light_dark) %>%
summarise(count=n()) %>%
spread(ethnicity_wm, count) %>%
mutate(minority_percent=(round(Minority/(Minority+White)*100,2)),white_percent=(round(White/(Minority+White)*100,2)))
light_dark2 <- light_dark_df2 %>%
select(light_dark, minority_percent, white_percent) %>%
gather("type", "percent", 2:3)
light_dark2$type <- gsub("_percent", "", light_dark2$type)
light_percent <- light_dark2[2,3]
dark_percent <- light_dark2[1,3]
department_name <- town_list[i]
g <- ggplot(light_dark2, aes(x=light_dark, y=percent, fill=type)) +
geom_bar(stat="identity", colour="black") +
coord_flip() +
labs(x="Time", y="Percent",
title=paste0(department_name, " traffic stops: Daylight versus darkness"),
subtitle=paste0("During daylight hours, ", light_percent, "% of stops involved minority drivers; after dark, this figure changed to ", dark_percent, "%."),
caption="Traffic Stop Data Report, CCSU Institute for Municipal and Regional Policy")
print(g)
dark_percent <- light_dark_df2[1,4]
light_percent <- light_dark_df2[2,4]
## SUNSET
stops_sub3 <- subset(stops_sub2, (hours_since < 4 & hours_since > -4))
stops_sub3 <- subset(stops_sub3, RealTime2 < strptime("1899-12-30 21:15:00", "%Y-%m-%d %H:%M:%S") & RealTime2 > strptime("1899-12-30 17:15:00", "%Y-%m-%d %H:%M:%S"))
stops_sub3$ForRealTime <- ymd_hms(stops_sub3$ForRealTime)
stops_sub3$solarnoon <- ymd_hms(stops_sub3$solarnoon)
stops_sub3 <- subset(stops_sub3, ForRealTime > solarnoon)
p <- ggplot()
p <- p + geom_point(data=stops_sub3, aes(x=RealTime2, y=hours_since, colour=ethnicity_wm))
p <- p + geom_rect(aes(xmin = as.POSIXct(strptime("1899-12-30 17:15", format = "%Y-%m-%d %H:%M")), xmax = as.POSIXct(strptime("1899-12-30 21:15", format = "%Y-%m-%d %H:%M")), ymin = 0, ymax = 4), alpha = 0.1, fill = "grey")
p <- p + geom_hline(yintercept = 0)
p <- p + theme_minimal(base_family="Arial Narrow")
p <- p + theme(plot.title=element_text(face="bold"))
p <- p + labs(x="Clock time", y="Hours since darkness",
title=paste0(department_name, " traffic stops around sunset"),
subtitle=paste0("During daylight hours, ", light_percent, "% of stops involved minority drivers; after dark, this figure changed to ", dark_percent, "%. \nThe large diagonal gap is a result of the shift from Eastern Daylight Time to Eastern Standard Time."),
caption="Traffic Stop Data Report, CCSU Institute for Municipal and Regional Policy")
print(p)
## SUNRISE
stops_sub3 <- subset(stops_sub2, (hours_since_rise < 4 & hours_since_rise > -4))
stops_sub3 <- subset(stops_sub3, RealTime2 < strptime("1899-12-30 08:15:00", "%Y-%m-%d %H:%M:%S") & RealTime2 > strptime("1899-12-30 04:15:00", "%Y-%m-%d %H:%M:%S"))
stops_sub3$ForRealTime <- ymd_hms(stops_sub3$ForRealTime)
stops_sub3$solarnoon <- ymd_hms(stops_sub3$solarnoon)
stops_sub3 <- subset(stops_sub3, ForRealTime < solarnoon)
p <- ggplot()
p <- p + geom_point(data=stops_sub3, aes(x=RealTime2, y=hours_since_rise, colour=ethnicity_wm))
p <- p + geom_rect(aes(xmin = as.POSIXct(strptime("1899-12-30 04:15", format = "%Y-%m-%d %H:%M")), xmax = as.POSIXct(strptime("1899-12-30 08:15", format = "%Y-%m-%d %H:%M")), ymin = 0, ymax = 4), alpha = 0.1, fill = "grey")
p <- p + geom_hline(yintercept = 0)
p <- p + theme_minimal(base_family="Arial Narrow")
p <- p + theme(plot.title=element_text(face="bold"))
p <- p + labs(x="Clock time", y="Hours since sunlight",
title=paste0(department_name, " traffic stops around sunrise"),
subtitle=paste0("During daylight hours, ", light_percent, "% of stops involved minority drivers; after dark, this figure changed to ", dark_percent, "%. \nThe large diagonal gap is a result of the shift from Eastern Daylight Time to Eastern Standard Time."),
caption="Traffic Stop Data Report, CCSU Institute for Municipal and Regional Policy")
print(p)
cat(paste0("Minority to Whites stopped during daylight ratio: ", round(light_dark2[2,3]/light_dark2[4,3],2), "\n"))
cat(paste0("Minority to Whites stopped during darkness ratio: ", round(light_dark2[1,3]/light_dark2[3,3],2), "\n"))
if (light_dark2[2,3]/light_dark2[4,3] > light_dark2[1,3]/light_dark2[3,3]) {
cat("Minorities were more likely to be pulled over during daylight hours.")
} else {
cat("Minorities were less likely to be pulled over during daylight hours.")
}
}
## Minority to Whites stopped during daylight ratio: 2.68
## Minority to Whites stopped during darkness ratio: 1.17
## Minorities were more likely to be pulled over during daylight hours.
## Minority to Whites stopped during daylight ratio: 0.17
## Minority to Whites stopped during darkness ratio: 0.2
## Minorities were less likely to be pulled over during daylight hours.
## Minority to Whites stopped during daylight ratio: 1.23
## Minority to Whites stopped during darkness ratio: 0.66
## Minorities were more likely to be pulled over during daylight hours.
## Minority to Whites stopped during daylight ratio: 0.66
## Minority to Whites stopped during darkness ratio: 0.61
## Minorities were more likely to be pulled over during daylight hours.
## Minority to Whites stopped during daylight ratio: 1
## Minority to Whites stopped during darkness ratio: 0.88
## Minorities were more likely to be pulled over during daylight hours.
## Minority to Whites stopped during daylight ratio: 1.54
## Minority to Whites stopped during darkness ratio: 0.87
## Minorities were more likely to be pulled over during daylight hours.
## Minority to Whites stopped during daylight ratio: 1.53
## Minority to Whites stopped during darkness ratio: 0.91
## Minorities were more likely to be pulled over during daylight hours.
## Minority to Whites stopped during daylight ratio: 1.83
## Minority to Whites stopped during darkness ratio: 1.48
## Minorities were more likely to be pulled over during daylight hours.
## Minority to Whites stopped during daylight ratio: 0.82
## Minority to Whites stopped during darkness ratio: 0.54
## Minorities were more likely to be pulled over during daylight hours.
## Minority to Whites stopped during daylight ratio: 0.73
## Minority to Whites stopped during darkness ratio: 0.59
## Minorities were more likely to be pulled over during daylight hours.
## Minority to Whites stopped during daylight ratio: 2.43
## Minority to Whites stopped during darkness ratio: 1.06
## Minorities were more likely to be pulled over during daylight hours.
## Minority to Whites stopped during daylight ratio: 1.07
## Minority to Whites stopped during darkness ratio: 0.59
## Minorities were more likely to be pulled over during daylight hours.