The topic for this project I chose today was about the NYPD Police arrest in 2013. I chose it because I wanted to use the data to explore or determine the usual suspects that are likely to get arrested. This dataset came from The NYPD website. The following variables that are used are Age, City, Weight, Timestop and Race.
library(tidyverse)
── Attaching core tidyverse packages ──────────────────────── tidyverse 2.0.0 ──
✔ dplyr 1.2.1 ✔ readr 2.2.0
✔ forcats 1.0.1 ✔ stringr 1.6.0
✔ ggplot2 4.0.3 ✔ tibble 3.3.1
✔ lubridate 1.9.5 ✔ tidyr 1.3.2
✔ purrr 1.2.2
── 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
Attaching package: 'scales'
The following object is masked from 'package:purrr':
discard
The following object is masked from 'package:readr':
col_factor
library(plotly)
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
# A tibble: 6 × 106
year pct ser_num datestop timestop recstat inout trhsloc perobs crimsusp
<dbl> <dbl> <dbl> <dbl> <dbl> <chr> <chr> <chr> <dbl> <chr>
1 2013 1 2 1012013 452 1 I T 6 GRAND LARC…
2 2013 1 3 1012013 315 1 I T 15 GRAND LARC…
3 2013 1 7 1012013 300 1 I T 10 ROBBERY
4 2013 1 11 1012013 1212 A O P 1 ROBBERY
5 2013 1 12 1012013 2300 A O P 5 BURGLARY
6 2013 5 1 1012013 115 1 O P 1 ROBBERY
# ℹ 96 more variables: perstop <chr>, typeofid <chr>, explnstp <chr>,
# othpers <chr>, arstmade <chr>, arstoffn <chr>, sumissue <chr>,
# sumoffen <chr>, compyear <dbl>, comppct <dbl>, offunif <chr>,
# officrid <chr>, frisked <chr>, searched <chr>, contrabn <chr>,
# adtlrept <chr>, pistol <chr>, riflshot <chr>, asltweap <chr>,
# knifcuti <chr>, machgun <chr>, othrweap <chr>, pf_hands <chr>,
# pf_wall <chr>, pf_grnd <chr>, pf_drwep <chr>, pf_ptwep <chr>, …
Importing and Cleaning the Dataset. This chunk loads the required R packages, imports the 2013 police stop dataset and cleans the missing values and corrects city names.
Call:
lm(formula = weight ~ age + stop_hour, data = regression_data)
Residuals:
Min 1Q Median 3Q Max
-187.67 -17.17 -2.35 14.53 334.49
Coefficients:
Estimate Std. Error t value Pr(>|t|)
(Intercept) 152.113205 0.218190 697.160 < 2e-16 ***
age 0.628120 0.005692 110.353 < 2e-16 ***
stop_hour -0.036216 0.008802 -4.115 3.88e-05 ***
---
Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
Residual standard error: 29.18 on 191224 degrees of freedom
Multiple R-squared: 0.06025, Adjusted R-squared: 0.06024
F-statistic: 6130 on 2 and 191224 DF, p-value: < 2.2e-16
I prepared age, weight, and time-of-stop variables. I created a regression model predicting weight using age and stop time. This Displays a scatterplot and summarizes the regression results.
Warning in geom2trace.default(dots[[1L]][[8L]], dots[[2L]][[1L]], dots[[3L]][[1L]]): geom_GeomTreemap() has yet to be implemented in plotly.
If you'd like to see this geom implemented,
Please open an issue with your example code at
https://github.com/ropensci/plotly/issues
Warning in geom2trace.default(dots[[1L]][[8L]], dots[[2L]][[1L]], dots[[3L]][[1L]]): geom_GeomTreemap() has yet to be implemented in plotly.
If you'd like to see this geom implemented,
Please open an issue with your example code at
https://github.com/ropensci/plotly/issues
Warning in geom2trace.default(dots[[1L]][[8L]], dots[[2L]][[1L]], dots[[3L]][[1L]]): geom_GeomTreemap() has yet to be implemented in plotly.
If you'd like to see this geom implemented,
Please open an issue with your example code at
https://github.com/ropensci/plotly/issues
Warning in geom2trace.default(dots[[1L]][[8L]], dots[[2L]][[1L]], dots[[3L]][[1L]]): geom_GeomTreemap() has yet to be implemented in plotly.
If you'd like to see this geom implemented,
Please open an issue with your example code at
https://github.com/ropensci/plotly/issues
Warning in geom2trace.default(dots[[1L]][[8L]], dots[[2L]][[1L]], dots[[3L]][[1L]]): geom_GeomTreemap() has yet to be implemented in plotly.
If you'd like to see this geom implemented,
Please open an issue with your example code at
https://github.com/ropensci/plotly/issues
Warning in geom2trace.default(dots[[1L]][[8L]], dots[[2L]][[1L]], dots[[3L]][[1L]]): geom_GeomTreemap() has yet to be implemented in plotly.
If you'd like to see this geom implemented,
Please open an issue with your example code at
https://github.com/ropensci/plotly/issues
Warning in geom2trace.default(dots[[1L]][[8L]], dots[[2L]][[1L]], dots[[3L]][[1L]]): geom_GeomTreemap() has yet to be implemented in plotly.
If you'd like to see this geom implemented,
Please open an issue with your example code at
https://github.com/ropensci/plotly/issues
Warning in geom2trace.default(dots[[1L]][[8L]], dots[[2L]][[1L]], dots[[3L]][[1L]]): geom_GeomTreemap() has yet to be implemented in plotly.
If you'd like to see this geom implemented,
Please open an issue with your example code at
https://github.com/ropensci/plotly/issues
Warning in geom2trace.default(dots[[1L]][[8L]], dots[[2L]][[1L]], dots[[3L]][[1L]]): geom_GeomTreemapText() has yet to be implemented in plotly.
If you'd like to see this geom implemented,
Please open an issue with your example code at
https://github.com/ropensci/plotly/issues
Warning in geom2trace.default(dots[[1L]][[8L]], dots[[2L]][[1L]], dots[[3L]][[1L]]): geom_GeomTreemapText() has yet to be implemented in plotly.
If you'd like to see this geom implemented,
Please open an issue with your example code at
https://github.com/ropensci/plotly/issues
Warning in geom2trace.default(dots[[1L]][[8L]], dots[[2L]][[1L]], dots[[3L]][[1L]]): geom_GeomTreemapText() has yet to be implemented in plotly.
If you'd like to see this geom implemented,
Please open an issue with your example code at
https://github.com/ropensci/plotly/issues
Warning in geom2trace.default(dots[[1L]][[8L]], dots[[2L]][[1L]], dots[[3L]][[1L]]): geom_GeomTreemapText() has yet to be implemented in plotly.
If you'd like to see this geom implemented,
Please open an issue with your example code at
https://github.com/ropensci/plotly/issues
Warning in geom2trace.default(dots[[1L]][[8L]], dots[[2L]][[1L]], dots[[3L]][[1L]]): geom_GeomTreemapText() has yet to be implemented in plotly.
If you'd like to see this geom implemented,
Please open an issue with your example code at
https://github.com/ropensci/plotly/issues
Warning in geom2trace.default(dots[[1L]][[8L]], dots[[2L]][[1L]], dots[[3L]][[1L]]): geom_GeomTreemapText() has yet to be implemented in plotly.
If you'd like to see this geom implemented,
Please open an issue with your example code at
https://github.com/ropensci/plotly/issues
Warning in geom2trace.default(dots[[1L]][[8L]], dots[[2L]][[1L]], dots[[3L]][[1L]]): geom_GeomTreemapText() has yet to be implemented in plotly.
If you'd like to see this geom implemented,
Please open an issue with your example code at
https://github.com/ropensci/plotly/issues
Warning in geom2trace.default(dots[[1L]][[8L]], dots[[2L]][[1L]], dots[[3L]][[1L]]): geom_GeomTreemapText() has yet to be implemented in plotly.
If you'd like to see this geom implemented,
Please open an issue with your example code at
https://github.com/ropensci/plotly/issues
This counts police stop records for each race category. I created a colored treemap showing the distribution by race. I added interactive information using Plotly.
library(tidyverse)library(plotly)library(scales)time_data <- police_arrests |>mutate(timestop =as.numeric(timestop),hour =floor(timestop /100),time_period =case_when( hour >=5& hour <12~"Morning", hour >=12& hour <17~"Afternoon",TRUE~"Evening/Night" ) ) |>filter(!is.na(timestop),between(hour, 0, 23), timestop %%100<60 ) |>count(hour, time_period, name ="total") |>mutate(hover_text =paste0("Hour: ", sprintf("%02d:00", hour),"<br>Period: ", time_period,"<br>Police Stops: ", comma(total) ) )p2 <-ggplot( time_data,aes(x = hour,y = total,color = time_period,group =1,text = hover_text )) +geom_line(linewidth =1.2) +geom_point(size =2.5) +scale_color_manual(values =c("Morning"="orange","Afternoon"="blue","Evening/Night"="purple" ),name ="Time of Day" ) +scale_x_continuous(breaks =seq(0, 23, 2) ) +labs(title ="Police Stops by Hour of Day in 2013",subtitle ="Number of recorded police stops across 24 hours",x ="Hour of Day (24-Hour Clock)",y ="Number of Police Stops",caption ="Source: Police_Arrests_2013.csv" ) +theme_minimal()p2
ggplotly(p2, tooltip ="text")
I converted police stop times into hourly categories. I grouped stops into morning, afternoon, and evening/night. I created an interactive line graph showing stops throughout the day.