Introduction

NYC is so large and has so many neighborhoods, it’s hard to fit them cleanly on a single map. With a city so large, policing it and keeping order is a challange.

### NYPD Complaint Data Historical Dive

NYC Open Data has tons of live and historical data for anyone to use. One of the datasets includes all valid felony, misdemeanor and violations reported to the NYPD from 2006 to 2019. This paper will focus on a small amount of the data and show a visualization of the top offences both felony and misdemeanor. Shiny apps also accompany the paper giving pinpoints on a map for more granular detail.

In this paper, we will see the top 5 offences per Boro (felony and misdemeanor) and in which neighborhood they most occur in.

Understanding the Data

The database is enormous with 6.98 million rows and 35 columns. If completely downloaded it takes up over 10 GB in CSV form. I have focused this paper on the last year reported, 2019. They give a better understanding of where we are right now. The data set includes all police forces, such as N.Y. Housing Police, N.Y. Transit Police, etc… I have only used complaints made to NYPD itself, and removed incomplete cases.

With the nature of this project, inferring the data by injecting another form of data extending process did not seem appropriate. The only incident where this method was deviated was in the murder category as the data did not have specific values and full generic values were used.

Each complaint has at least the following columns:

Variables Description
CMPLNT_NUM Randomly generated persistent ID for each complaint
CMPLNT_FR_DT Exact date of occurrence for the reported event (or starting date of occurrence, if CMPLNT_TO_DT exists)
CMPLNT_FR_TM Exact time of occurrence for the reported event (or starting time of occurrence, if CMPLNT_TO_TM exists)
KY_CD Three digit offense classification code
OFNS_DESC Description of offense corresponding with key code
PD_CD Three digit internal classification code (more granular than Key Code)
PD_DESC Description of internal classification corresponding with PD code (more granular than Offense Description)
LAW_CAT_CD Level of offense: felony, misdemeanor, violation
BORO_NM The name of the borough in which the incident occurred
Latitude Midblock Latitude coordinate for Global Coordinate System, WGS 1984, decimal degrees (EPSG 4326)
Longitude Midblock Longitude coordinate for Global Coordinate System, WGS 1984, decimal degrees (EPSG 4326)
Lat_Lon Geospatial Location Point (latitude and Longitude combined)

General Understanding

As stated previously, the data is huge. Even after filtering down to the level we have so far, we can quickly lose scope. We break the data in half on ‘LAW_CAT_CD’ so we can clearly see where the hot spots are in each category.

As we move though the analysis, these categories are our ones to watch.

  • Grand Larceny
  • Felony Assault
  • Robbery
  • Penal Law
  • Burglary
  • Criminal Mischief

As in the Felonies, these are the ones to watch as we move forward.

  • Petit Larceny
  • Assault 3
  • Criminal Mischief
  • Offense against Public Sensability
  • Vehicle and Traffic Laws

Breakdown by Boro

As we start to break down the data by Boro, we start with a broad overview of all the offences by Boro. The NYC boro was created during data processing to account for ‘Murder & Non-negl. Manslaughter’ as the boro column was empty. The data did have the Lat/Long of the offense so the individual incidents will come back in their proper places.

The data was seperated by Boro and I calculated a count of the top five offences. Each section below has the same graphs filtered to the individual Boro.

Manhattan

Bronx

Brooklyn

Staten Island

Shiny Applications:

https://kelloggjohnd.shinyapps.io/Data_608_Final_NYPD_Felony/ https://kelloggjohnd.shinyapps.io/Data_608_Final_NYPD_Misdemeanor/

Code Appendix

library(shiny)
library(ggplot2)
library(ggmap)
library(gganimate)
library(tidyverse)
library(RSocrata)
library(viridis)
library(hrbrthemes)
library(zipcodeR)
library(plotly)
library(lubridate)
library(data.table)
library(rgeos)
library(maptools)
##df <- read.socrata(
#  "https://data.cityofnewyork.us/resource/qgea-i56i.json",
#  app_token = "4c0Xn0ScoEFzZnMaDwHXobtve",
#  email     = "kelloggjohnd@gmail.com",
#  password  = "******"
#)

nypd_data_load <- read_csv("~/CSPS work/608 - Visual Analytics/final/data/NYPD_Complaint_Data_Historic.csv", 
    col_types = cols(Latitude = col_number(), 
        Longitude = col_number()))
#View(NYPD_Complaint_Data)

colnames(nypd_data_load)[1] <- "CMPLNT_NUM"
data_working <- nypd_data_load%>%
  filter(JURIS_DESC == "N.Y. POLICE DEPT")%>%
  select(CMPLNT_NUM, CMPLNT_FR_DT, CMPLNT_FR_TM, ADDR_PCT_CD, JURISDICTION_CODE, JURIS_DESC, KY_CD,OFNS_DESC,PD_CD,PD_DESC,LAW_CAT_CD, Latitude,Longitude,BORO_NM,PATROL_BORO,VIC_AGE_GROUP,VIC_RACE,VIC_SEX)
