#Introduction
In this analysis, we explore a dataset detailing NYPD arrests on a year-to-date basis, focusing on various dimensions such as boroughs, race, arrest dates, gender, and precincts. Through five distinct visualizations, we aim to uncover patterns and insights into the dynamics of arrests across New York City. These visual representations include bar charts, pie charts, a heatmap, and a stacked bar chart, each designed to highlight different aspects of the data and offer a comprehensive understanding of arrest trends.
##Visualization 1
Bar Chart. This visualization shows the top 10 precincts in NYC with the higest arrest. The bar chart helps provide a deeper understanding of what area is making the most arrest this year so far.
##Visualization 2
Multiple Pie Charts. These pie charts help demonstrates among the known sexes of the arrests which race is making up the majority of the arrests. This helps us gain a better demographic underdstanding of which groups are commiting the most crime by gender
##Visualization 3
Stacked Bar Chart. In this visualization we can see over the last year which month had the most crime and illustrates which age group holds the majority. This helps us see which age groups are commining the most crime consistently.
##Visualization 4
Donut pie chart. With this visualization we are able to see among the five boroughs, where the most crime happens.
##Visualization 5
Heatmap. With this heatmap we can see that among the five boroughs which race is making up for the majority of the crime in the area. Through this we are able to narrow down even further where and who is responsible for the NYPD arrest thus far.
library(readr)
df <- read_csv("~/Desktop/Data Visualization/NYPDArrestYTD.csv")
options(repos = c(CRAN="https://cloud.r-project.org"))
#------Installing necessary packages-----------
install.packages("dplyr")
library(dplyr)
#-------Setting up Precinct data to be ready to visualized-------
count(df, ARREST_PRECINCT)
Precinct <- data.frame(count(df, ARREST_PRECINCT))
Precinct
Precinct <- Precinct[order(Precinct$n, decreasing = TRUE), ]
Precinct
install.packages("ggplot2")
library(ggplot2)
head(Precinct, 10)
str(Precinct)
#Creating Bar Chart
ggplot(Precinct[1:10,], aes(x = reorder(ARREST_PRECINCT, -n), y=n)) +
geom_bar(colour="black", fill="gray76", stat = "identity") +
labs(title = "Number of Arrests by Precinct (Top 10)", x = "Precinct", y = "Arrest Count") +
theme(plot.title = element_text(hjust = 0.5))
#-Multiple Pie Charts
#Load Libraries
library(dplyr)
library(lubridate)
library(ggplot2)
library(ggthemes)
#Seeing top arrests by sex
length(unique(df$PERP_SEX))
top_genders <-count(df, PERP_SEX)
top_genders[order(top_genders$n, decreasing = TRUE), ]
top_genders
#Grouping Arrests by Race and Gender
Arrest_by_sex_and_race <- df %>%
count(PERP_SEX, PERP_RACE)
Arrest_by_sex_and_race
#Plotting
ggplot(data = Arrest_by_sex_and_race, aes(x="", y = n, fill = PERP_RACE)) +
geom_bar(stat = "identity", position = "fill" ) +
coord_polar(theta = "y", start = 0) +
labs(fill="Race", x = NULL,y = NULL, title = "Arrests by Race and Sex") +
theme_light() +
theme(plot.title = element_text(hjust = 0.5),
axis.text = element_blank(),
axis.ticks = element_blank(),
panel.grid = element_blank()) +
facet_wrap(~PERP_SEX, ncol = 3, nrow = 3) +
scale_fill_brewer(palette = "greens")
#--------------------Stacked Bar Chart-------------------------------
# Load libraries
install.packages(c("ggplot2", "dplyr", "lubridate", "scales", "ggthemes", "RColorBrewer"))
library(ggplot2)
library(dplyr)
library(lubridate)
library(scales)
library(ggthemes)
library(RColorBrewer)
# Load data
df <- read.csv("~/Desktop/Data Visualization/NYPDArrestYTD.csv")
# Convert ARREST_DATE to a Date object
df$ARREST_DATE <- as.Date(df$ARREST_DATE, format = "%m/%d/%Y")
# Extract month and year from ARREST_DATE
df <- df %>%
mutate(month_year = format(ARREST_DATE, "%Y-%m"))
#includes head(df) and head(arrest_counts) to print the first few rows of the df and arrest_counts data frames. Please check the output to verify #that the month_year column is present in df and that the arrest_counts data frame is created as expected.
# Print the first few rows to check the data
head(df)
# Count arrests by month, year, and age group
arrest_counts <- df %>%
count(month_year, AGE_GROUP)
# Print the first few rows of arrest_counts to check the data
head(arrest_counts)
# Plotting
ggplot(data = arrest_counts, aes(x = month_year, y = n, fill = AGE_GROUP)) +
geom_bar(stat = "identity", position = "stack") +
theme_minimal() +
scale_fill_brewer(palette = "Set3") +
labs(title = "Arrests by Age Group in Each Month",
x = "Month",
y = "Number of Arrests")
#-------------------Pie Chart using Plotly--------------------------------
#Load Libraries
install.packages("plotly")
library(plotly)
library(scales)
#Separating data
length(unique(df$ARREST_BORO))
Boro_df <-count(df, ARREST_BORO)
Boro_df
#Creating inital Pie Chart
plot_ly(Boro_df, labels = ~ARREST_BORO,
values = ~n,
type = "pie", textposition =
"outside",
textinfo = "label + percent") %>%
layout(title
= "Arrests by NYC Borough YTD")
#Creating Doughnut Chart
Boro_df %>%
plot_ly(., labels = ~ARREST_BORO,
values = ~n,
type = "pie", textposition =
"outside",
textinfo = "label + percent") %>%
layout(title
= "Arrests by NYC Borough YTD")
#Plotting
plot_ly(Boro_df, labels = ~ARREST_BORO, values = ~n) %>%
add_pie(hole=0.6) %>%
layout(title="NYPD Arrests YTD by Borough") %>%
layout(annotations=list(text=paste0("Total Arrest Count:\n", scales::comma(sum(Boro_df$n))), "showarrow"=F))
#---------------------Heat Map---------------------------------
#Load Libraries
library(ggplot2)
library(dplyr)
library(lubridate)
library(scales)
library(ggthemes)
library(RColorBrewer)
#Creating Break
arrest_counts4 <- df %>%
count(PERP_RACE, ARREST_BORO)
arrest_counts4
Breaks <- c(seq(0, max(arrest_counts4$n), by=3500))
Breaks
#Plotting
v <- ggplot(arrest_counts4, aes(x = PERP_RACE, y = ARREST_BORO, fill=n)) +
geom_tile(color="black") +
geom_text(aes(label=comma(n))) +
coord_equal(ratio = 1) +
labs(title = "Heatmap: Arrests by Race in each Borough",
x = "Race",
y = "Borough",
fill= "Arrest Count") +
theme_minimal() +
theme(plot.title = element_text(hjust =0.5)) +
scale_y_discrete(limits = rev(levels(arrest_counts4$ARREST_BORO))) +
scale_fill_continuous(low="lightpink", high="darkred", breaks = Breaks) +
guides(fill =guide_legend(reverse = TRUE, override.aes = list(colour="black")))
#Fixing x-axis
library(plotly)
ggplotly(v, tooltip = c("n", "PERP_RACE", "ARREST_BORO")) %>%
style(hoverlabel = list(bgcolor = "white"))
v + theme(axis.text.x = element_text(angle = 45, hjust = 1)) +
theme(plot.margin = margin(30, 10, 10, 10, "pt"))