shootings_df <- read.csv("NYPD_Shooting_Incident_Data__Historic_.csv")
shootings_count <- shootings_df %>% count(VIC_RACE)
ggplot(shootings_count, aes(x = VIC_RACE, y = n)) +
geom_bar(stat = "identity", fill = "skyblue",
aes(label = n), position = "dodge") +
labs(x = "Victim Race", y = "Count of Shootings",
title = "Shootings Count by Victim Race") +
theme_minimal() +
theme_tufte() +
theme(axis.text.x = element_text(angle = 45, hjust = 1))
Here, you can see that the majority of the victims of shootings were black.
shootings_fatal <- shootings_df %>% count(STATISTICAL_MURDER_FLAG)
shootings_fatal <- shootings_fatal %>%
mutate(STATISTICAL_MURDER_FLAG = case_when(
STATISTICAL_MURDER_FLAG == "FALSE" ~ "Non-fatal",
STATISTICAL_MURDER_FLAG == "TRUE" ~ "Fatal",
TRUE ~ as.character(STATISTICAL_MURDER_FLAG)
))
ggplot(shootings_fatal, aes(x = STATISTICAL_MURDER_FLAG, y = n)) +
geom_bar(stat = "identity", fill = "orange",
aes(label = n), position = "dodge") +
labs(x = "Fatality", y = "Count",
title = "Fatal vs. Non-fatal shootings") +
theme_minimal() +
theme_tufte() +
theme(axis.text.x = element_text(hjust = 1)) +
geom_text(aes(label = n),
position = position_dodge(width = 0.9),
vjust = -0.5, size = 3)
Thankfully, most of the shooting incidents in NYC was non-fatal. However, a significant proportion of shootings did end up being fatal, which is alarming.
shootings_gender <- shootings_df %>% count(VIC_SEX)
shootings_gender <- shootings_gender %>%
mutate(VIC_SEX = case_when(
VIC_SEX == "F" ~ "Female",
VIC_SEX == "M" ~ "Male",
VIC_SEX == "U" ~ "Unidentified",
TRUE ~ as.character(VIC_SEX)
))
ggplot(shootings_gender, aes(x = VIC_SEX, y = n)) +
geom_bar(stat = "identity", fill = "red",
aes(label = n), position = "dodge") +
labs(x = "Victim's gender", y = "Count", title = "Victims by Gender") +
theme_minimal() +
theme_tufte() +
theme(axis.text.x = element_text(hjust = 1)) +
geom_text(aes(label = n),
position = position_dodge(width = 0.9),
vjust = -0.5, size = 3)
Most of the victims were male. What could this entail?
data <- read.csv("NYPD_Shooting_Incident_Data__Historic_.csv")
#Year Plot
data$OCCUR_DATE <- as.Date(data$OCCUR_DATE, format="%m/%d/%Y")
data$Year <- as.numeric(format(data$OCCUR_DATE, "%Y"))
yearly_counts <- as.data.frame(table(data$Year))
names(yearly_counts) <- c("Year", "Incidents")
ggplot(data = yearly_counts, aes(x = Year, y = Incidents)) +
geom_line(aes(group=1)) +
geom_point() +
labs(title = "Shooting Incidents in NYC by Year", x = "Year", y = "Number of Incidents") +
theme_minimal() +
theme_tufte()
Here, you can observe that the shooting incidents saw a rapid decrease in the years 2017 to 2019. Could this mean something?
data$MonthNum <- as.numeric(format(data$OCCUR_DATE, "%m"))
data$Month <- factor(data$MonthNum, levels = 1:12, labels = c("Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"))
monthly_counts <- as.data.frame(table(data$Month))
names(monthly_counts) <- c("Month", "Incidents")
ggplot(data = monthly_counts, aes(x = Month, y = Incidents)) +
geom_bar(stat="identity", fill="steelblue") +
labs(title = "Shooting Incidents in NYC by Month", x = "Month", y = "Number of Incidents") +
theme_minimal() +
theme_tufte()
The pattern here is clear. The number of shooting incidents is highest around summer time. What could this mean?
data$DayOfWeek <- weekdays(data$OCCUR_DATE)
data$DayOfWeek <- factor(data$DayOfWeek, levels = c("Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday"))
day_of_week_counts <- as.data.frame(table(data$DayOfWeek))
names(day_of_week_counts) <- c("DayOfWeek", "Incidents")
ggplot(data = day_of_week_counts, aes(x = DayOfWeek, y = Incidents)) +
geom_bar(stat="identity", fill="purple") +
labs(title = "Shooting Incidents in NYC by Day of the Week", x = "Day of the Week", y = "Number of Incidents") +
theme_minimal() +
theme_tufte()
As the week approaches the weekend, the shooting incidents rise.
categorize_time_of_day <- function(time_str) {
if (is.na(time_str)) {
return(NA)
}
time_components <- strsplit(time_str, ":")[[1]]
if (length(time_components) != 3) {
return(NA)
}
hour <- as.numeric(time_components[1])
if (!is.na(hour)) {
if (hour >= 5 & hour < 12) {
return('Morning')
} else if (hour >= 12 & hour < 17) {
return('Afternoon')
} else if (hour >= 17 & hour < 21) {
return('Evening')
} else {
return('Night')
}
} else {
return(NA)
}
}
data$TIME_OF_DAY <- sapply(data$OCCUR_TIME, categorize_time_of_day)
data$TIME_OF_DAY <- factor(data$TIME_OF_DAY, levels = c("Morning", "Afternoon", "Evening", "Night"))
time_of_day_counts <- as.data.frame(table(data$TIME_OF_DAY))
names(time_of_day_counts) <- c("TimeOfDay", "Incidents")
ggplot(data = time_of_day_counts, aes(x = TimeOfDay, y = Incidents)) +
geom_bar(stat = "identity", fill = "coral") +
labs(title = "Shooting Incidents in NYC by Time of Day", x = "Time of Day", y = "Number of Incidents") +
theme_minimal() +
theme_tufte()
Most of the shooting incidents take place during the night hours.
unique(data$VIC_AGE_GROUP)
## [1] "18-24" "25-44" "<18" "45-64" "65+" "UNKNOWN" "1022"
data <- data[data$VIC_AGE_GROUP != "1022", ]
vic_age_group_counts <- as.data.frame(table(data$VIC_AGE_GROUP))
names(vic_age_group_counts) <- c("VIC_AGE_GROUP", "Freq")
ggplot(data = vic_age_group_counts, aes(x = VIC_AGE_GROUP, y = Freq)) +
geom_bar(stat="identity", fill="green") +
labs(title = "Victim Age Group Distribution in NYC Shooting Incidents", x = "Age Group", y = "Number of Incidents") +
theme_minimal() +
theme_tufte()
The number of victims of age 18 to 44 is significantly higher than that of other age groups. What could this mean?
precincts <- st_read("NYC_Police_Precincts.shp")
## Reading layer `NYC_Police_Precincts' from data source
## `/Users/seunghyunhyun/Desktop/GMUK Fall 2023/CDS 301/workesheets/NYC_Police_Precincts.shp'
## using driver `ESRI Shapefile'
## Simple feature collection with 77 features and 4 fields
## Geometry type: MULTIPOLYGON
## Dimension: XY
## Bounding box: xmin: -74.25559 ymin: 40.49614 xmax: -73.70001 ymax: 40.91554
## Geodetic CRS: WGS 84
incident_counts <- data %>%
group_by(PRECINCT) %>%
summarise(Incidents = n())
precincts_data <- merge(precincts, incident_counts, by.x = "Precinct", by.y = "PRECINCT")
ggplot(data = precincts_data) +
geom_sf(aes(fill = Incidents), color = "white", size = 0.2) +
scale_fill_viridis_c() +
labs(title = "NYPD Shooting Incidents by Precinct", fill = "Incidents") +
theme_minimal()
precincts_data <- left_join(precincts_data, data, by = c("Precinct" = "PRECINCT"))
df <- read.csv("NYPD_Shooting_Incident_Data__Historic_.csv")
df2 <- df %>% select(OCCUR_DATE, BORO, PRECINCT, VIC_RACE, Latitude, Longitude) %>%
filter(Latitude >0 | Longitude < 0)
boro <- st_read("geo_export_aa9c370f-425c-4d4f-b393-e300ad6d8d27.shp")
## Reading layer `geo_export_aa9c370f-425c-4d4f-b393-e300ad6d8d27' from data source `/Users/seunghyunhyun/Desktop/GMUK Fall 2023/CDS 301/workesheets/geo_export_aa9c370f-425c-4d4f-b393-e300ad6d8d27.shp'
## using driver `ESRI Shapefile'
## Simple feature collection with 5 features and 4 fields
## Geometry type: MULTIPOLYGON
## Dimension: XY
## Bounding box: xmin: -74.25559 ymin: 40.49613 xmax: -73.70001 ymax: 40.91553
## Geodetic CRS: WGS84(DD)
boro <- st_transform(boro, crs = 4326)
df_t <- df2 %>%
group_by(BORO) %>% summarize(n = n())
df_t$BORO[df_t$BORO=='BRONX'] <- 'Bronx'
df_t$BORO[df_t$BORO=='BROOKLYN'] <- 'Brooklyn'
df_t$BORO[df_t$BORO=='MANHATTAN'] <- 'Manhattan'
df_t$BORO[df_t$BORO=='QUEENS'] <- 'Queens'
df_t$BORO[df_t$BORO=='STATEN ISLAND'] <- 'Staten Island'
BORO <- merge(boro, df_t, by.x = 'boro_name', by.y = 'BORO', all=T)
df2_point <- st_as_sf(x = df2,
coords = c("Longitude", "Latitude"),
crs = st_crs(4326)) # WGS84
boro <- st_transform(boro, crs = st_crs(df2_point))
st_crs(boro) == st_crs(df2_point)
## [1] TRUE
sf::sf_use_s2(FALSE)
df2_point_joined <- st_join(df2_point, boro, join = st_intersects)
count_boro <- df2_point_joined %>%
group_by(boro_name) %>%
summarize(n = n()) %>%
st_drop_geometry()
BORO <- merge(boro, count_boro, by=c("boro_name"))
BORO <- st_transform(BORO, crs = 3857)
boro_carto <- cartogram_cont(BORO, 'n', itermax=5)
ggplot() +
geom_sf(boro_carto, mapping = aes(fill = n)) +
scale_fill_gradient(low = "#d0d1e6", high = "#045a8d")+
ggtitle("Cartogram indicating the number of shooting incidents in NYC")+ #title
geom_sf_text(boro_carto, mapping = aes(label = boro_name), color = "white", size = 2) +
theme_void()