Generate Philadelphia Violent Crime Data (2020–2022)
n <- 4000
crime <- data.frame(
lat = rnorm(n,39.95,0.03),
lng = rnorm(n,-75.16,0.04),
date = sample(seq(as.Date("2020-01-01"),as.Date("2022-12-31"),by="day"),n,replace=TRUE),
crime_type = sample(c("Homicide","Robbery","Aggravated Assault"),n,replace=TRUE)
)
crime$year <- year(crime$date)
crime$quarter <- quarter(crime$date)
violent <- crime %>%
filter(year>=2020 & year<=2022)
violent$year_quarter <- paste0(year(violent$date),"-Q",quarter(violent$date))
Create Daily Violent Crime Counts
daily_crime <- violent %>%
group_by(date) %>%
summarise(total_violent_crimes=n())
Scatterplot of Violent Crime Over Time
ggplot(daily_crime,aes(x=date,y=total_violent_crimes))+
geom_point(alpha=0.5,color="red")+
theme_minimal()+
labs(
title="Violent Crime Over Time in Philadelphia (2020–2022)",
x="Date",
y="Number of Violent Crimes"
)

Histogram of Violent Crimes by Quarter (2020–2022)
ggplot(violent,aes(x=year_quarter))+
geom_bar(fill="steelblue",color="black")+
theme_minimal()+
labs(
title="Violent Crimes by Quarter (2020–2022)",
x="Year-Quarter",
y="Number of Crimes"
)+
theme(axis.text.x=element_text(angle=45,hjust=1))

Map of Philadelphia with Crime Locations
philly_map <- map_data("county") %>%
filter(region=="pennsylvania",subregion=="philadelphia")
ggplot()+
geom_polygon(data=philly_map,aes(x=long,y=lat,group=group),
fill="gray90",color="black")+
geom_point(data=violent,aes(x=lng,y=lat),
alpha=0.15,color="darkred",size=0.5)+
coord_quickmap()+
theme_minimal()+
labs(title="Map of Violent Crime Incidents in Philadelphia (2020–2022)")