nyc_shapefile_man@data$id <- as.character(nyc_shapefile_man@data$Name)
nyc.points <- fortify(gBuffer(nyc_shapefile_man, byid = TRUE, width = 0), region = "Name")
nyc.df <- inner_join(nyc.points,nyc_shapefile_man@data, by = "id")
nyc.cent <- nyc.df %>%
  group_by(id)%>%
  summarise(long = median(long), lat = median(lat))
data_coords <- transmute(data_working,
                long = ifelse(is.na(Longitude), 0, Longitude),
                lat = ifelse(is.na(Latitude), 0, Latitude)
                        )

coordinates(data_coords) <- c('long', 'lat')

nhoods <- over(data_coords, nyc_shapefile_man)

#names(nhoods) <- paste('location', toupper(names(nhoods)),sep = "_")

data_working <- cbind(data_working, nhoods)

names(data_working)[names(data_working) == "Name"] <- "NEIGHBORHOOD"
rm(nhoods)
rm(data_coords)
rm(nyc_shapefile_man)
rm(nyc_shapefile)

rm(nyc.points)
data_working <- data_working %>%
  mutate(BORO_NM = ifelse(grepl("QUEENS",data_working$PATROL_BORO),"QUEENS",data_working$BORO_NM))

data_working <- data_working%>%
  mutate(BORO_NM = ifelse(grepl("BKLYN",data_working$PATROL_BORO),"BROOKLYN",data_working$BORO_NM))

data_working <- data_working%>%
  mutate(BORO_NM = ifelse(grepl("MAN",data_working$PATROL_BORO),"MANHATTAN",data_working$BORO_NM))

data_working <- data_working%>%
  mutate(BORO_NM = ifelse(grepl("BRONX",data_working$PATROL_BORO),"BRONX",data_working$BORO_NM))

data_working <- data_working%>%
  mutate(BORO_NM = ifelse(grepl("STATEN ISLAND",data_working$PATROL_BORO),"STATEN",data_working$BORO_NM))

# adjusting data as Murder does not have complete data.  
data_working <- data_working %>%
  mutate(PD_CD = if_else(OFNS_DESC =="MURDER & NON-NEGL. MANSLAUGHTER",999,data_working$PD_CD))%>%
  mutate(PD_DESC = if_else(OFNS_DESC =="MURDER & NON-NEGL. MANSLAUGHTER","MURDER & NON-NEGL. MANSLAUGHTER",data_working$PD_DESC))%>%
  mutate(JURISDICTION_CODE = if_else(OFNS_DESC =="MURDER & NON-NEGL. MANSLAUGHTER",0,data_working$JURISDICTION_CODE))%>%
  mutate(BORO_NM = if_else(OFNS_DESC =="MURDER & NON-NEGL. MANSLAUGHTER","NYC",data_working$BORO_NM))%>%
  mutate(PATROL_BORO = if_else(OFNS_DESC =="MURDER & NON-NEGL. MANSLAUGHTER","NYC",data_working$PATROL_BORO))

felony <- data_working %>% filter(LAW_CAT_CD == "FELONY")%>%drop_na()%>%complete()
misdemeanor <- data_working %>% filter(LAW_CAT_CD == "MISDEMEANOR")%>%drop_na()%>%complete()
library(ggrepel)

ggplot(nyc.df, aes(long, lat, fill = id)) + 
  geom_polygon() +
  geom_path(color = "white") +
  coord_equal() +
  theme(legend.position = "none") +
  geom_text_repel(aes(label = id), data = nyc.cent, size = 3)
felony_count <- felony %>% 
  group_by(OFNS_DESC)%>%
  count(OFNS_DESC)
  


fig2 <- felony_count %>%
  ggplot(aes(x = reorder(OFNS_DESC, -n), y = n))+
  geom_bar(stat = "summary")+
  #geom_text(aes(label=n, angle=45, nudge_y = 1.5), color="blue")+
  ggtitle("Felony Categories") +
  #theme(legend.position="none")+
  theme_classic(base_size = 25)+
  theme(axis.text.x = element_text(angle = 60, hjust = 1, size = 8, vjust = .5),
        axis.text.y = element_text(size = 8))+
  ylab("")+
  xlab("")

fig2_fel <- ggplotly(fig2)
fig2_fel
fig3 <- ggplot(felony, aes(fill=OFNS_DESC, x=OFNS_DESC)) + 
    geom_bar(position="dodge", stat="count") +
    scale_fill_viridis(discrete = T, option = "E") +
    ggtitle("Felony Study by Boro") +
    facet_wrap(~BORO_NM) +
    theme_minimal()+
    theme(legend.position="none") +
    theme(axis.text.x = element_text(angle = 90, hjust = 1, size = 7, vjust = .5))+
    xlab("")

