/
veilofdarkness.Rmd
244 lines (188 loc) · 12.2 KB
/
veilofdarkness.Rmd
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
---
title: "Veil of Darkness"
author: "Andrew Ba Tran"
date: "May 8, 2016"
output: html_document
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
```
Dummy text
```{r cars, fig.height=6, fig.width=10}
library(lubridate)
library(dplyr)
library(maptools)
library(stringr)
library(tidyr)
library(ggalt)
library(ggplot2)
library(extrafont)
stops_sub <- read.csv("data/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("data/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.\nStops that occurred during the civil twilight were discarded."),
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.")
}
}
explore <- read.csv("data/mega_df.csv")
explore <- subset(explore, is.na(ReportingOfficerIdentificationID))
explore <- explore[c("DepartmentName", "minority_dark", "minority_light")]
explore$diff <- explore$minority_light-explore$minority_dark
explore$minority_light <- explore$minority_light/100
explore$minority_dark <- explore$minority_dark/100
explore <- subset(explore, !is.na(diff))
explore <- arrange(explore, desc(diff))
explore$DepartmentName <- factor(explore$DepartmentName, levels=rev(explore$DepartmentName))
percent_first <- function(x) {
x <- sprintf("%d%%", round(x*100))
x[2:length(x)] <- sub("%$", "", x[2:length(x)])
x
}
gg <- ggplot()
gg <- gg + geom_segment(data=explore, aes(y=DepartmentName, yend=DepartmentName, x=0, xend=1), color="#b2b2b2", size=0.15)
gg <- gg + geom_dumbbell(data=explore, aes(y=DepartmentName, x=minority_dark, xend=`minority_light`),
size=1.5, color="#b2b2b2", point.size.l=3, point.size.r=3,
point.colour.l="tomato", point.colour.r="steelblue")
# point.colour.l="tomato", point.colour.r="steelblue"
#gg <- gg + geom_lollipop(point.colour="steelblue", point.size=3, horizontal=TRUE)
gg <- gg + scale_x_continuous(expand=c(0,0), limits=c(0, 1))
# text below points
gg <- gg + geom_text(data=filter(explore, DepartmentName=="Hartford"),
aes(x=minority_dark, y=DepartmentName, label="Stopped in darkness"),
color="tomato", size=5, vjust=-2, hjust=1, fontface="bold", family="Calibri")
gg <- gg + geom_text(data=filter(explore, DepartmentName=="Hartford"),
aes(x=minority_light, y=DepartmentName, label="Stopped in daylight"),
color="steelblue", size=5, vjust=-2, hjust=.7, fontface="bold", family="Calibri")
# text above points
gg <- gg + geom_text(data=explore, aes(x=minority_dark, y=DepartmentName, label=percent_first(minority_dark)),
color="tomato", size=5.5, hjust=1.75, family="Calibri")
gg <- gg + geom_text(data=explore, color="steelblue", size=5.5, hjust=-.5, family="Calibri",
aes(x=minority_light, y=DepartmentName, label=percent_first(minority_light)),
color="tomato", size=5.5, hjust=-.8, family="Calibri")
# difference column
gg <- gg + geom_rect(data=explore, aes(xmin=.94, xmax=1, ymin=-Inf, ymax=Inf), fill="#efefe3")
gg <- gg + geom_text(data=explore, aes(label=round(diff,0), y=DepartmentName, x=.97), fontface="bold", size=6, family="Calibri")
gg <- gg + geom_text(data=filter(explore, DepartmentName=="Hartford"), aes(x=.97, y=DepartmentName, label="DIFF"),
color="#7a7d7e", size=5, vjust=-2, fontface="bold", family="Calibri")
#gg <- gg + scale_x_continuous(expand=c(0,0), limits=c(0, 1.175))
gg <- gg + scale_y_discrete(expand=c(0.045,0))
gg <- gg + labs(x=NULL, y=NULL, title="Daylight vs darkness minorities stopped",
subtitle="In all towns but four, departments pulled over minorities at a higher rate during day light than darkness.",
caption="CCSU Institute for Municipal and Regional Policy Management")
gg <- gg + theme_bw(base_family="Calibri")
gg <- gg + theme(text = element_text(size=20))
gg <- gg + theme(panel.grid.major=element_blank())
gg <- gg + theme(panel.grid.minor=element_blank())
gg <- gg + theme(panel.border=element_blank())
gg <- gg + theme(axis.ticks=element_blank())
gg <- gg + theme(axis.text.x=element_blank())
gg <- gg + theme(plot.title=element_text(face="bold", family="Lato Black", size=22))
gg <- gg + theme(plot.subtitle=element_text(face="italic", size=9, margin=margin(b=12)))
gg <- gg + theme(plot.caption=element_text(size=7, margin=margin(t=12), color="#7a7d7e"))
gg
ggsave(gg, file = "img/big.png", width = 9, height = 22, type = "cairo-png")
```