-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstaticplot.R
More file actions
201 lines (182 loc) · 7.69 KB
/
staticplot.R
File metadata and controls
201 lines (182 loc) · 7.69 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
#FINAL PROJECT PART 2 - STATIC PLOT FILES
# PPHA 30536 1 - Data and Programming for Public Policy II - R Programming
# Data Skills 2 - R
## Fall Quarter 2024
# Date: 11.30.24
# Student: Natalia Zorrilla
# Final Project - Static Plots
## Due: December 7, 2024 before midnight on Gradescope
############################################################################################################
#THIS PART OF THE PROJECT WILL SERVE TO VISUALIZE DIFFERENT ASPECTS OF CRIME, NARCOTICS CRIMES, MENTAL HEALTH RESOURCES AND SOCIOECONOMIC INDICATORS
library(dplyr)
library(ggplot2)
library(readr)
library(scales)
library(RColorBrewer)
library(viridis)
library(sf)
#All plots in this script will be automatically saved in the current working directory.
#To replicate this project, please set your working directory in this initial stage.
setwd("/Users/Natalia/Documents/The University of Chicago/Harris School of Public Policy/MPP/Fall quarter 2024/PPHA 30536 1 - Data and Programming for Public Policy II - R Programming /Assignments/Final project")
#Clear all objects
rm(list=ls())
#The following data sets will be needed
narcotics_health_project_data <- read_csv("narcotics_health_project_data.csv")
chicago_crime_sf <- read_csv("chicago_crime_sf.csv")
narc_sf <- readRDS("narcotics_spatial_data.rds")
crisis_sf <- readRDS("mental_health_spatial_data.rds")
##### STATIC PLOT 1 - TYPE OF CRIMES
# Group by crime type and count incidents
#Note: This was done as well in the cleaning process but it's replicated to create a plot
crime_by_type <- chicago_crime_sf %>%
group_by(`Primary Type`) %>%
summarize(Count = n(), .groups = "drop") %>%
arrange(desc(Count)) %>%
slice_max(Count, n = 10)
#Bar plot showing top 10 crime types
ggplot(crime_by_type, aes(x = reorder(`Primary Type`, Count), y = Count)) +
geom_bar(stat = "identity", fill = "steelblue") +
coord_flip() +
labs(
title = "Top 10 Crime Types in Chicago (2018-2022)",
x = "Crime Type",
y = "Number of Incidents",
caption = "Source: City of Chicago Data Portal"
) +
scale_y_continuous(labels = comma, breaks = seq(0, 250000, 50000)) +
theme_minimal() +
theme(
plot.caption = element_text(hjust = 1, size = 10, face = "italic"), # Style caption
plot.title = element_text(size = 16, face = "bold")
)
#save plot
ggsave("top_ten_crimes_barplot.png")
##### STATIC PLOT 2 - NARCOTICS AND SOCIOECONOMIC FACTORS
#Bar Plot
ggplot(narcotics_health_project_data, aes(x = Income_Bracket, y = total_crimes, fill = Income_Bracket)) +
geom_bar(stat = "identity") +
labs(
title = "Narcotics Crime Rates in Chicago by Income",
subtitle = "Binned Income Levels (2018-2022)",
x = "Median Household Income Bracket",
y = "Narcotics Crime Count",
caption = "Sources: Chicago Data Portal (Crime), Chicago Health Atlas (Socioeconomic Indicators)"
) +
scale_fill_brewer(palette = "Blues") +
theme_minimal() +
theme(
axis.text.x = element_text(angle = 45, hjust = 1),
plot.caption = element_text(hjust = 1),
legend.position = "none"
)
ggsave("narcotics_crimes_by_income.png", width = 8, height = 6)
##### STATIC PLOT 3 - NARCOTICS AND LAW ENFORCEMENT
#Bar Plot
ggplot(narcotics_health_project_data, aes(x = Trust_Law_Enforcement_Bins, y = total_crimes, fill = Trust_Law_Enforcement_Bins)) +
geom_bar(stat = "identity") +
labs(
title = "Narcotics Crime Rates in Chicago by Trust in Law Enforcement",
subtitle = "Binned Trust Rates Levels (2018-2022)",
x = "Trust in Law Enforcement (%)",
y = "Total Narcotics Crimes",
fill = "Trust Bin",
caption = "Sources: Chicago Data Portal (Crime), Chicago Health Atlas (Socioeconomic Indicators)"
) +
scale_fill_brewer(palette = "Blues") +
theme_minimal() +
theme(
axis.text.x = element_text(angle = 45, hjust = 1),
plot.caption = element_text(hjust = 1),
legend.position = "none"
)
ggsave("narcotics_crimes_by_trust_in_law.png", width = 8, height = 6)
##### STATIC PLOT 4 - NARCOTICS AND UNEMPLOYMENT
#Bar Plot
ggplot(narcotics_health_project_data, aes(x = Unemployment_Rate_Bins, y = total_crimes, fill = Unemployment_Rate_Bins)) +
geom_bar(stat = "identity") +
labs(
title = "Narcotics Crime Rates in Chicago by Unemployment Rate",
subtitle = "Binned Unemployment Rates (2018-2022)",
x = "Unemployment Rate (%)",
y = "Total Narcotics Crimes",
caption = "Sources: Chicago Data Portal (Crime), Chicago Health Atlas (Socioeconomic Indicators)"
) +
scale_fill_brewer(palette = "Blues") +
theme_minimal() +
theme(
axis.text.x = element_text(angle = 45, hjust = 1),
plot.caption = element_text(hjust = 1),
legend.position = "none"
)
ggsave("narcotics_crimes_by_unemployment.png", width = 8, height = 6)
##### STATIC PLOT 5 - NARCOTICS AND NEIGHBORHOOD SAFETY
#Bar Plot
ggplot(narcotics_health_project_data, aes(x = Safety_Rate_Bins, y = total_crimes, fill = Safety_Rate_Bins)) +
geom_bar(stat = "identity") +
labs(
title = "Narcotics Crime Rates by Neighborhood Safety Rate",
subtitle = "Binned Safety Rate Levels (2018-2022)",
x = "Neighborhood Safety Rate (%)",
y = "Total Narcotics Crimes",
caption = "Sources: Chicago Data Portal (Crime), Chicago Health Atlas (Socioeconomic Indicators)"
) +
scale_fill_brewer(palette = "Reds") +
theme_minimal() +
theme(
axis.text.x = element_text(angle = 45, hjust = 1),
plot.caption = element_text(hjust = 1),
legend.position = "none"
)
ggsave("narcotics_crimes_by_safety.png", width = 8, height = 6)
##### STATIC PLOT 6 - NARCOTICS CRIMES
#Note: Spatial plot to understand the geographic distribution of narcotics related crimes across Chicago
#Heat map
ggplot() +
geom_sf(data = narc_sf, aes(fill = total_crimes)) +
scale_fill_viridis_c(option = "viridis",
na.value = "gray80",
name = "Total Crimes") +
labs(title = "Total Narcotic related crimes in Chicago by Neighborhood",
caption = "Sources: Chicago Data Portal (Crime)") +
theme_minimal() +
theme(
plot.title = element_text(hjust = 0.5, size = 14),
axis.text = element_text(size = 8),
plot.caption = element_text(hjust = 1)
)
ggsave("narcotics_crimes_heatmap.png", width = 8, height = 6)
##### STATIC PLOT 7 - MENTAL HEALTH RESOURCES HEATMAP
#Note: Spatial plot to understand the geographic distribution of health clinics across Chicago
#Heat map
ggplot() +
geom_sf(data = crisis_sf, aes(fill = total_providers)) +
scale_fill_viridis_c(option = "viridis",
na.value = "gray80",
name = "Total Mental Health Providers",
labels = scales::label_number(accuracy = 1)) +
labs(title = "Total Mental Health Providers in Chicago by Neighborhood",
caption = "Source: Chicago Mental Health Resources Data") +
theme_minimal() +
theme(
plot.title = element_text(hjust = 0.5, size = 14),
axis.text = element_text(size = 8),
plot.caption = element_text(hjust = 1)
)
ggsave("mental_health_resources_heatmap.png", width = 8, height = 6)
##### STATIC PLOT 8 - CRISIS PROVIDERS HEATMAP
#Note: Spatial plot to understand the geographic distribution of crisis providers across Chicago
#Heat map
ggplot() +
geom_sf(data = crisis_sf, aes(fill = crisis_providers)) +
scale_fill_viridis_c(option = "viridis",
na.value = "gray80",
name = "Total Crisis Providers") +
labs(title = "Total Mental Health Crisis Providers in Chicago by Neighborhood",
caption = "Source: Chicago Mental Health Resources Data") +
theme_minimal() +
theme(
plot.title = element_text(hjust = 0.5, size = 14),
axis.text = element_text(size = 8),
plot.caption = element_text(hjust = 1)
)
ggsave("crisis_providers_heatmap.png", width = 8, height = 6)