-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path2. staicplot.R
More file actions
481 lines (389 loc) · 15.2 KB
/
2. staicplot.R
File metadata and controls
481 lines (389 loc) · 15.2 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
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
library(tidyverse)
library(ggplot2)
library(rvest)
library(stats)
library(rvest)
library(sentimentr)
library(tidytext)
library(sf)
library(stringr)
library(shiny)
library(dplyr)
library(shinyWidgets)
# 1. Load the data and fix the column names
# Set the directory
setwd("/Users/sohyunlim/Desktop/R_final_project/")
# Load the dataset
data <- read.csv("data/Public_Health_Statistics_-_Selected_public_health_indicators_by_Chicago_community_area_-_Historical_20241031.csv")
# Change column names
colnames(data) <- colnames(data) %>%
tolower() %>%
str_replace_all(fixed("."), "_") %>%
str_replace_all("__", "_") %>%
str_replace("_$", "")
# Select necessary columns
data <- data %>% select(community_area, community_area_name,
cancer_all_sites, diabetes_related,
below_poverty_level, unemployment, per_capita_income)
# 2-1. Create a bar graph with top 10 community area by main variables
# Public Health Indicator
# a, cancer_all_sites : Mortality per 100,000 persons
# b. diabetes_related : Mortality per 100,000 persons
# Socioeconomic Indicator
# a. below_poverty_level : Percent of households
# b. unemployment : Percent of persons in labor force aged 16 years and older
# c. per_capita_income : 2011 inflation-adjusted dollars
# Create a table for cancer_all_sites
cancer_areas <- data %>%
select(community_area, community_area_name, cancer_all_sites) %>%
arrange(desc(cancer_all_sites))
top_10_cancer_areas <- head(cancer_areas, 10)
diabetes_areas <- data %>%
select(community_area, community_area_name, diabetes_related) %>%
arrange(desc(diabetes_related))
top_10_diabetes_areas <- head(diabetes_areas, 10)
poverty_areas <- data %>%
select(community_area, community_area_name, below_poverty_level) %>%
arrange(desc(below_poverty_level))
top_10_poverty_areas <- head(poverty_areas, 10)
umemployment_areas <- data %>%
select(community_area, community_area_name, unemployment) %>%
arrange(desc(unemployment))
top_10_umemployment_areas <- head(umemployment_areas, 10)
income_areas <- data %>%
select(community_area, community_area_name, per_capita_income) %>%
arrange(per_capita_income)
top_10_income_areas <- head(income_areas, 10)
# Create a bar chart
# 2-1-1_top_10_cancer.png
bar_cancer <- ggplot(top_10_cancer_areas, aes(x = reorder(community_area_name, cancer_all_sites), y = cancer_all_sites)) +
geom_bar(stat = "identity", fill = "#377EB8") +
coord_flip() +
labs(
title = "Top 10 Community Areas with Highest Cancer Mortality",
x = "Community Area",
y = "Cancer (Mortality per 100,000 persons)"
) +
theme_minimal() +
theme(
plot.title = element_text(size = 14, face = "bold", hjust = 0.5),
axis.title.x = element_text(size = 12, face = "bold"),
axis.title.y = element_text(size = 12, face = "bold"),
axis.text.x = element_text(size = 10, face = "bold"),
axis.text.y = element_text(size = 10, face = "bold")
)
bar_cancer
bar_cancer_path <- "/Users/sohyunlim/Desktop/R_final_project/images/2-1_top_10_cancer.png"
ggsave(bar_cancer_path, plot = bar_cancer, width = 8, height = 6, dpi = 300)
# 2-1-2_top_10_diabetes.png
bar_diabetes <- ggplot(top_10_diabetes_areas, aes(x = reorder(community_area_name, diabetes_related), y = diabetes_related)) +
geom_bar(stat = "identity", fill = "#377EB8") +
coord_flip() +
labs(
title = "Top 10 Community Areas with Highest Diabetes Mortality",
x = "Community Area",
y = "Diabetes (Mortality per 100,000 persons)"
) +
theme_minimal() +
theme(
plot.title = element_text(size = 14, face = "bold", hjust = 0.5),
axis.title.x = element_text(size = 12, face = "bold"),
axis.title.y = element_text(size = 12, face = "bold"),
axis.text.x = element_text(size = 10, face = "bold"),
axis.text.y = element_text(size = 10, face = "bold")
) +
scale_y_continuous(
limits = c(0, 130),
breaks = seq(0, 125, by = 25)
)
bar_diabetes
bar_diabetes_path <- "/Users/sohyunlim/Desktop/R_final_project/images/2-2_top_10_diabetes.png"
ggsave(bar_diabetes_path, plot = bar_diabetes, width = 11, height = 6, dpi = 300)
# 2-1-3_top_10_poverty.png
bar_poverty <- ggplot(top_10_poverty_areas, aes(x = reorder(community_area_name, below_poverty_level), y = below_poverty_level)) +
geom_bar(stat = "identity", fill = "#377EB8") +
coord_flip() +
labs(
title = "Top 10 Community Areas with Highest Poverty Level",
x = "Community Area",
y = "Poverty (%)"
) +
theme_minimal() +
theme(
plot.title = element_text(size = 14, face = "bold", hjust = 0.5),
axis.title.x = element_text(size = 12, face = "bold"),
axis.title.y = element_text(size = 12, face = "bold"),
axis.text.x = element_text(size = 10, face = "bold"),
axis.text.y = element_text(size = 10, face = "bold")
)
bar_poverty
bar_poverty_path <- "/Users/sohyunlim/Desktop/R_final_project/images/2-3_top_10_poverty.png"
ggsave(bar_poverty_path, plot = bar_poverty, width = 8, height = 6, dpi = 300)
# 2-1-4_top_10_unemployment.png
bar_unemployment <- ggplot(top_10_umemployment_areas, aes(x = reorder(community_area_name, unemployment), y = unemployment)) +
geom_bar(stat = "identity", fill = "#377EB8") +
coord_flip() +
labs(
title = "Top 10 Community Areas with Highest Unemployment",
x = "Community Area",
y = "Unemployment (%)"
) +
theme_minimal() +
theme(
plot.title = element_text(size = 14, face = "bold", hjust = 0.5),
axis.title.x = element_text(size = 12, face = "bold"),
axis.title.y = element_text(size = 12, face = "bold"),
axis.text.x = element_text(size = 10, face = "bold"),
axis.text.y = element_text(size = 10, face = "bold")
)
bar_unemployment
bar_unemployment_path <- "/Users/sohyunlim/Desktop/R_final_project/images/2-4_top_10_unemployment.png"
ggsave(bar_unemployment_path, plot = bar_unemployment, width = 8, height = 6, dpi = 300)
# 2-1-5_top_10_income.png
bar_income <- ggplot(top_10_income_areas, aes(x = reorder(community_area_name, -per_capita_income), y = per_capita_income)) +
geom_bar(stat = "identity", fill = "#377EB8") +
coord_flip() +
labs(
title = "Top 10 Community Areas with Lowest Income",
x = "Community Area",
y = "Income ($)"
) +
theme_minimal() +
theme(
plot.title = element_text(size = 14, face = "bold", hjust = 0.5),
axis.title.x = element_text(size = 12, face = "bold"),
axis.title.y = element_text(size = 12, face = "bold"),
axis.text.x = element_text(size = 10, face = "bold"),
axis.text.y = element_text(size = 10, face = "bold")
)
bar_income
bar_income_path <- "/Users/sohyunlim/Desktop/R_final_project/images/2-5_top_10_income.png"
ggsave(bar_income_path, plot = bar_income, width = 8, height = 6, dpi = 300)
# 2-2. Geospatial analysis
# Load a shp file.
# Read csv file
healthcare_data <- read.csv("/Users/sohyunlim/Desktop/R_final_project/data/Map_-_Public_Health_Services_-_Chicago_Primary_Care_Community_Health_Centers.csv")
# Fix the column names
colnames(healthcare_data) <- colnames(healthcare_data) %>%
tolower() %>%
str_replace_all(fixed("."), "_") %>%
str_replace_all("__", "_") %>%
str_replace("_$", "")
head(healthcare_data)
# Extract latitude and longitude from Address
extract_lat_lon <- function(address) {
match <- str_match(address, "\\(([-+]?[0-9]*\\.?[0-9]+), ([-+]?[0-9]*\\.?[0-9]+)\\)")
if (!is.na(match[1, 1])) {
return(c(as.numeric(match[1, 2]), as.numeric(match[1, 3])))
} else {
return(c(NA, NA))
}
}
# Add latitude and longitude columns
lat_lon <- t(apply(as.data.frame(healthcare_data$address), 1, extract_lat_lon))
healthcare_data$latitude <- lat_lon[, 1]
healthcare_data$longitude <- lat_lon[, 2]
head(healthcare_data[, c("facility", "latitude", "longitude")])
# Drop NA
healthcare_data <- healthcare_data %>% filter(!is.na(latitude) & !is.na(longitude))
# Convert into sf
points_sf <- st_as_sf(
healthcare_data,
coords = c("longitude", "latitude"),
crs = 4326
)
# Read Shapefile
chi_shp <- st_read("/Users/sohyunlim/Desktop/R_final_project/data/Comm_20Areas__1_/CommAreas.shp")
print(chi_shp)
#. Chicago Health Center Map
# Check CRS
chi_shp <- st_transform(chi_shp, crs = 4326)
points_sf <- st_transform(points_sf, crs = 4326)
# Set the range of longitude and latitude of Chicago
chi_longitude_range <- c(-88, -87.5)
chi_latitude_range <- c(41.6, 42)
# Create a map
chicago_health_center <- ggplot() +
geom_sf(data = chi_shp, fill = "lightgrey", color = "gray", alpha = 0.3) +
geom_sf(data = points_sf, color = "red", size = 2, shape = 21) +
labs(
title = "Chicago Map with Healthcare Centers"
) +
coord_sf(
xlim = chi_longitude_range,
ylim = chi_latitude_range,
expand = FALSE
) +
theme_void() +
theme(
plot.title = element_text(
hjust = 0.5,
size = 14,
margin = margin(t = 10, b = 20)
)
)
chicago_health_center
chicago_health_center_path <- "/Users/sohyunlim/Desktop/R_final_project/images/2-6_chicago_health_center.png"
ggsave(filename = chicago_health_center_path, plot = chicago_health_center, width = 10, height = 6, dpi = 300)
# Merge chicago shp and health dataset
# Chicago shape file : chi_shp
# Chicago health dataset : data
# Fix the column names
colnames(chi_shp) <- colnames(chi_shp) %>%
tolower() %>%
str_replace_all(fixed("."), "_") %>%
str_replace_all("__", "_") %>%
str_replace("_$", "")
# Change column name in the shp file.
chi_shp <- chi_shp %>%
rename(community_area = area_num_1)
chi_shp$community_area <- as.numeric(chi_shp$community_area)
data$community_area <- as.numeric(data$community_area)
# Merge data
merged_data <- chi_shp %>%
left_join(data, by = "community_area")
# 2-3. Chicago map with public health indicators
# 2-3-1. cancer_all_sites
map_cancer <- ggplot(data = merged_data) +
geom_sf(aes(fill = cancer_all_sites), color = "gray", size = 0.2) +
scale_fill_gradient(
low = "lightblue",
high = "darkblue",
name = "Cancer (Mortality per 100,000 persons)"
) +
labs(
title = "Cancer MOrtality in Chicago Community Areas"
) +
theme_minimal() +
theme(
plot.title = element_text(hjust = 0.5, size = 16, face = "bold"),
axis.text = element_blank(),
axis.ticks = element_blank()
)
map_cancer
map_cancer_path <- "/Users/sohyunlim/Desktop/R_final_project/images/2-7_chicago_cancer.png"
ggsave(filename = map_cancer_path, plot = map_cancer, width = 10, height = 6, dpi = 300)
# 2-3-2. diabetes_related
map_diabetes <- ggplot(data = merged_data) +
geom_sf(aes(fill = diabetes_related), color = "gray", size = 0.2) +
scale_fill_gradient(
low = "lightblue",
high = "darkblue",
name = "Diabetes (Mortality per 100,000 persons)"
) +
labs(
title = "Diabetes Mortality in Chicago Community Areas"
) +
theme_minimal() +
theme(
plot.title = element_text(hjust = 0.5, size = 16, face = "bold"),
axis.text = element_blank(),
axis.ticks = element_blank()
)
map_diabetes
map_diabetes_path <- "/Users/sohyunlim/Desktop/R_final_project/images/2-8_chicago_diabetes.png"
ggsave(filename = map_diabetes_path, plot = map_diabetes, width = 10, height = 6, dpi = 300)
# 2-4. Chicago map with socioeconomic indicators
# 2-4-1. below_poverty_level
map_poverty <- ggplot(data = merged_data) +
geom_sf(aes(fill = below_poverty_level), color = "gray", size = 0.2) +
scale_fill_gradient(
low = "#FFF5B7",
high = "red",
name = "Poverty Level (%)"
) +
labs(
title = "Below Poverty Level by Chicago Community Areas"
) +
theme_minimal() +
theme(
plot.title = element_text(hjust = 0.5, size = 16, face = "bold"),
axis.text = element_blank(),
axis.ticks = element_blank()
)
map_poverty
map_poverty_path <- "/Users/sohyunlim/Desktop/R_final_project/images/2-9_chicago_poverty.png"
ggsave(filename = map_poverty_path, plot = map_poverty, width = 10, height = 6, dpi = 300)
# 2-4-2. unemployment
map_unemployment <- ggplot(data = merged_data) +
geom_sf(aes(fill = unemployment), color = "gray", size = 0.2) +
scale_fill_gradient(
low = "#FFF5B7",
high = "red",
name = "Unemployment (%)"
) +
labs(
title = "Unemployment by Chicago Community Areas"
) +
theme_minimal() +
theme(
plot.title = element_text(hjust = 0.5, size = 16, face = "bold"),
axis.text = element_blank(),
axis.ticks = element_blank()
)
map_unemployment
map_unemployment_path <- "/Users/sohyunlim/Desktop/R_final_project/images/2-10_chicago_unemployment.png"
ggsave(filename = map_unemployment_path, plot = map_unemployment, width = 10, height = 6, dpi = 300)
# 2-4-3. per_capita_income
map_income <- ggplot(data = merged_data) +
geom_sf(aes(fill = per_capita_income), color = "gray", size = 0.2) +
scale_fill_gradient(
low = "#FFF5B7",
high = "#FF8040",
name = "Income($)"
) +
labs(
title = "Income by Chicago Community Areas"
) +
theme_minimal() +
theme(
plot.title = element_text(hjust = 0.5, size = 16, face = "bold"),
axis.text = element_blank(),
axis.ticks = element_blank()
)
map_income
map_income_path <- "/Users/sohyunlim/Desktop/R_final_project/images/2-11_chicago_income.png"
ggsave(filename = map_income_path, plot = map_income, width = 10, height = 6, dpi = 300)
# 2-5. Additional. Create a map showing health outcome and socioeconomic status in Chicago
# Top 10 regions overlapped regarding cancer and diabetes mortality
overlap_cancer_diabetes <- intersect(top_10_cancer_areas$community_area, top_10_diabetes_areas$community_area)
# Filter the data of those regions
overlap_data <- chi_shp %>%
filter(community_area %in% overlap_cancer_diabetes)
# Create a map
map_cancer_diabetes_overlap <- ggplot() +
geom_sf(data = chi_shp, fill = "lightgrey", color = "gray", alpha = 0.3) +
geom_sf(data = overlap_data, fill = "red", color = "black", size = 0.2) +
labs(
title = "Top 10 Cancer & Diabetes Areas in Chicago"
) +
theme_void() +
theme(
plot.title = element_text(hjust = 0.5, size = 16, face = "bold", margin = margin(t = 10, b = 20))
)
map_cancer_diabetes_overlap
map_cancer_diabetes_overlap_path <- "/Users/sohyunlim/Desktop/R_final_project/images/2-12_top_10_overlap_health.png"
ggsave(map_cancer_diabetes_overlap_path, plot = map_cancer_diabetes_overlap, width = 8, height = 6, dpi = 300)
# Top 10 regions overlapped regarding highest poverty, unemplyoemtn, and low income
overlap_socioeconomic <- Reduce(intersect, list(
top_10_poverty_areas$community_area,
top_10_umemployment_areas$community_area,
top_10_income_areas$community_area
))
# Filter the data of those regions
overlap_socioeconomic_data <- chi_shp %>%
filter(community_area %in% overlap_socioeconomic)
# Create a map
map_socioeconomic_overlap <- ggplot() +
geom_sf(data = chi_shp, fill = "lightgrey", color = "gray", alpha = 0.3) +
geom_sf(data = overlap_socioeconomic_data, fill = "blue", color = "black", size = 0.2) +
labs(
title = "Top 10 Socioeconomic Indicators in Chicago"
) +
theme_void() +
theme(
plot.title = element_text(hjust = 0.5, size = 16, face = "bold", margin = margin(t = 10, b = 20))
)
map_socioeconomic_overlap
map_socioeconomic_overlap_path <- "/Users/sohyunlim/Desktop/R_final_project/images/2-13_top_10_overlap_socio.png"
ggsave(map_socioeconomic_overlap_path, plot = map_socioeconomic_overlap, width = 8, height = 6, dpi = 300)