#Question 1

#install.packages("trelliscopejs")
#install.packages("dslabs")
#install.packages("ggrepel")
#install.packages("tidyverse")
#install.packages("dplyr")
library(trelliscopejs)
## Warning: package 'trelliscopejs' was built under R version 4.5.2
## This package is no longer maintained. Please use the 'trelliscope' package instead (see https://github.com/trelliscope/).
library(tidyverse)
## Warning: package 'tidyverse' was built under R version 4.5.2
## Warning: package 'ggplot2' was built under R version 4.5.2
## Warning: package 'tidyr' was built under R version 4.5.2
## Warning: package 'readr' was built under R version 4.5.2
## Warning: package 'purrr' was built under R version 4.5.2
## Warning: package 'forcats' was built under R version 4.5.2
## Warning: package 'lubridate' was built under R version 4.5.2
## ── Attaching core tidyverse packages ──────────────────────── tidyverse 2.0.0 ──
## ✔ dplyr     1.1.4     ✔ readr     2.1.5
## ✔ forcats   1.0.1     ✔ stringr   1.5.2
## ✔ ggplot2   4.0.0     ✔ tibble    3.3.0
## ✔ lubridate 1.9.4     ✔ tidyr     1.3.1
## ✔ purrr     1.2.0
## ── Conflicts ────────────────────────────────────────── tidyverse_conflicts() ──
## ✖ dplyr::filter() masks stats::filter()
## ✖ dplyr::lag()    masks stats::lag()
## ℹ Use the conflicted package (<http://conflicted.r-lib.org/>) to force all conflicts to become errors
library(ggplot2)
library(dplyr)
library(readr)
cc <- read_csv("C:/Users/p_ric/OneDrive/Desktop/Abbey/RDU/WK 4/Cities Comparison.csv")
## Rows: 152 Columns: 13
## ── Column specification ────────────────────────────────────────────────────────
## Delimiter: ","
## chr  (2): Agency, Year Interval
## dbl (11): Year, Total Sworn, Population, Pop. Change (%), Sworn Rate Per-Cap...
## 
## ℹ Use `spec()` to retrieve the full column specification for this data.
## ℹ Specify the column types or set `show_col_types = FALSE` to quiet this message.
cc <- cc %>%
  group_by(Agency) %>%
  mutate(mean_clearance = mean(`Case Clearance (%)`, na.rm = TRUE), 
         mean_clearance_cog = cog(mean_clearance, 
                                  desc = "Mean case clearance rate for this agency",default_label = TRUE, default_active = TRUE))

scale_factor <- max(cc$`Workload Ratio`, na.rm = TRUE)/max(cc$`Case Clearance (%)`, na.rm = TRUE)  
  
ggplot(cc, aes(x= Year)) +
  geom_line(aes(y = `Workload Ratio`, color = "Workload"), size = 1.2) +
  geom_point(aes(y = `Workload Ratio`, color = "Workload")) +
  geom_line(aes(y = `Case Clearance (%)` * scale_factor, color = "Clearance %"), size = 1.2) +
  geom_point(aes(y = `Case Clearance (%)` * scale_factor, color = "Clearance %")) +
  scale_y_continuous( name = "Workload Ratio", 
                      sec.axis = sec_axis(~ . / scale_factor, name = "Case Clearance Rate")) +
  theme_dark() +
  theme(legend.text = element_text(size = 5), legend.title = element_text(size = 5)) +
  labs(title = "Workload vs Clearance Rate by Agency") +
  
   facet_trelliscope(~ Agency,
                    name = "Agency",
                    desc = "Case Clearance vs Workload",
                    nrow = 2, ncol = 2,
                    path = ".",
                    self_contained = TRUE)
## Warning: Using `size` aesthetic for lines was deprecated in ggplot2 3.4.0.
## ℹ Please use `linewidth` instead.
## This warning is displayed once every 8 hours.
## Call `lifecycle::last_lifecycle_warnings()` to see where this warning was
## generated.
## using data from the first layer
## Warning: Removed 1 row containing missing values or values outside the scale range
## (`geom_point()`).
## Warning: Removed 1 row containing missing values or values outside the scale range
## (`geom_point()`).
#I have been examining the relationship between the Belmont Police Department’s population growth, calls for service, and workforce to understand how the demand for police services has changed over time. This dataset contains information about seven other law enforcement agencies that police similar-sized cities or populations across North Carolina.

#In previous graphs, I focused on the Belmont Police Department’s call volume; however, the data revealed sudden, abnormal spikes in call volume, while population levels remained relatively stable. Suggesting the method used to count calls has changed over the years. According to the FBI, the crime index is made up of eight serious crimes: murder, rape, robbery, aggravated assault, burglary, larceny-theft, auto theft, and arson (Crime Index | Research Starters | EBSCO Research, n.d.). These types of crimes tend to generate heavy workloads with in-depth investigations requiring crime scene responses, evidence collection, interviews, warrants, and reports.  Therefore, I calculated the workload by examining the crime index and the total number of sworn officers, as the index crimes provided a more consistent baseline.

#Additionally, using index crime allowed me to make workload comparisons across multiple agencies. Additionally, I aimed to investigate the case clearance rate in relation to the workload. Using these two variables on a dual-axis time-series line graph allowed me to gain insight into how an officer’s workload impacts investigation performance.  

#By facetting the plots, I was able to examine the case clearance rate in relation to the workload of multiple agencies without overplotting. This approach made it easier to read while still allowing me to compare Belmont to other agencies side by side. Based on the graph, Belmont illustrates a clear relationship between clearance rate and workload. As the workload increases in Belmont, the clearance rate drops. Reidsville also illustrates a similar relationship with other agencies, such as Lenior and Mount Holly, at certain moments in time. Laurinburg depicted a steadily heavy workload and a low clearance rate. Overall, the clearance rate in relation to the workload would not be a standalone indicator for increasing staffing levels, but it makes a good starting point.

#The most challenging aspect of this assignment was creating a dual y-axis. My initial graph would plot the workload ratio, but provide a flat line for the case clearance rate. What I discovered was that my scale was too big. To fix it, I adjusted the “scale_factor” and “sec.axis” so that the secondary axis remained in relation to its data points. After solving that problem and looking at the graphs, I then realized that I needed to apply a legend, which created a legend key that squished the graph together, making it difficult to read. I then went into “theme” and set parameters for the legend’s text, decreasing its size.

#Additionally, I decreased the number of columns from four to two. I then added a cognostic illustrating the mean of cases cleared per agency across all the years. This added step enabled me to assess which agencies performed best at a glance, allowing me to dig deeper and dissect what may have led to those results. While my research aims to justify the expansion of the police force for the Belmont Police Department, it may also lead to identifying training or efficiency issues that can improve the quality of service the department provides.

#References

#Crime Index | Research Starters | EBSCO Research. (n.d.). EBSCO. https://www.ebsco.com/research-starters/law/crime-index