Introduction

Welcome to the Unit 1 Final Deliverable. This deliverable includes five separate visualizations. Embedded within the HTML document is the source code for each visualization.

Dataset

This Dataset was created by the Los Angeles Police Department. It includes all arrests is LA from 2020-2024. Each arrest includes data such as date, neighborhood, decription of offense and the sex of the individual arrested.

Findings

Tab 1

Bar Chart. Shows the top ten most arrested age groups in Los Angeles from 2020-2024. This bar chart serves to give an understanding the age demographic of people who are arrested, as all of the following visualizations utilize arrest data.

#------ Below is are some preliminary scrips in order to gain an understanding of the data set.

library(readr)
df <- read_csv("Arrest_Data_from_2020_to_Present.csv")
options(repos = c(CRAN = "https://cloud.r-project.org"))
problems()
colnames(df)
head(df)
tail(df)
dimensions_df <- dim(df)
print(dimensions_df)  

str(df)
summary(df)


unique(df$CALENDAR_YEAR)

#------ Below is a script which downloads the necessary packages and sets up the data for the first visualization.

install.packages("dplyr")
library(dplyr)
count(df, Age)
Agenumber <- data.frame(count(df, Age))
Agenumber
Agenumber <- Agenumber[order(Agenumber$n, decreasing = TRUE), ]
Agenumber

install.packages("ggplot2")
library(ggplot2)
head(Agenumber, 10)
str(Agenumber)
Agenumber$n <-as.numeric(Agenumber$n)
str(Agenumber)

#------ Visualization 1: Bar Chart. Shows the top ten most arrested age groups in Los Angeles from 2020-2024.
#------ This bar chart serves to give an understanding the age demographic of people who are arrested, as all of the following visualizations utilize arrest data. 

ggplot(Agenumber[1:10,], aes(x = reorder(Age, -n), y = n)) + 
  geom_bar(colour = "black", fill = "gray76", stat = "identity") +
  labs(title = "Number of Arrests by Age (Top10)") +
  theme(plot.title = element_text(hjust = 0.5))

Tab 2

Visulization 2 Line Graph. This line graph displays the total amount of charges for each month of the calendar year.This visualization serves to give a general overview of when arrests occur. Future charts delve deeper into this data.This visualization serves to give a general overview of when arrests occur. Future charts delve deeper into this data.

#------ Below is the creation of a data frame which displays charge group descriptions (reason for an arrest). This will be utilized in Data Visualization 5
library(dplyr)

colnames(df) <- make.names(colnames(df))

df_reasons <- count(df, Charge.Group.Description)
head(df_reasons)

df_reasons <- df_reasons[order(df_reasons$n, decreasing = TRUE),]

top_reasons <- df_reasons$Charge.Group.Description[1:10]
top_reasons

df$Charge.Group.Description %in% top_reasons
table(df$Charge.Group.Description %in% top_reasons)

library(dplyr)

library(lubridate)  
new_df <- df %>%
  filter (Charge.Group.Description %in% top_reasons) %>%
  select(Arrest.Date, Charge.Group.Description) %>%
  mutate(year = year(mdy_hms(Arrest.Date))) %>%
  group_by(Charge.Group.Description, year) %>%
  summarise(n = length(Charge.Group.Description), .groups = 'keep') %>%
  data.frame()
head(new_df)



#------ Visulization #2 Line Graph. This line graph displays the total amount of charges for each month of the calendar year.
#------ This visualization serves to give a general overview of when arrests occur. Future charts delve deeper into this data.

library(lubridate)
library(dplyr)
library(scales)
library(ggthemes)
library(ggplot2)
library(ggrepel)

months_df <- df %>%
  select(Arrest.Date) %>%
  mutate(month12 = month(mdy_hms(Arrest.Date))) %>%
  group_by(month12) %>%
  summarise(n = length(Arrest.Date), .groups = 'keep') %>%
  data.frame()

str(months_df)

x_axis_labels <- min(months_df$month12):max(months_df$month12)

hi_lo <- months_df %>%
  filter(n == min(n) | n == max(n)) %>%
  data.frame()

ggplot(months_df, aes(x = month12, y = n)) +
  geom_line(color = 'black', size = 1) +
  geom_point(shape = 21, size = 4, color = 'red', fill = 'white') +
  labs(x = "Month", y = "Number of Arrests", title = "Number of Arrests Per Month", caption = "Source: LAPD") +
  theme_light() +
  theme(plot.title = element_text(hjust = 0.5)) +
  scale_x_continuous(labels = x_axis_labels, breaks = x_axis_labels, minor_breaks = NULL) +
  geom_point(data = hi_lo, aes(x = month12, y = n), shape = 21, size = 4, fill = 'blue', color = 'blue') +
  geom_label_repel(aes(label = ifelse(n == max(n) | n == min(n), scales::comma(n) , "")),
                   box.padding = 1,
                   point.padding = 1,
                   size = 4,
                   color = "grey50", 
                   segment.color = 'darkblue')

Tab 3

Visualization 3 Multiple Bar Charts. This visualization displays arrests by month and year through a bar chart for each year. This chart delves deeper into the data the previous chart showed by offering insight total monthly arrests for each individdual calendar year.