fig3_fel <- ggplotly(fig3)
fig3_fel
fig3 <- ggplot(misdemeanor, aes(fill=OFNS_DESC, x=OFNS_DESC)) + 
    geom_bar(position="dodge", stat="count") +
    scale_fill_viridis(discrete = T, option = "E") +
    ggtitle("Misdemeanor Study by Boro") +
    facet_wrap(~BORO_NM) +
    theme_minimal()+
    theme(legend.position="none") +
    theme(axis.text.x = element_text(angle = 90, hjust = 1, size = 7, vjust = .5))+
    xlab("")

fig3_mis <- ggplotly(fig3)
fig3_mis
manhattan_top_fel <- data_working%>%
  filter(BORO_NM == "MANHATTAN" & LAW_CAT_CD == "FELONY")%>%
  select(OFNS_DESC)%>%
  count(OFNS_DESC)%>%
  arrange(desc(n))%>%
  top_n(5)


manhattan_top_mis <- data_working%>%
  filter(BORO_NM == "MANHATTAN" & LAW_CAT_CD == "MISDEMEANOR")%>%
  select(OFNS_DESC)%>%
  count(OFNS_DESC)%>%
  arrange(desc(n))%>%
  top_n(5)

manhattan_top_fel <- as.vector(manhattan_top_fel$OFNS_DESC)
manhattan_top_mis <- as.vector(manhattan_top_mis$OFNS_DESC)

felony <- data_working%>%
  filter(BORO_NM == "MANHATTAN" & LAW_CAT_CD == "FELONY")%>%
  filter(OFNS_DESC %in% manhattan_top_fel)%>%
  group_by(CMPLNT_FR_DT, LAW_CAT_CD)%>%
  count(OFNS_DESC)

felony$CMPLNT_FR_DT = mdy(felony$CMPLNT_FR_DT)

misdemeanor <- data_working%>%
  filter(BORO_NM == "MANHATTAN" & LAW_CAT_CD == "MISDEMEANOR")%>%
  filter(OFNS_DESC %in% manhattan_top_mis)%>%
  group_by(CMPLNT_FR_DT, LAW_CAT_CD)%>%
  count(OFNS_DESC)

misdemeanor$CMPLNT_FR_DT = mdy(misdemeanor$CMPLNT_FR_DT)


felony <- felony %>%drop_na()%>%complete()
misdemeanor <- misdemeanor %>%drop_na()%>%complete()

fig4_fel <- felony %>%
  ggplot(aes(x=CMPLNT_FR_DT, y=n, group=OFNS_DESC, color=OFNS_DESC))+
  geom_point(cex=1) +
  geom_smooth(method = lm,
              se=FALSE,
              fullrange = TRUE)+
  ggtitle("Top Felonies in Manhattan") +
  #scale_color_viridis(discrete = TRUE) +
  ylab("High Offence count") +
  xlab("")+
  theme(panel.grid.major = element_blank(), panel.grid.minor = element_blank(),
panel.background = element_blank(), axis.line = element_line(colour = "black"))

fig4_mis <- misdemeanor %>%
  ggplot(aes(x=CMPLNT_FR_DT, y=n, group=OFNS_DESC, color=OFNS_DESC))+
  geom_point(cex=1) +
  geom_smooth(method = lm,
              se=FALSE,
              fullrange = TRUE)+
    ggtitle("Top 5 Misdemanors in Manhattan") +
  #scale_color_viridis(discrete = TRUE) +
  ylab("High Offence count") +
  xlab("")+
  theme(panel.grid.major = element_blank(), panel.grid.minor = element_blank(),
panel.background = element_blank(), axis.line = element_line(colour = "black"))


fig4_fel<- ggplotly(fig4_fel)
fig4_mis<- ggplotly(fig4_mis)
fig4_fel
fig4_mis
felony <- data_working%>%
  filter(BORO_NM == "MANHATTAN")%>%
  filter(LAW_CAT_CD == "FELONY")%>%
  filter(OFNS_DESC %in% manhattan_top_fel)%>%
  group_by(CMPLNT_FR_DT, BORO_NM, NEIGHBORHOOD, LAW_CAT_CD)%>%
  count(OFNS_DESC)

felony$CMPLNT_FR_DT = mdy(felony$CMPLNT_FR_DT)
felony$off_month <- lubridate::month(ymd(felony$CMPLNT_FR_DT), label = TRUE, abbr = FALSE)

misdemeanor <- data_working%>%
  filter(BORO_NM == "MANHATTAN")%>%
  filter(LAW_CAT_CD == "MISDEMEANOR")%>%
  filter(OFNS_DESC %in% manhattan_top_mis)%>%
  group_by(CMPLNT_FR_DT, BORO_NM, NEIGHBORHOOD, LAW_CAT_CD)%>%
  count(OFNS_DESC)

