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(ggplot2)
## Warning: package 'ggplot2' was built under R version 4.3.3
library(NineteenEightyR)

library(sf)     
## Warning: package 'sf' was built under R version 4.3.2
## Linking to GEOS 3.11.2, GDAL 3.7.2, PROJ 9.3.0; sf_use_s2() is TRUE
library(spData) 
## Warning: package 'spData' was built under R version 4.3.2
## To access larger datasets in this package, install the spDataLarge
## package with: `install.packages('spDataLarge',
## repos='https://nowosad.github.io/drat/', type='source')`
library(urbnmapr)
library(sp)
## Warning: package 'sp' was built under R version 4.3.2
library(ggmap)
## ℹ Google's Terms of Service: <https://mapsplatform.google.com>
##   Stadia Maps' Terms of Service: <https://stadiamaps.com/terms-of-service/>
##   OpenStreetMap's Tile Usage Policy: <https://operations.osmfoundation.org/policies/tiles/>
## ℹ Please cite ggmap if you use it! Use `citation("ggmap")` for details.
library(ggthemes)
## Warning: package 'ggthemes' was built under R version 4.3.2
library(stringr)
## Warning: package 'stringr' was built under R version 4.3.2
police_killings <- read.csv("C://Users//ndl54//OneDrive//Documents//School//DIDA 325//DIDA Project//fatal-police-shootings-data.csv")

police_killings
#plotting age distribution of police shooting victims

ggplot(police_killings, aes(x = age)) +
  geom_histogram(binwidth = 5, fill = "grey", color = "black") +
  labs(title = "Age Distribution of Police Shooting Victims", x = "Age", y = "Count")
## Warning: Removed 568 rows containing non-finite outside the scale range
## (`stat_bin()`).

#plotting race distribution of police shooting victims

ggplot(police_killings, aes(x = race, fill = race)) +
  geom_bar(color = "black") +
  labs(title = "Race Distribution of Police Shooting Victims", x = "Race", y = "Count") +
  theme_minimal() +
  scale_fill_manual(values = c("skyblue", "darkorange", "forestgreen", "firebrick", "purple", "pink", "grey", "lightgrey")) +
  guides(fill = guide_legend(title = "Race of Victim"))

#filtering the data so it is only using data from Massachusetts

mass_data <- police_killings %>%
  filter(state == "MA")

#filtering the data by certain types of weapons

included_values <- c("gun","unarmed","replica","other","knife","blunt_object","vehicle","undetermined","other;gun","unknown","blunt_object;blunt_object","gun;knife","knife;blunt_object","vehicle;gun","gun;vehicle","replica;vehicle","blunt_object;knife","knife;vehicle","vehicle;knife;other","replica;knife","other;blunt_object;knife")

filtered_data <- subset(mass_data, armed_with %in% included_values)

#plotting the count of victims by what they were armed with

ggplot(filtered_data, aes(x = armed_with, fill = armed_with)) +
  geom_bar(color = "black", na.rm = T) +
  labs(title = "", x = "Count of What the Victim was Armed With", y = "Count") +
  theme_minimal() +
  guides(fill = guide_legend(title = "Armed With"))+
  theme(axis.text.x = element_text(angle = 90, hjust = 1))

police_killings$date <- as.Date(police_killings$date, format = "%m/%d/%Y")

# Create a month column
police_killings$month <- factor(format(police_killings$date, "%B"), levels = month.name)

# Calculate the average shootings per month
average_monthly_counts <- police_killings %>%
  group_by(month) %>%
  summarise(average_shootings = n() / length(unique(date)))

# Plot the bar chart with average counts
ggplot(average_monthly_counts, aes(x = month, y = average_shootings)) +
  geom_bar(stat = "identity", fill = "grey", color = "black") +
  labs(title = "Average Monthly Distribution of Police Shootings", x = "Month", y = "Average Count") +
  theme(axis.text.x = element_text(angle = 45, hjust = 1))

police_killings$date <- as.Date(police_killings$date, format = "%Y-%m-%d")

# Extract month and year
police_killings <- police_killings %>%
  mutate(month = format(date, "%m"),
         year = format(date, "%Y"))

# Group by month and year, count the number of shootings
monthly_counts <- police_killings %>%
  group_by(year, month) %>%
  summarise(shootings = n())
## `summarise()` has grouped output by 'year'. You can override using the
## `.groups` argument.
# Create a time series line plot
ggplot(monthly_counts, aes(x = as.Date(paste(year, month, "01", sep = "-")), y = shootings)) +
  geom_line() +
  geom_point() +
  labs(title = "Total Shootings Over Time",
       x = "Date",
       y = "Number of Shootings") +
  scale_x_date(date_labels = "%b %Y", date_breaks = "12 months", expand = c(0,0)) +
  theme_minimal()

county_data <- read.csv("C://Users//ndl54//OneDrive//Documents//School//DIDA 325//DIDA Project//county_data (2).csv")

county_data
shootings_count <- police_killings %>%
  group_by(county, state) %>%
  summarise(shootings = n())
## `summarise()` has grouped output by 'county'. You can override using the
## `.groups` argument.
shootings_count
# Merge with county_data
merged_data <- merge(county_data, shootings_count, by.x = c("county_name", "stateabbrv"), by.y = c("county", "state"), all.x = TRUE)

# Print the resulting dataframe
#print(merged_data)

merged_data_new <-na.omit(merged_data)

merged_data_new
shootings_per_100k <- ((merged_data_new$shootings/merged_data_new$county_pop2000)*100000)