#------ Visualization 3 Multiple Bar Charts. This visualization displays arrests by month and year through a bar chart for each year.
#------ This chart delves deeper into the data the previous chart showed by offering insight total monthly arrests for each individdual calendar year.

library(ggplot2)
library(dplyr)
library(lubridate)
library(scales)
library(ggthemes)
library(RColorBrewer)

months_df <- df %>%
  select(Arrest.Date) %>%
  mutate(month12 = month(mdy_hms(Arrest.Date))) %>%
  group_by(month12) %>%
  summarise(n = n(), .groups = 'keep') %>%
  data.frame()

months_df <- df %>%
  select(Arrest.Date) %>%
  mutate(months = months(mdy_hms(Arrest.Date), abbreviate = TRUE),
         year = year(mdy_hms(Arrest.Date))) %>%
  group_by(year, months) %>%
  summarise(n = n(), .groups='keep') %>%
  data.frame()

str(months_df)

mymonths <- c("Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec")
month_order <- factor(months_df$months, level=mymonths)

months_df$year <- as.factor(months_df$year)

ggplot(months_df, aes(x = month_order, y = n, fill = year)) +
  geom_bar(stat = "identity", position = "dodge") +
  theme_light() +
  labs(title = "Multiple Bar Charts- Arrests per Month and Year", 
       x = "Month", 
       y = "Number of Arrests", 
       fill = "Year") +
  scale_fill_brewer(palette = "Set2") +
  facet_wrap(~year, ncol=3, nrow=2) +
  theme(axis.text.x = element_text(size = 5))

Tab 4

Visualization 4 Multiple Pie Charts. Each pie chart displays the top five neighborhoods in LA with the most amount of arrests and their percentage of total arrests.Visualizations for age demographics and total arrests have already been discovered. This chart now looks into where the arrests occur.

#------ Visualization 4 Multiple Pie Charts. Each pie chart displays the top five neighborhoods in LA with the most amount of arrests and their percentage of total arrests.
#------ Visualizations for age demographics and total arrests have already been discovered. This chart now looks into where the arrests occur.

library(dplyr)
library(lubridate)
library(ggplot2)
library(ggthemes)

unique(df$Area.Name)
top_areas <- df %>%
  count(Area.Name) %>%
  arrange(desc(n))
top_areas

top_areas[top_areas$Area.Name %in% c("Rampart","77th Street", "Central", "Pacific", "Hollywood"), "n"] /sum(top_areas$n)

area_df <- df %>%
  select(Area.Name, Arrest.Date) %>%
  mutate (year = year(mdy_hms(Arrest.Date)),
          myArea = ifelse(Area.Name == "Rampart", "Rampart", ifelse(Area.Name == "77th Street", "77th Street", ifelse(Area.Name == "Central", "Central", ifelse(Area.Name == "Pacific", "Pacific", ifelse(Area.Name == "Hollywood", "Hollywood", "Other")))))) %>%
  group_by(year, myArea) %>%
  summarise(n=length(myArea), .groups='keep') %>%
  group_by(year) %>%
  mutate(percent_of_total = round(100 * n / sum(n), 1)) %>%
  ungroup() %>%
  filter(myArea != "Other")

  data.frame()
area_df

area_df[area_df$year == 2020,]

str(area_df)
ggplot(data = area_df, aes(x="", y = n, fill = myArea)) +
  geom_bar(stat="identity", position="fill") +
  coord_polar(theta="y", start=0) +
  labs(fill = "States", x = NULL, y = NULL, title = "Arrests by Year and Area of LA") +
  theme_light() +
  theme(plot.title = element_text(hjust = 0.5),
        axis.text = element_blank(),
        axis.ticks = element_blank(),
        panel.grid = element_blank()) +
  facet_wrap(~year, ncol=3, nrow=2) +
  geom_text(aes(x=1.7, label=paste0(percent_of_total,"%")),
            size=4,
            position=position_fill(vjust=0.5))

Tab 5

Visualization 5 Stacked Bar Chart. This chart displays total arrest count by description of the charges. This was the final major statistical category that was not covered by the previous visualizations and offers important insight into why the arrests occurred.

#------ Visualization 5 Stacked Bar Chart. This chart displays total arrest count by description of the charges.
#------ This was the final major statistical category that was not covered by the previous visalizations and offers important insight into why the arrests occured.

new_df
install.packages("plyr")
library(ggplot2)
library(scales)
library(RColorBrewer)
library(ggthemes)
library(plyr)

str(new_df)
new_df$year <- as.factor(new_df$year)

ggplot(new_df, aes(x = reorder(Charge.Group.Description, n, sum), y = n, fill = year)) +
  geom_bar(stat="identity", position = position_stack(reverse = TRUE)) +
  coord_flip() +
  labs(title = "Arrest Count by Description", x="", y = "Arrest Count", fill = "Year") +
  theme_clean() +
  theme(plot.title = element_text(hjust = 0.5)) +
  scale_fill_brewer(palette="Paired", guide = guide_legend(reverse = TRUE))