This is an R Markdown Notebook. When you execute code within the notebook, the results appear beneath the code.
Try executing this chunk by clicking the Run button within the chunk or by placing your cursor inside it and pressing Cmd+Shift+Enter.
# Downloaded the data from https://catalog.data.gov/dataset/nypd-hate-crimes
# Remove first row which is the title and then export as csv in order to load the data set into R
# This just changes the types of some of the columns. For instance, I changed the ID and Month number to be of integer type
# (Probably was not necessary but just incase we need to do some calculations with it)
library(readr)
library(sf)
Linking to GEOS 3.11.0, GDAL 3.5.3, PROJ 9.1.0; sf_use_s2() is TRUE
library(dplyr)
Attaching package: ‘dplyr’
The following objects are masked from ‘package:stats’:
filter, lag
The following objects are masked from ‘package:base’:
intersect, setdiff, setequal, union
library(spData)
To access larger datasets in this package, install the spDataLarge package with:
`install.packages('spDataLarge', repos='https://nowosad.github.io/drat/', type='source')`
library(ggplot2)
library(ggthemes)
library(urbnmapr)
hate_crimes <- read_csv("~/Desktop/Spatial R 370/NYPD_Hate_Crimes.csv",
col_types = cols(`Full Complaint ID` = col_number(),
`Month Number` = col_integer(), `Record Create Date` = col_date(format = "%m/%d/%Y")))
# Replace the County names to more easy to understand names
hate_crimes$County[hate_crimes$County == "KINGS"] <- "BROOKLYN"
hate_crimes$County[hate_crimes$County == "RICHMOND"] <- "STATEN ISLAND"
hate_crimes$County[hate_crimes$County == "NEW YORK"] <- "MANHATTAN"
setwd("~/Desktop/Spatial R 370/nyc_neighborhoods")
Warning: The working directory was changed to /Users/kenlu/Desktop/Spatial R 370/nyc_neighborhoods inside a notebook chunk. The working directory will be reset when the chunk is finished running. Use the knitr root.dir option in the setup chunk to change the working directory for notebook chunks.
nyc <- st_read("NYC_Nhood ACS2008_12.shp")
Reading layer `NYC_Nhood ACS2008_12' from data source
`/Users/kenlu/Desktop/Spatial R 370/nyc_neighborhoods/NYC_Nhood ACS2008_12.shp' using driver `ESRI Shapefile'
Simple feature collection with 195 features and 98 fields
Geometry type: MULTIPOLYGON
Dimension: XY
Bounding box: xmin: -74.25559 ymin: 40.49612 xmax: -73.70001 ymax: 40.91553
Geodetic CRS: WGS 84
# Convert the integer borocode to their respective borough in string format
nyc$borocode[nyc$borocode == 1] <- "MANHATTAN"
nyc$borocode[nyc$borocode == 2] <- "BRONX"
nyc$borocode[nyc$borocode == 3] <- "BROOKLYN"
nyc$borocode[nyc$borocode == 4] <- "QUEENS"
nyc$borocode[nyc$borocode == 5] <- "STATEN ISLAND"
ggplot()+
geom_sf(data = nyc, mapping=aes(fill = borocode))+
labs(fill = "Borough")+
theme_void()
# Get number of hate crimes by boroughs
number_of_hate_crimes <- hate_crimes %>%
group_by(County) %>% count()
colnames(number_of_hate_crimes) <- c("Borough", "hate_crime_count")
ggplot(data = number_of_hate_crimes,
aes(x = Borough, y = hate_crime_count, group = hate_crime_count, fill = Borough)) +
geom_bar(stat = "Identity") +
labs(x = "Borough", y = "Total Hate Crimes",
title = "Total Hate Crimes by Borough") +
theme(axis.title.x=element_blank(),axis.text.x=element_blank()) +
theme(plot.title=element_text(hjust=0.5))
# Get number of each ethnic group being targeted and then get top 10 results
number_of_hate_by_ethnic_group <- hate_crimes %>%
group_by(`Bias Motive Description`) %>% count()
number_of_hate_by_ethnic_group <- number_of_hate_by_ethnic_group[order(number_of_hate_by_ethnic_group$n, decreasing = TRUE),]
number_of_hate_by_ethnic_group <- number_of_hate_by_ethnic_group %>% head(10)
ggplot(data = number_of_hate_by_ethnic_group,
aes(x = `Bias Motive Description`, y = n, fill = `Bias Motive Description`)) +
geom_bar(stat = "Identity") +
labs(x = "Group of People", y = "Number of Hate Crimes",
title = "Total Hate Crimes for each Group") +
theme(axis.title.x=element_blank(),axis.text.x=element_blank()) +
theme(plot.title=element_text(hjust=0.5))
# Load in police precincts shapefile in order to map our Hate Crime dataset
setwd("~/Desktop/Spatial R 370/Police Precincts")
Warning: The working directory was changed to /Users/kenlu/Desktop/Spatial R 370/Police Precincts inside a notebook chunk. The working directory will be reset when the chunk is finished running. Use the knitr root.dir option in the setup chunk to change the working directory for notebook chunks.
nyc_precincts <- st_read("geo_export_e4a7b6a4-8fe3-4f10-862a-dbe0ebfca9eb.shp")
Reading layer `geo_export_e4a7b6a4-8fe3-4f10-862a-dbe0ebfca9eb' from data source
`/Users/kenlu/Desktop/Spatial R 370/Police Precincts/geo_export_e4a7b6a4-8fe3-4f10-862a-dbe0ebfca9eb.shp'
using driver `ESRI Shapefile'
Simple feature collection with 77 features and 3 fields
Geometry type: MULTIPOLYGON
Dimension: XY
Bounding box: xmin: -74.25559 ymin: 40.49613 xmax: -73.70001 ymax: 40.91553
Geodetic CRS: WGS84(DD)
# change the CRS to match the one above
nyc_precincts <- st_transform(nyc_precincts, crs = 4326)
# Join the dataset to get the geometry
joined_hate_crimes <- hate_crimes %>%
left_join(nyc_precincts, by = c("Complaint Precinct Code" = "precinct"))
# Rename 'Complaint Precinct Code' column to just 'Precinct'
colnames(joined_hate_crimes)[5] <- "Precinct"
# Create a new column to see the number of crimes
joined_hate_crimes <- joined_hate_crimes %>%
group_by(Precinct) %>%
mutate(Complaint_Count = n())
# Map out the number of hate crime complaints by precinct
ggplot()+
geom_sf(data = joined_hate_crimes, mapping=aes(fill = Complaint_Count, geometry=geometry)) +
scale_fill_gradient(name = "Complaint Count", low = "lightblue", high = "darkblue") +
labs(fill = "Number of Complaints")+
theme_void()
# Now that we have some sort of visualization of the number of hate crimes in new york some questions we will try to answer are:
# Is there some sort of correlation between number of hate crimes and socioeconomic factors such as poverty ?
# Why does Staten Island and the Bronx have such low hate crimes compared to Brooklyn and Manhattan ? ( we can look at the type of hate crime and see is it because of the people that live in these areas ?)
# What are some indicators of higher hate crime rates.
# This will help us to learn whether we should concentrate resources in certain areas more than others.
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.