-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhate crimes analysis.Rmd
245 lines (189 loc) · 8.64 KB
/
hate crimes analysis.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
242
243
244
---
title: "Data Project 2"
output: html_notebook
---
Under New York State’s Hate Crime Law (Penal Law Article 485), a person commits a hate crime when one of a specified set of offenses is committed targeting a victim because of a perception or belief about their race, color, national origin, ancestry, gender, religion, religious practice, age, disability, or sexual orientation, or when such an act is committed as a result of that type of perception or belief.
```{r}
library(stringr)
library(dplyr)
library(magrittr)
library(readr)
library(lubridate)
library(tidyr)
library(ggplot2)
# modify your directory path to wherever you downloaded the file in the line below
crimes = read_csv("hate_crime.csv")
#convert date column to date type
crimes$INCIDENT_DATE = as.Date(crimes$INCIDENT_DATE,format="%d-%b-%y")
crimes$month = month(crimes$INCIDENT_DATE)
crimes
#table(crimes$BIAS_DESC)
```
Questions I will be answering:
- Which states/cities have the most reported hate crimes?
- How has the quantity/type of hate crimes changed over the years?
- Distribution of crimes by bias type --> which races experience the most hate crimes?
- Distribution of hate crime locations
- Relationship between offender race and bias type
- Relationship between crime location and bias type
#Top Hate Crime Bias-types
```{r}
bias <- crimes %>%
group_by(BIAS_DESC) %>%
tally()
bias <- filter(bias, n > 1000)
bias = bias[order(-bias$n),]
bias
#par(mar=c(10,4,4,4))
barplot(bias$n,ylab="count",main="Top Hate Crime Bias-types", names.arg=bias$BIAS_DESC, col = "black", las=2, cex.axis=.5, cex.names=0.5)
#keep only the bias_desc with freq > 1000
crimes = filter(crimes, BIAS_DESC %in% bias$BIAS_DESC)
```
#Total Hate Crimes Over the Years
```{r}
#count by year
crimesbyyear <- crimes %>%
group_by(DATA_YEAR) %>%
tally()
crimesbyyear
#barplot(crimesbyyear$n,xlab="Years",ylab="Count",main="Hate crimes count over the years", names.arg=crimesbyyear$DATA_YEAR, col = "blue", las=2)
#total hate crimes over the years
ggplot(data=crimesbyyear, aes(x=DATA_YEAR, y=n, group=1)) +
geom_line()+
geom_point() + ylab("Count") + ggtitle("Hate Crimes Frequency Over the Years") + xlab("Year") + scale_x_continuous(breaks = scales::pretty_breaks(n = 5))
```
#Hate Crimes Over the Years by Bias Type
```{r}
#count by bias type
bybias <- crimes %>%
group_by(DATA_YEAR, BIAS_DESC) %>%
tally()
bybias
#keep only top 5 bias_desc types
top5bias = head(bias,5)
top5bias_time = filter(bybias, BIAS_DESC %in% top5bias$BIAS_DESC)
#keep only top 10 bias_desc types
top10bias = head(bias,10)
top10bias_time = filter(bybias, BIAS_DESC %in% top10bias$BIAS_DESC)
top10bias_time
#top5
ggplot(data=top5bias_time, aes(x=DATA_YEAR, y=n)) +
geom_line(aes(color=BIAS_DESC))+
geom_point(aes(color=BIAS_DESC)) + scale_color_brewer(palette="Dark2") + ylab("Count") + ggtitle("Hate Crimes Over the Years by Top 5 Bias Types") + xlab("Year") + scale_x_continuous(breaks = scales::pretty_breaks(n = 5))
#top 10
ggplot(data=top10bias_time, aes(x=DATA_YEAR, y=n)) +
geom_line(aes(color=BIAS_DESC))+
geom_point(aes(color=BIAS_DESC)) + ylab("Count") + ggtitle("Hate Crimes Over the Years by Top 10 Bias Types") + xlab("Year") + scale_x_continuous(breaks = scales::pretty_breaks(n = 5))
#filter out anti-black for scale
topbias_nonafr = filter(top10bias_time, BIAS_DESC != "Anti-Black or African American")
ggplot(data=topbias_nonafr, aes(x=DATA_YEAR, y=n)) +
geom_line(aes(color=BIAS_DESC))+
geom_point(aes(color=BIAS_DESC)) + ylab("Count") + ggtitle("Hate Crimes Over the Years by Bias Type (minus Anti-African American)") + xlab("Year") + scale_x_continuous(breaks = scales::pretty_breaks(n = 5))
```
Bar charts for each bias type
```{r}
#anti-asian count by year
antiasian = filter(bybias, BIAS_DESC == "Anti-Asian")
barplot(antiasian$n,xlab="Year",ylab="Count",main="Anti-asian hate crimes count over the years", names.arg=antiasian$DATA_YEAR, col = "red", las=2)
#anti-jewish count by year
antijewish = filter(bybias, BIAS_DESC == "Anti-Jewish")
barplot(antijewish$n,xlab="Year",ylab="Count",main="Anti-jewish hate crimes count over the years", names.arg=antijewish$DATA_YEAR, col = "green", las=2)
#anti-black
antiblack = filter(bybias, BIAS_DESC == "Anti-Black or African American")
barplot(antiblack$n,xlab="Year",ylab="Count",main="Anti-black hate crimes count over the years", names.arg=antiblack$DATA_YEAR, col = "black", las=2)
#anti-white count by year
antiwhite = filter(bybias, BIAS_DESC == "Anti-White")
barplot(antiwhite$n,xlab="Year",ylab="Count",main="Anti-white hate crimes count over the years", names.arg=antiwhite$DATA_YEAR, col = "gray", las=2)
#anti-gay count by year
antigay = filter(bybias, BIAS_DESC == "Anti-Gay (Male)")
barplot(antigay$n,xlab="Year",ylab="Count",main="Anti-gay (male) hate crimes count over the years", names.arg=antigay$DATA_YEAR, col = "purple", las=2)
#anti-gay count by year
antigay = filter(bybias, BIAS_DESC == "Anti-Gay (Male)")
barplot(antigay$n,xlab="Year",ylab="Count",main="Anti-gay (male) hate crimes count over the years", names.arg=antigay$DATA_YEAR, col = "purple", las=2)
#anti-lesbian count by year
antilesbian = filter(bybias, BIAS_DESC == "Anti-Lesbian (Female)")
barplot(antilesbian$n,xlab="Year",ylab="Count",main="Anti-lesbian (female) hate crimes count over the years", names.arg=antilesbian$DATA_YEAR, col = "pink", las=2)
#anti-gay count by year
antihispanic = filter(bybias, BIAS_DESC == "Anti-Hispanic or Latino")
barplot(antihispanic$n,xlab="Year",ylab="Count",main="Anti-Hispanic hate crimes count over the years", names.arg=antihispanic$DATA_YEAR, col = "orange", las=2)
```
#Hate crimes monthly trend
```{r}
#count by month
crimesbymonth <- crimes %>%
group_by(month) %>%
tally()
crimesbymonth
barplot(crimesbymonth$n,xlab="Month",ylab="Count",main="Hate crimes yearly trend", names.arg= crimesbymonth$month, col = "black", las = 1, cex.axis=.75)
crimesbymonth$month = as.integer(crimesbymonth$month)
ggplot(data=crimesbymonth, aes(x=month, y=n, group=1)) +
geom_line()+
geom_point() + ylab("Count") + ggtitle("Hate Crimes Yearly Trend") + xlab("Month") + scale_x_continuous(breaks = scales::pretty_breaks(n = 12))
```
#Hate crimes by location
```{r}
#by state
crimesbystate <- crimes %>%
group_by(STATE_NAME) %>%
tally()
crimesbystate = crimesbystate[order(-crimesbystate$n),]
crimesbystate
#by agency(city)
crimesbyagency <- crimes %>%
group_by(PUB_AGENCY_NAME) %>%
tally()
crimesbyagency = crimesbyagency[order(-crimesbyagency$n),]
crimesbyagency
```
Cloroplethr Map
```{r}
install.packages("choroplethr")
library(choroplethr)
library(choroplethrMaps)
crimesbystate$region = tolower(crimesbystate$region)
colnames(crimesbystate) = c("region","value")
state_choropleth(crimesbystate, title = "Quantity of Hate Crimes", num_colors=9)
```
Stacked percentage plot
```{r}
#keep only top 5 bias types
crimestop5bias = filter(crimes, BIAS_DESC %in% top5bias$BIAS_DESC)
crimestop5bias
#keep only top 5 offender race types
top5bias = head(bias,5)
top5bias
offenderrace <- crimes %>%
group_by(OFFENDER_RACE) %>%
tally()
offenderrace
crimes_filter_race = filter(crimes, BIAS_DESC %in% top5bias$BIAS_DESC,OFFENDER_RACE %in% offenderrace$OFFENDER_RACE)
crimes_filter_race = select(crimes_filter_race, OFFENDER_RACE, BIAS_DESC)
crimes_filter_race
crimes_filter_race_counts = crimes_filter_race %>%
group_by(OFFENDER_RACE, BIAS_DESC) %>%
tally()
crimes_filter_race_counts
```
Relationship between offender race and bias_desc
```{r}
# Stacked + percent
raceplot = ggplot(crimes_filter_race_counts, aes(fill=OFFENDER_RACE, y=n, x=BIAS_DESC)) +
geom_bar(position="fill", stat="identity")
raceplot = raceplot + ylab("Percentage") + ggtitle("Relationship Between Offender Race and Bias Type") + xlab("Bias Type") + labs(fill = "Offender's Race") + coord_flip()
raceplot
```
#Location of Offense - Top 10
```{r}
crimes
crimesbylocation = crimes %>%
group_by(LOCATION_NAME) %>%
tally()
crimesbylocation = crimesbylocation[order(-crimesbylocation$n),]
crimesbylocation = head(crimesbylocation, 10)
crimesbylocation
ggplot(crimesbylocation, aes(x = reorder(LOCATION_NAME, n), y=n)) +
geom_bar(stat = "identity") + ylab("Count") + ggtitle("Hate Crimes by Location Type") + xlab("Location Type") + coord_flip()
```
Add a new chunk by clicking the *Insert Chunk* button on the toolbar or by pressing *Cmd+Option+I*.
When you save the notebook, an HTML file containing the code and output will be saved alongside it (click the *Preview* button or press *Cmd+Shift+K* to preview the HTML file).
The preview shows you a rendered HTML copy of the contents of the editor. Consequently, unlike *Knit*, *Preview* does not run any R code chunks. Instead, the output of the chunk when it was last run in the editor is displayed.