misdemeanor$CMPLNT_FR_DT = mdy(misdemeanor$CMPLNT_FR_DT)
misdemeanor$off_month <- lubridate::month(ymd(misdemeanor$CMPLNT_FR_DT), label = TRUE, abbr = FALSE)


fig4.1_fel <- ggplot(felony, aes(fill=OFNS_DESC, y=n, x=NEIGHBORHOOD))+
  geom_bar(position = "dodge", stat = "identity")+
  ylab("")+
  theme(
    axis.text.x = element_text(size = 9, angle = 90)
    )+
  ggtitle("Top Felonies in Manhattan")+
  theme(panel.grid.major = element_blank(), panel.grid.minor = element_blank(),
  panel.background = element_blank(), axis.line = element_line(colour = "black"))

fig4.1_mis <- ggplot(misdemeanor, aes(fill=OFNS_DESC, y=n, x=NEIGHBORHOOD))+
  geom_bar(position = "dodge", stat = "identity")+
  theme(
    axis.text.x = element_text(size = 9, angle = 90)
    )+
  ggtitle("Top Misdemeanors in Manhattan") +
  ylab("")+
  theme(panel.grid.major = element_blank(), panel.grid.minor = element_blank(),
  panel.background = element_blank(), axis.line = element_line(colour = "black"))

fig4.1_fel <- ggplotly(fig4.1_fel)
fig4.1_mis <- ggplotly(fig4.1_mis)
fig4.1_fel
fig4.1_mis
bronx_top_fel <- data_working%>%
  filter(BORO_NM == "BRONX" & LAW_CAT_CD == "FELONY")%>%
  select(OFNS_DESC)%>%
  count(OFNS_DESC)%>%
  arrange(desc(n))%>%
  top_n(5)


bronx_top_mis <- data_working%>%
  filter(BORO_NM == "BRONX" & LAW_CAT_CD == "MISDEMEANOR")%>%
  select(OFNS_DESC)%>%
  count(OFNS_DESC)%>%
  arrange(desc(n))%>%
  top_n(5)

bronx_top_fel <- as.vector(bronx_top_fel$OFNS_DESC)
bronx_top_mis <- as.vector(bronx_top_mis$OFNS_DESC)

felony <- data_working%>%
  filter(BORO_NM == "BRONX" & LAW_CAT_CD == "FELONY")%>%
  filter(OFNS_DESC %in% bronx_top_fel)%>%
  group_by(CMPLNT_FR_DT, LAW_CAT_CD)%>%
  count(OFNS_DESC)

felony$CMPLNT_FR_DT = mdy(felony$CMPLNT_FR_DT)

misdemeanor <- data_working%>%
  filter(BORO_NM == "BRONX" & LAW_CAT_CD == "MISDEMEANOR")%>%
  filter(OFNS_DESC %in% bronx_top_mis)%>%
  group_by(CMPLNT_FR_DT, LAW_CAT_CD)%>%
  count(OFNS_DESC)

misdemeanor$CMPLNT_FR_DT = mdy(misdemeanor$CMPLNT_FR_DT)


felony <- felony %>%drop_na()%>%complete()
misdemeanor <- misdemeanor %>%drop_na()%>%complete()

fig5_fel <- felony %>%
  ggplot(aes(x=CMPLNT_FR_DT, y=n, group=OFNS_DESC, color=OFNS_DESC))+
  geom_point(cex=1) +
  geom_smooth(method = lm,
              se=FALSE,
              fullrange = TRUE)+
  ggtitle("Top Felonies in Bronx") +
  #scale_color_viridis(discrete = TRUE) +
  ylab("High Offence count") +
  xlab("")+
  theme(panel.grid.major = element_blank(), panel.grid.minor = element_blank(),
panel.background = element_blank(), axis.line = element_line(colour = "black"))

fig5_mis <- misdemeanor %>%
  ggplot(aes(x=CMPLNT_FR_DT, y=n, group=OFNS_DESC, color=OFNS_DESC))+
  geom_point(cex=1) +
  geom_smooth(method = lm,
              se=FALSE,
              fullrange = TRUE)+
    ggtitle("Top 5 Misdemanors in Bronx") +
  #scale_color_viridis(discrete = TRUE) +
  ylab("High Offence count") +
  xlab("")+
  theme(panel.grid.major = element_blank(), panel.grid.minor = element_blank(),
panel.background = element_blank(), axis.line = element_line(colour = "black"))


fig5_fel<- ggplotly(fig5_fel)
fig5_mis<- ggplotly(fig5_mis)
fig5_fel
fig5_mis
felony <- data_working%>%
  filter(BORO_NM == "BRONX")%>%
  filter(LAW_CAT_CD == "FELONY")%>%
  filter(OFNS_DESC %in% bronx_top_fel)%>%
  group_by(CMPLNT_FR_DT, BORO_NM, NEIGHBORHOOD, LAW_CAT_CD)%>%
  count(OFNS_DESC)

