library(plotly)
## Loading required package: ggplot2
##
## Attaching package: 'plotly'
## The following object is masked from 'package:ggplot2':
##
## last_plot
## The following object is masked from 'package:stats':
##
## filter
## The following object is masked from 'package:graphics':
##
## layout
library(trelliscopejs)
## This package is no longer maintained. Please use the 'trelliscope' package instead (see https://github.com/trelliscope/).
library(tidyverse)
## ── Attaching core tidyverse packages ──────────────────────── tidyverse 2.0.0 ──
## ✔ dplyr 1.1.4 ✔ readr 2.1.6
## ✔ forcats 1.0.1 ✔ stringr 1.6.0
## ✔ lubridate 1.9.4 ✔ tibble 3.3.0
## ✔ purrr 1.2.0 ✔ tidyr 1.3.1
## ── Conflicts ────────────────────────────────────────── tidyverse_conflicts() ──
## ✖ dplyr::filter() masks plotly::filter(), 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(dslabs)
library(readr)
cc <- read_csv("C:/Users/p_ric/OneDrive/Desktop/Abbey/RDU/WK 5/Cities Comparison.csv")
## Rows: 152 Columns: 13
## ── Column specification ────────────────────────────────────────────────────────
## Delimiter: ","
## chr (3): Agency, Year Interval, Case Clearance (%)
## dbl (10): 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 %>%
filter(Agency %in% c("Belmont", "Mount Holly", "Reidsville", "Wendell"))
cc %>%
plot_ly(., x = ~Arrest, y = ~`Crime Rate`, hoverinfo = "text",
text = ~ paste("Agency:", Agency,
"<br>Year:", Year,
"<br>Arrests:", Arrest,
"<br>Crime Rate:", `Crime Rate`,
"<br>Sworn Rate Per-Capita:", `Sworn Rate Per-Capita`,
"<br>Workload Ratio:", `Workload Ratio`,
"<br>Clearance Rate:", `Case Clearance (%)` )) %>%
add_markers( showlegend = TRUE, size = ~`Workload Ratio`, color = ~Agency) %>%
layout( title = "Arrests vs Crime Rate by Agency", xaxis = list(title = "Total Arrests"),
yaxis = list(title = "Crime Rate (per 100,000 )"), legend = list(title = list(text = "Agency")))
## Warning: Ignoring 1 observations
## Warning: `line.width` does not currently support multiple values.
## Warning: `line.width` does not currently support multiple values.
## Warning: `line.width` does not currently support multiple values.
## Warning: `line.width` does not currently support multiple values.
In the last assignment, I examined the case clearance rate in relation to the workload of seven different agencies, comparing them to Belmont. While I gained insight that I found to be helpful, I also realized there was too much information to compare. During this assignment, I decided to focus on the three agencies that were most comparable to Belmont: Mount Holly, Reidsville, and Wendell. This scatter plot illustrates the relationship between the total arrests and crime rate for the four agencies for 18 years. Each data point represents a single agency within the given year. Additionally, the size of the data point reflects the officer’s workload for that given year. At a glance, the scatter plot reveals a pattern of higher crime rates in years with a higher number of arrests. Additionally, as the crime rate increased, so did the workload. To capture the complete picture of each data point, hovering over a point allows viewers to view the year, number of arrests, crime rate, sworn rate per capita, workload ratio, and case clearance rate. This approach aims to enable the viewer to see how an increase in the crime rate affects workload and case clearance rates, while the sworn rate per capita provides context for the workload observed. The only challenge I experienced was adding multiple texts to the hover section, as I had forgotten to include the paste argument in my text. Fortunately, I recalled Dr. Combs discussing how to perform that task on the video, and after reviewing the video, I was able to complete the task.
library(plotly)
library(trelliscopejs)
library(tidyverse)
library(ggplot2)
library(dplyr)
library(dslabs)
library(readr)
cc <- read_csv("C:/Users/p_ric/OneDrive/Desktop/Abbey/RDU/WK 5/Cities Comparison.csv")
## Rows: 152 Columns: 13
## ── Column specification ────────────────────────────────────────────────────────
## Delimiter: ","
## chr (3): Agency, Year Interval, Case Clearance (%)
## dbl (10): 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 %>%
filter(Agency %in% c("Belmont", "Mount Holly", "Reidsville", "Wendell"))
cc_anim<- cc %>%
plot_ly(., x = ~Arrest, y = ~`Crime Rate`, hoverinfo = "text",
text = ~ paste("Agency:", Agency,
"<br>Year:", Year,
"<br>Arrests:", Arrest,
"<br>Crime Rate:", `Crime Rate`,
"<br>Sworn Rate Per-Capita:", `Sworn Rate Per-Capita`,
"<br>Workload Ratio:", `Workload Ratio`,
"<br>Clearance Rate:", `Case Clearance (%)` )) %>%
add_markers(frame = ~ Year, ids = ~ Agency, showlegend = TRUE, size = ~`Workload Ratio`, color = ~Agency) %>%
layout( title = "Arrests vs Crime Rate by Agency", xaxis = list(title = "Total Arrests"),
yaxis = list(title = "Crime Rate (per 100,000 )"), legend = list(title = list(text = "Agency")))
cc_anim %>%
animation_opts(frame = 1500, transition = 500, easing = "elastic") %>%
animation_slider(font = list(color = "black", size = 15))
## Warning: Ignoring 1 observations
My animated scatter plot illustrates the shift in total arrests and crime rates from 2006 to 2024 for Belmont, Mount Holly, Reidsville, and Wendell. As before, the size of the data point reflects the officer’s workload for that given year. One of the trends that seemed more noticeable with the animation is that Reidsville appears to have the most significant point and the highest crime rate year after year. Additionally, the animation provided a better understanding of how similar Belmont and Mount Holly were throughout the years. Since I had already completed the scatter plot, I found it easy to animate and did not encounter any issues.