library(sf)     
## Linking to GEOS 3.11.0, GDAL 3.5.3, PROJ 9.1.0; sf_use_s2() is TRUE
library(dplyr)   
## 
## Attaching package: 'dplyr'
## The following objects are masked from 'package:stats':
## 
##     filter, lag
## The following objects are masked from 'package:base':
## 
##     intersect, setdiff, setequal, union
library(spData) 
## To access larger datasets in this package, install the spDataLarge
## package with: `install.packages('spDataLarge',
## repos='https://nowosad.github.io/drat/', type='source')`
library(ggplot2)
library(ggthemes)
library(spdep)
library(spatialreg)
## Loading required package: Matrix
## 
## Attaching package: 'spatialreg'
## The following objects are masked from 'package:spdep':
## 
##     get.ClusterOption, get.coresOption, get.mcOption,
##     get.VerboseOption, get.ZeroPolicyOption, set.ClusterOption,
##     set.coresOption, set.mcOption, set.VerboseOption,
##     set.ZeroPolicyOption
library(GWmodel)
## Loading required package: robustbase
## Loading required package: sp
## Loading required package: Rcpp
## Welcome to GWmodel version 2.3-2.
library(tidyr)
## 
## Attaching package: 'tidyr'
## The following objects are masked from 'package:Matrix':
## 
##     expand, pack, unpack
#load some potential data sets to use
setwd("/Users/clarkeddie272/Documents/DIDA 370")
nypd_hate <- read.csv("NYPD_Hate_Crimes.csv")
county_data <- read.csv("county_data.csv")

setwd("/Users/clarkeddie272/Documents/DIDA 370/nyc_neighborhoods")
borough_data <- st_read("NYC_Nhood ACS2008_12.shp")
## Reading layer `NYC_Nhood ACS2008_12' from data source 
##   `/Users/clarkeddie272/Documents/DIDA 370/nyc_neighborhoods/NYC_Nhood ACS2008_12.shp' 
##   using driver `ESRI Shapefile'
## Simple feature collection with 195 features and 98 fields
## Geometry type: MULTIPOLYGON
## Dimension:     XY
## Bounding box:  xmin: -74.25559 ymin: 40.49612 xmax: -73.70001 ymax: 40.91553
## Geodetic CRS:  WGS 84
#filter data 
county_data <- county_data %>% filter(stateabbrv == "NY")

#new data set - try to left join with a shape file
setwd("/Users/clarkeddie272/Documents/DIDA 370")
new_data <- read.csv("medi_inc_project.csv")
#try to make a leaflet showing unemployment and poverty
library(leaflet)
library(tidyterra)
## 
## Attaching package: 'tidyterra'
## The following object is masked from 'package:stats':
## 
##     filter
library(terra)
## terra 1.7.71
## 
## Attaching package: 'terra'
## The following object is masked from 'package:tidyr':
## 
##     extract
e_legend <- paste0("<strong>population unemployed: </strong>", borough_data$popunemplo)
e_legend2 <- paste0("<strong>population beneath poverty line: </strong>", borough_data$poor)

pal_use <- colorNumeric("Blues", NULL , n = 4)
pal_use2 <- colorNumeric("Reds", NULL , n = 4)

leaflet() %>%
  leaflet::addPolygons(data = borough_data, 
                       stroke = FALSE, 
                       fillColor = ~pal_use(popunemplo), 
                       fillOpacity = 1, 
                       smoothFactor = 0.25, 
                       popup = e_legend,
                       group = "unemploy") %>%   
  leaflet::addPolygons(data = borough_data, 
                       stroke = FALSE, 
                       fillColor = ~pal_use2(poor), 
                       fillOpacity = 1, 
                       smoothFactor = 0.25, 
                       popup = e_legend2,
                       group = "poverty") %>% 
  addLegend("bottomright",  
            pal=pal_use,    
            values=borough_data$popunemplo,
            title = 'population unemployed',
            group = "unemploy") %>% 
    addLegend("bottomleft",  
            pal=pal_use2,    
            values=borough_data$poor, 
            title = 'population beneath poverty line',
            group = "poverty") %>% 
  addTiles() %>% 
  addProviderTiles(providers$CartoDB.Positron) %>% 
  addLayersControl(overlayGroups = c("unemploy", "poverty"),
                   options = layersControlOptions(collapsed = FALSE))
#try to make a bar chart of crime in counties
#first count function for # of hate crimes
nypd_hate_crimes <- nypd_hate %>% 
  group_by(County) %>% count() 

# re-name the counties to be familiar
nypd_hate_crimes$County[5] <- "STATEN ISLAND"
nypd_hate_crimes$County[2] <- "BROOKLYN"
nypd_hate_crimes$County[3] <- "MANHATTAN"

colnames(nypd_hate_crimes) <- c("Borough", "Total.Hate.Crimes")

new_york <- nypd_hate_crimes %>% select(Borough, Total.Hate.Crimes) 
  
new_york2 <- county_data %>% select(county_name, county_pop2000)
new_york2 <- new_york2 %>% filter(county_name %in% c("New York", "Kings", "Richmond", "Bronx", "Queens")) 

new_york2$county_name[5] <- "STATEN ISLAND"
new_york2$county_name[2] <- "BROOKLYN"
new_york2$county_name[3] <- "MANHATTAN"
new_york2$county_name[1] <- "BRONX"
new_york2$county_name[4] <- "QUEENS"

new_york2$county_name == nypd_hate_crimes$Borough
## [1] TRUE TRUE TRUE TRUE TRUE
newyork3 <- nypd_hate_crimes %>% left_join(new_york2, by = c("Borough" = "county_name"))

newyork3 <- newyork3 %>% mutate(percent_hate = Total.Hate.Crimes/county_pop2000)

  ggplot(data = nypd_hate_crimes, 
       aes(x = Borough, y = Total.Hate.Crimes, group = Total.Hate.Crimes, fill = Borough)) +
  geom_bar(stat = "Identity") +
  labs(x = "Borough", y = "Total Hate Crimes", 
       title = "Hate Crimes by Borough") +
  theme(axis.title.x=element_blank(),axis.text.x=element_blank()) +
  theme(plot.title=element_text(hjust=0.5)) 

    ggplot(data = newyork3, 
       aes(x = Borough, y = percent_hate, group = percent_hate, fill = Borough)) +
  geom_bar(stat = "Identity") +
  labs(x = "Borough", y = "percent hate crimes", 
       title = "Percent Hate Crimes by Borough") +
  theme(axis.title.x=element_blank(),axis.text.x=element_blank()) +
  theme(plot.title=element_text(hjust=0.5))