felony$CMPLNT_FR_DT = mdy(felony$CMPLNT_FR_DT)
felony$off_month <- lubridate::month(ymd(felony$CMPLNT_FR_DT), label = TRUE, abbr = FALSE)

misdemeanor <- data_working%>%
  filter(BORO_NM == "BRONX")%>%
  filter(LAW_CAT_CD == "MISDEMEANOR")%>%
  filter(OFNS_DESC %in% bronx_top_mis)%>%
  group_by(CMPLNT_FR_DT, BORO_NM, NEIGHBORHOOD, LAW_CAT_CD)%>%
  count(OFNS_DESC)

misdemeanor$CMPLNT_FR_DT = mdy(misdemeanor$CMPLNT_FR_DT)
misdemeanor$off_month <- lubridate::month(ymd(misdemeanor$CMPLNT_FR_DT), label = TRUE, abbr = FALSE)

fig5.1_fel <- ggplot(felony, aes(fill=OFNS_DESC, y=n, x=NEIGHBORHOOD))+
  geom_bar(position = "dodge", stat = "identity")+
  ylab("")+
  theme(
    axis.text.x = element_text(size = 9, angle = 90)
    )+
  ggtitle("Top Felonies in Bronx")+
  theme(panel.grid.major = element_blank(), panel.grid.minor = element_blank(),
  panel.background = element_blank(), axis.line = element_line(colour = "black"))

fig5.1_mis <- ggplot(misdemeanor, aes(fill=OFNS_DESC, y=n, x=NEIGHBORHOOD))+
  geom_bar(position = "dodge", stat = "identity")+
  theme(
    axis.text.x = element_text(size = 9, angle = 90)
    )+
  ggtitle("Top Misdemeanors in Bronx") +
  ylab("")+
  theme(panel.grid.major = element_blank(), panel.grid.minor = element_blank(),
  panel.background = element_blank(), axis.line = element_line(colour = "black"))

fig5.1_fel  <- ggplotly(fig5.1_fel)
fig5.1_mis <- ggplotly(fig5.1_mis)
fig5.1_fel 
fig5.1_mis
queens_top_fel <- data_working%>%
  filter(BORO_NM == "QUEENS" & LAW_CAT_CD == "FELONY")%>%
  select(OFNS_DESC)%>%
  count(OFNS_DESC)%>%
  arrange(desc(n))%>%
  top_n(5)


queens_top_mis <- data_working%>%
  filter(BORO_NM == "QUEENS" & LAW_CAT_CD == "MISDEMEANOR")%>%
  select(OFNS_DESC)%>%
  count(OFNS_DESC)%>%
  arrange(desc(n))%>%
  top_n(5)

queens_top_fel <- as.vector(queens_top_fel$OFNS_DESC)
queens_top_mis <- as.vector(queens_top_mis$OFNS_DESC)

felony <- data_working%>%
  filter(BORO_NM == "QUEENS" & LAW_CAT_CD == "FELONY")%>%
  filter(OFNS_DESC %in% queens_top_fel)%>%
  group_by(CMPLNT_FR_DT, LAW_CAT_CD)%>%
  count(OFNS_DESC)

felony$CMPLNT_FR_DT = mdy(felony$CMPLNT_FR_DT)

misdemeanor <- data_working%>%
  filter(BORO_NM == "QUEENS" & LAW_CAT_CD == "MISDEMEANOR")%>%
  filter(OFNS_DESC %in% queens_top_mis)%>%
  group_by(CMPLNT_FR_DT, LAW_CAT_CD)%>%
  count(OFNS_DESC)

misdemeanor$CMPLNT_FR_DT = mdy(misdemeanor$CMPLNT_FR_DT)


felony <- felony %>%drop_na()%>%complete()
misdemeanor <- misdemeanor %>%drop_na()%>%complete()

fig6_fel <- felony %>%
  ggplot(aes(x=CMPLNT_FR_DT, y=n, group=OFNS_DESC, color=OFNS_DESC))+
  geom_point(cex=1) +
  geom_smooth(method = lm,
              se=FALSE,
              fullrange = TRUE)+
  ggtitle("Top Felonies in Queens") +
  #scale_color_viridis(discrete = TRUE) +
  ylab("High Offence count") +
  xlab("")+
  theme(panel.grid.major = element_blank(), panel.grid.minor = element_blank(),
panel.background = element_blank(), axis.line = element_line(colour = "black"))

fig6_mis <- misdemeanor %>%
  ggplot(aes(x=CMPLNT_FR_DT, y=n, group=OFNS_DESC, color=OFNS_DESC))+
  geom_point(cex=1) +
  geom_smooth(method = lm,
              se=FALSE,
              fullrange = TRUE)+
    ggtitle("Top 5 Misdemanors in Queens") +
  #scale_color_viridis(discrete = TRUE) +
  ylab("High Offence count") +
  xlab("")+
  theme(panel.grid.major = element_blank(), panel.grid.minor = element_blank(),
panel.background = element_blank(), axis.line = element_line(colour = "black"))