merged_data_final<- cbind(merged_data_new, shootings_per_100k)
merged_data_final
options(scipen = 999, digits = 6)

#print(merged_data_final)

max(merged_data_final$shootings_per_100k)
## [1] 51.4933
merged_data_final[which.max(merged_data_final$shootings_per_100k),]
library(cowplot)
## 
## Attaching package: 'cowplot'
## The following object is masked from 'package:ggthemes':
## 
##     theme_map
## The following object is masked from 'package:ggmap':
## 
##     theme_nothing
a <- ggplot(merged_data_final)+
  geom_point(aes(x = crime_rate, y = shootings_per_100k))+
  geom_smooth(method = "lm", aes(x = crime_rate, y = shootings))+
  theme_minimal()+
  labs(title = "Segregation vs Inequality")

b <- ggplot(merged_data_new)+
  geom_point(aes(x = poverty_rate, y = shootings_per_100k))+
  geom_smooth(method = "lm", aes(x = poverty_rate, y = shootings))+
  theme_minimal()+
  labs(title = "Segregation vs Crime Rates")

c <- ggplot(merged_data_new)+
  geom_point(aes(x = percent_college_degree, y = shootings_per_100k))+
  geom_smooth(method = "lm", aes(x = percent_college_degree, y = shootings))+
  theme_minimal()+
  labs(title = "Segregation vs College Education")

plot_grid(a, b, c, nrow = 1)
## `geom_smooth()` using formula = 'y ~ x'
## `geom_smooth()` using formula = 'y ~ x'
## `geom_smooth()` using formula = 'y ~ x'

#Armed with bar graph 
police_killings_weapon = police_killings %>% select(c(armed_with, body_camera))
police_killings_weapon <- police_killings_weapon %>% 
                              filter(armed_with == c("blunt_object", "knife", "gun", "unarmed"))
## Warning: There was 1 warning in `filter()`.
## ℹ In argument: `armed_with == c("blunt_object", "knife", "gun", "unarmed")`.
## Caused by warning in `armed_with == c("blunt_object", "knife", "gun", "unarmed")`:
## ! longer object length is not a multiple of shorter object length
ggplot(police_killings_weapon, aes(x=armed_with)) +
  geom_bar(fill=sunset1(n=4, alpha = 1), color = "black") +
  labs(title = "Suspect Armed With", x = "Weapon", y = "Count") +
  theme(axis.text.x = element_text(angle = 45, hjust = 1)) +
  theme_minimal()

#body cam bar graph
color_bodycam = c("darkred", "green")
ggplot(police_killings, aes(x=body_camera, fill=body_camera)) +
  geom_bar(color = "black") +
  scale_fill_manual(values = color_bodycam)

  labs(title = "Bodycam Presence During Shootings", x = "Bodycam Presence", y = "Count") +
  theme(axis.text.x = element_text(angle = 45, hjust = 1)) +
  theme_minimal()
## NULL
#shootings per 100k counties map
counties = get_urbn_map("counties", sf=TRUE)
counties
## old-style crs object detected; please recreate object with a recent sf::st_crs()
## Warning in CPL_crs_from_input(x): GDAL Message 1: CRS EPSG:2163 is deprecated.
## Its non-deprecated replacement EPSG:9311 will be used instead. To use the
## original CRS, set the OSR_USE_NON_DEPRECATED configuration option to NO.
## old-style crs object detected; please recreate object with a recent sf::st_crs()
counties1 <- counties %>% 
  #remove unecessary words and the extra space 
  mutate(county_name = str_remove_all(county_name, " County| Parish| Borough| Census Area| Municipality")) %>% 
  #get rid of the period using gsub
  mutate(county_name = gsub("[.]", "", county_name)) 
pov_data = merged_data_final %>% select(stateabbrv, county_name, shootings_per_100k) 
colnames(pov_data)[1] = "state_abbv"
pov_data
data_merged = merge(counties1, pov_data, by = c("county_name", "state_abbv"))
## old-style crs object detected; please recreate object with a recent sf::st_crs()
data_merged
ggplot() +
  geom_sf(data = counties1, fill = "gray", color = "white") +
  geom_sf(data =data_merged, aes(fill = shootings_per_100k), color = "white") +
  theme_minimal() +
  scale_fill_gradient(low = "green", high = "tomato3") +
  labs(title = "Shootings per 100k by County",
       fill = "Shootings per 100k") +
  theme(plot.title = element_text(hjust = 0.5))
## old-style crs object detected; please recreate object with a recent sf::st_crs()
## old-style crs object detected; please recreate object with a recent sf::st_crs()

#shootings per 100k states map
states <- get_urbn_map("states", sf = TRUE)

pov_data1 <- pov_data %>% select(state_abbv, shootings_per_100k) %>% 
  group_by(state_abbv) %>% 
  summarise(mean_shootings = mean(shootings_per_100k))

data_merged1 = merge(states, pov_data1, by = c("state_abbv"))
## old-style crs object detected; please recreate object with a recent sf::st_crs()
data_merged1
ggplot() +
  geom_sf(data = states, fill = "lightgrey", color = "black") +
  geom_sf(data =data_merged1, aes(fill = mean_shootings), color = "black") +
  theme_minimal() +
  scale_fill_gradient(low = "white", high = "red") +
  labs(title = "Shootings per 100k by State",
       fill = "Shootings per 100k") +
  theme(plot.title = element_text(hjust = 0.5))
## old-style crs object detected; please recreate object with a recent sf::st_crs()
## old-style crs object detected; please recreate object with a recent sf::st_crs()