fig6_fel<- ggplotly(fig6_fel)
fig6_mis<- ggplotly(fig6_mis)
fig6_fel
fig6_mis
felony <- data_working%>%
  filter(BORO_NM == "QUEENS")%>%
  filter(LAW_CAT_CD == "FELONY")%>%
  filter(OFNS_DESC %in% queens_top_fel)%>%
  group_by(CMPLNT_FR_DT, BORO_NM, NEIGHBORHOOD, LAW_CAT_CD)%>%
  count(OFNS_DESC)

felony$CMPLNT_FR_DT = mdy(felony$CMPLNT_FR_DT)
felony$off_month <- lubridate::month(ymd(felony$CMPLNT_FR_DT), label = TRUE, abbr = FALSE)

misdemeanor <- data_working%>%
  filter(BORO_NM == "QUEENS")%>%
  filter(LAW_CAT_CD == "MISDEMEANOR")%>%
  filter(OFNS_DESC %in% queens_top_mis)%>%
  group_by(CMPLNT_FR_DT, BORO_NM, NEIGHBORHOOD, LAW_CAT_CD)%>%
  count(OFNS_DESC)

misdemeanor$CMPLNT_FR_DT = mdy(misdemeanor$CMPLNT_FR_DT)
misdemeanor$off_month <- lubridate::month(ymd(misdemeanor$CMPLNT_FR_DT), label = TRUE, abbr = FALSE)

fig6.1_fel <- ggplot(felony, aes(fill=OFNS_DESC, y=n, x=NEIGHBORHOOD))+
  geom_bar(position = "dodge", stat = "identity")+
  ylab("")+
  theme(
    axis.text.x = element_text(size = 9, angle = 90)
    )+
  ggtitle("Top Felonies in Queens")+
  theme(panel.grid.major = element_blank(), panel.grid.minor = element_blank(),
  panel.background = element_blank(), axis.line = element_line(colour = "black"))

fig6.1_mis <- ggplot(misdemeanor, aes(fill=OFNS_DESC, y=n, x=NEIGHBORHOOD))+
  geom_bar(position = "dodge", stat = "identity")+
  theme(
    axis.text.x = element_text(size = 9, angle = 90)
    )+
  ggtitle("Top Misdemeanors in Queens") +
  ylab("")+
  theme(panel.grid.major = element_blank(), panel.grid.minor = element_blank(),
  panel.background = element_blank(), axis.line = element_line(colour = "black"))

fig6.1_fel  <- ggplotly(fig6.1_fel)
fig6.1_mis <- ggplotly(fig6.1_mis)
fig6.1_fel 
fig6.1_mis
brooklyn_top_fel <- data_working%>%
  filter(BORO_NM == "BROOKLYN" & LAW_CAT_CD == "FELONY")%>%
  select(OFNS_DESC)%>%
  count(OFNS_DESC)%>%
  arrange(desc(n))%>%
  top_n(5)


brooklyn_top_mis <- data_working%>%
  filter(BORO_NM == "BROOKLYN" & LAW_CAT_CD == "MISDEMEANOR")%>%
  select(OFNS_DESC)%>%
  count(OFNS_DESC)%>%
  arrange(desc(n))%>%
  top_n(5)

brooklyn_top_fel <- as.vector(brooklyn_top_fel$OFNS_DESC)
brooklyn_top_mis <- as.vector(brooklyn_top_mis$OFNS_DESC)

felony <- data_working%>%
  filter(BORO_NM == "BROOKLYN" & LAW_CAT_CD == "FELONY")%>%
  filter(OFNS_DESC %in% brooklyn_top_fel)%>%
  group_by(CMPLNT_FR_DT, LAW_CAT_CD)%>%
  count(OFNS_DESC)

felony$CMPLNT_FR_DT = mdy(felony$CMPLNT_FR_DT)

misdemeanor <- data_working%>%
  filter(BORO_NM == "BROOKLYN" & LAW_CAT_CD == "MISDEMEANOR")%>%
  filter(OFNS_DESC %in% brooklyn_top_mis)%>%
  group_by(CMPLNT_FR_DT, LAW_CAT_CD)%>%
  count(OFNS_DESC)

misdemeanor$CMPLNT_FR_DT = mdy(misdemeanor$CMPLNT_FR_DT)


felony <- felony %>%drop_na()%>%complete()
misdemeanor <- misdemeanor %>%drop_na()%>%complete()

fig7_fel <- felony %>%
  ggplot(aes(x=CMPLNT_FR_DT, y=n, group=OFNS_DESC, color=OFNS_DESC))+
  geom_point(cex=1) +
  geom_smooth(method = lm,
              se=FALSE,
              fullrange = TRUE)+
  ggtitle("Top Felonies in Brooklyn") +
  #scale_color_viridis(discrete = TRUE) +
  ylab("High Offence count") +
  xlab("")+
  theme(panel.grid.major = element_blank(), panel.grid.minor = element_blank(),
panel.background = element_blank(), axis.line = element_line(colour = "black"))

fig7_mis <- misdemeanor %>%
  ggplot(aes(x=CMPLNT_FR_DT, y=n, group=OFNS_DESC, color=OFNS_DESC))+
  geom_point(cex=1) +
  geom_smooth(method = lm,
              se=FALSE,
              fullrange = TRUE)+
    ggtitle("Top 5 Misdemanors in Brooklyn") +
  #scale_color_viridis(discrete = TRUE) +
  ylab("High Offence count") +
  xlab("")+
  theme(panel.grid.major = element_blank(), panel.grid.minor = element_blank(),
panel.background = element_blank(), axis.line = element_line(colour = "black"))


fig7_fel<- ggplotly(fig7_fel)
fig7_mis<- ggplotly(fig7_mis)
fig7_fel
fig7_mis
felony <- data_working%>%
  filter(BORO_NM == "BROOKLYN")%>%
  filter(LAW_CAT_CD == "FELONY")%>%
  filter(OFNS_DESC %in% brooklyn_top_fel)%>%
  group_by(CMPLNT_FR_DT, BORO_NM, NEIGHBORHOOD, LAW_CAT_CD)%>%
  count(OFNS_DESC)

felony$CMPLNT_FR_DT = mdy(felony$CMPLNT_FR_DT)
felony$off_month <- lubridate::month(ymd(felony$CMPLNT_FR_DT), label = TRUE, abbr = FALSE)

misdemeanor <- data_working%>%
  filter(BORO_NM == "BROOKLYN")%>%
  filter(LAW_CAT_CD == "MISDEMEANOR")%>%
  filter(OFNS_DESC %in% brooklyn_top_mis)%>%
  group_by(CMPLNT_FR_DT, BORO_NM, NEIGHBORHOOD, LAW_CAT_CD)%>%
  count(OFNS_DESC)

misdemeanor$CMPLNT_FR_DT = mdy(misdemeanor$CMPLNT_FR_DT)
misdemeanor$off_month <- lubridate::month(ymd(misdemeanor$CMPLNT_FR_DT), label = TRUE, abbr = FALSE)

fig7.1_fel <- ggplot(felony, aes(fill=OFNS_DESC, y=n, x=NEIGHBORHOOD))+
  geom_bar(position = "dodge", stat = "identity")+
  ylab("")+
  theme(
    axis.text.x = element_text(size = 9, angle = 90)
    )+
  ggtitle("Top Felonies in Brooklyn")+
  theme(panel.grid.major = element_blank(), panel.grid.minor = element_blank(),
  panel.background = element_blank(), axis.line = element_line(colour = "black"))

fig7.1_mis <- ggplot(misdemeanor, aes(fill=OFNS_DESC, y=n, x=NEIGHBORHOOD))+
  geom_bar(position = "dodge", stat = "identity")+
  theme(
    axis.text.x = element_text(size = 9, angle = 90)
    )+
  ggtitle("Top Misdemeanors in Brooklyn") +
  ylab("")+
  theme(panel.grid.major = element_blank(), panel.grid.minor = element_blank(),
  panel.background = element_blank(), axis.line = element_line(colour = "black"))

fig7.1_fel  <- ggplotly(fig7.1_fel)
fig7.1_mis <- ggplotly(fig7.1_mis)
fig7.1_fel 
fig7.1_mis
staten_top_fel <- data_working%>%
  filter(BORO_NM == "STATEN" & LAW_CAT_CD == "FELONY")%>%
  select(OFNS_DESC)%>%
  count(OFNS_DESC)%>%
  arrange(desc(n))%>%
  top_n(5)


staten_top_mis <- data_working%>%
  filter(BORO_NM == "STATEN" & LAW_CAT_CD == "MISDEMEANOR")%>%
  select(OFNS_DESC)%>%
  count(OFNS_DESC)%>%
  arrange(desc(n))%>%
  top_n(5)

staten_top_fel <- as.vector(staten_top_fel$OFNS_DESC)
staten_top_mis <- as.vector(staten_top_mis$OFNS_DESC)

felony <- data_working%>%
  filter(BORO_NM == "STATEN" & LAW_CAT_CD == "FELONY")%>%
  filter(OFNS_DESC %in% staten_top_fel)%>%
  group_by(CMPLNT_FR_DT, LAW_CAT_CD)%>%
  count(OFNS_DESC)

felony$CMPLNT_FR_DT = mdy(felony$CMPLNT_FR_DT)

misdemeanor <- data_working%>%
  filter(BORO_NM == "STATEN" & LAW_CAT_CD == "MISDEMEANOR")%>%
  filter(OFNS_DESC %in% staten_top_mis)%>%
  group_by(CMPLNT_FR_DT, LAW_CAT_CD)%>%
  count(OFNS_DESC)

misdemeanor$CMPLNT_FR_DT = mdy(misdemeanor$CMPLNT_FR_DT)


felony <- felony %>%drop_na()%>%complete()
misdemeanor <- misdemeanor %>%drop_na()%>%complete()

fig8_fel <- felony %>%
  ggplot(aes(x=CMPLNT_FR_DT, y=n, group=OFNS_DESC, color=OFNS_DESC))+
  geom_point(cex=1) +
  geom_smooth(method = lm,
              se=FALSE,
              fullrange = TRUE)+
  ggtitle("Top Felonies in Staten Island") +
  #scale_color_viridis(discrete = TRUE) +
  ylab("High Offence count") +
  xlab("")+
  theme(panel.grid.major = element_blank(), panel.grid.minor = element_blank(),
panel.background = element_blank(), axis.line = element_line(colour = "black"))

fig8_mis <- misdemeanor %>%
  ggplot(aes(x=CMPLNT_FR_DT, y=n, group=OFNS_DESC, color=OFNS_DESC))+
  geom_point(cex=1) +
  geom_smooth(method = lm,
              se=FALSE,
              fullrange = TRUE)+
    ggtitle("Top 5 Misdemanors in Staten Island") +
  #scale_color_viridis(discrete = TRUE) +
  ylab("High Offence count") +
  xlab("")+
  theme(panel.grid.major = element_blank(), panel.grid.minor = element_blank(),
panel.background = element_blank(), axis.line = element_line(colour = "black"))


fig8_fel<- ggplotly(fig8_fel)
fig8_mis<- ggplotly(fig8_mis)
fig8_fel
fig8_mis
felony <- data_working%>%
  filter(BORO_NM == "STATEN")%>%
  filter(LAW_CAT_CD == "FELONY")%>%
  filter(OFNS_DESC %in% staten_top_fel)%>%
  group_by(CMPLNT_FR_DT, BORO_NM, NEIGHBORHOOD, LAW_CAT_CD)%>%
  count(OFNS_DESC)

felony$CMPLNT_FR_DT = mdy(felony$CMPLNT_FR_DT)
felony$off_month <- lubridate::month(ymd(felony$CMPLNT_FR_DT), label = TRUE, abbr = FALSE)

misdemeanor <- data_working%>%
  filter(BORO_NM == "STATEN")%>%
  filter(LAW_CAT_CD == "MISDEMEANOR")%>%
  filter(OFNS_DESC %in% staten_top_mis)%>%
  group_by(CMPLNT_FR_DT, BORO_NM, NEIGHBORHOOD, LAW_CAT_CD)%>%
  count(OFNS_DESC)

misdemeanor$CMPLNT_FR_DT = mdy(misdemeanor$CMPLNT_FR_DT)
misdemeanor$off_month <- lubridate::month(ymd(misdemeanor$CMPLNT_FR_DT), label = TRUE, abbr = FALSE)

fig8.1_fel <- ggplot(felony, aes(fill=OFNS_DESC, y=n, x=NEIGHBORHOOD))+
  geom_bar(position = "dodge", stat = "identity")+
  ylab("")+
  theme(
    axis.text.x = element_text(size = 9, angle = 90)
    )+
  ggtitle("Top Felonies in Staten Island")+
  theme(panel.grid.major = element_blank(), panel.grid.minor = element_blank(),
  panel.background = element_blank(), axis.line = element_line(colour = "black"))

fig8.1_mis <- ggplot(misdemeanor, aes(fill=OFNS_DESC, y=n, x=NEIGHBORHOOD))+
  geom_bar(position = "dodge", stat = "identity")+
  theme(
    axis.text.x = element_text(size = 9, angle = 90)
    )+
  ggtitle("Top Misdemeanors in Staten Island") +
  ylab("")+
  theme(panel.grid.major = element_blank(), panel.grid.minor = element_blank(),
  panel.background = element_blank(), axis.line = element_line(colour = "black"))

fig8.1_fel  <- ggplotly(fig8.1_fel)
fig8.1_mis <- ggplotly(fig8.1_mis)
fig8.1_fel 
fig8.1_mis
#rm(manhattan_top_mis,manhattan_top_fel,manhattan_top, misdemeanor, nyc.points,nyc_shapefile_man,nyc_shapefile,nyc.cent,nyc.df,queens_top_mis, queens_top_fel,queens_top,bronx_top_mis,bronx_top_fel,bronx_top, felony, staten_top_mis, staten_top_fel, top_bronx,top_brooklyn,top_man,top_queens,top_staten,brooklyn_top, staten_top)