-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path.Rhistory
512 lines (512 loc) · 20.1 KB
/
.Rhistory
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
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
xlab("Number of Deaths (log10)") +
scale_x_log10(limits = c(),
breaks = c(0, 1, 10, 30, 100, 300)) +
scale_y_log10(name = "Number Of Admissions (log10)",
limits = c(),
breaks = c(1, 10, 30, 100, 300, 1000, 10000)) +
facet_wrap(~sex, ncol = 0) +
coord_flip() +
guides(color=guide_legend(title = "Age Group"))
#Need to include the Death Data
clean_full_ui_filtered %>%
ggplot(aes(x=numberof_deaths, y=number_of_admissions, color=age_group)) +
geom_point(alpha = 1, size = 1.7) +
scale_color_brewer(palette = "Oranges") +
theme_dark() +
ggtitle("Rate of Deaths in Falling Injury Admissions") +
xlab("Number of Deaths (log10)") +
scale_x_log10(limits = c(),
breaks = c(0, 1, 10, 30, 100, 300)) +
scale_y_log10(name = "Number Of Admissions (log10)",
limits = c(),
breaks = c(1, 10, 30, 100, 300, 1000, 10000)) +
facet_wrap(~sex, ncol = 0) +
coord_flip() +
guides(color=guide_legend(title = "Age Group"))
#Need to include the Death Data
clean_full_ui_filtered %>%
ggplot(aes(x=numberof_deaths, y=number_of_admissions, color=age_group)) +
geom_point(alpha = 1, size = 1.7) +
scale_color_brewer(palette = "Accent") +
theme_dark() +
ggtitle("Rate of Deaths in Falling Injury Admissions") +
xlab("Number of Deaths (log10)") +
scale_x_log10(limits = c(),
breaks = c(0, 1, 10, 30, 100, 300)) +
scale_y_log10(name = "Number Of Admissions (log10)",
limits = c(),
breaks = c(1, 10, 30, 100, 300, 1000, 10000)) +
facet_wrap(~sex, ncol = 0) +
coord_flip() +
guides(color=guide_legend(title = "Age Group"))
#Need to include the Death Data
clean_full_ui_filtered %>%
ggplot(aes(x=numberof_deaths, y=number_of_admissions, color=age_group)) +
geom_point(alpha = 1, size = 1.7) +
scale_color_brewer(palette = "Spectral") +
theme_dark() +
ggtitle("Rate of Deaths in Falling Injury Admissions") +
xlab("Number of Deaths (log10)") +
scale_x_log10(limits = c(),
breaks = c(0, 1, 10, 30, 100, 300)) +
scale_y_log10(name = "Number Of Admissions (log10)",
limits = c(),
breaks = c(1, 10, 30, 100, 300, 1000, 10000)) +
facet_wrap(~sex, ncol = 0) +
coord_flip() +
guides(color=guide_legend(title = "Age Group"))
#Need to include the Death Data
clean_full_ui_filtered %>%
ggplot(aes(x=numberof_deaths, y=number_of_admissions, color=age_group)) +
geom_point(alpha = 1, size = 1.7) +
scale_color_brewer(palette = "Paired") +
theme_dark() +
ggtitle("Rate of Deaths in Falling Injury Admissions") +
xlab("Number of Deaths (log10)") +
scale_x_log10(limits = c(),
breaks = c(0, 1, 10, 30, 100, 300)) +
scale_y_log10(name = "Number Of Admissions (log10)",
limits = c(),
breaks = c(1, 10, 30, 100, 300, 1000, 10000)) +
facet_wrap(~sex, ncol = 0) +
coord_flip() +
guides(color=guide_legend(title = "Age Group"))
#Need to include the Death Data
clean_full_ui_filtered %>%
ggplot(aes(x=numberof_deaths, y=number_of_admissions, color=age_group)) +
geom_point(alpha = 1, size = 1.7) +
geom_smooth(method=lm , color="red", fill="#69b3a2", se=TRUE)
scale_color_brewer(palette = "Paired") +
theme_dark() +
ggtitle("Rate of Deaths in Falling Injury Admissions") +
xlab("Number of Deaths (log10)") +
scale_x_log10(limits = c(),
breaks = c(0, 1, 10, 30, 100, 300)) +
scale_y_log10(name = "Number Of Admissions (log10)",
limits = c(),
breaks = c(1, 10, 30, 100, 300, 1000, 10000)) +
facet_wrap(~sex, ncol = 0) +
coord_flip() +
guides(color=guide_legend(title = "Age Group"))
clean_full_ui_filtered %>%
ggplot(aes(x=numberof_deaths, y=number_of_admissions, color=age_group)) +
geom_point(alpha = 1, size = 1.7) +
geom_smooth(method=lm , color="red", fill="#69b3a2", se=TRUE) +
scale_color_brewer(palette = "Paired") +
theme_dark() +
ggtitle("Rate of Deaths in Falling Injury Admissions") +
xlab("Number of Deaths (log10)") +
scale_x_log10(limits = c(),
breaks = c(0, 1, 10, 30, 100, 300)) +
scale_y_log10(name = "Number Of Admissions (log10)",
limits = c(),
breaks = c(1, 10, 30, 100, 300, 1000, 10000)) +
facet_wrap(~sex, ncol = 0) +
coord_flip() +
guides(color=guide_legend(title = "Age Group"))
#Need to include the Death Data
clean_full_ui_filtered %>%
ggplot(aes(x=numberof_deaths, y=number_of_admissions, color=age_group)) +
geom_point(alpha = 1, size = 1.7) +
geom_smooth(method=lm , color="black", fill="#69b3a2", se=TRUE) +
scale_color_brewer(palette = "Paired") +
theme_dark() +
ggtitle("Rate of Deaths in Falling Injury Admissions") +
xlab("Number of Deaths (log10)") +
scale_x_log10(limits = c(),
breaks = c(0, 1, 10, 30, 100, 300)) +
scale_y_log10(name = "Number Of Admissions (log10)",
limits = c(),
breaks = c(1, 10, 30, 100, 300, 1000, 10000)) +
facet_wrap(~sex, ncol = 0) +
coord_flip() +
guides(color=guide_legend(title = "Age Group"))
#Need to include the Death Data
clean_full_ui_filtered %>%
ggplot(aes(x=numberof_deaths, y=number_of_admissions, color=age_group)) +
geom_point(alpha = 1, size = 1.7) +
geom_smooth(method=lm , color="black", fill="#69b3a2", se=TRUE) +
scale_color_brewer(palette = "Paired") +
theme_dark() +
ggtitle("Rate of Deaths in Falling Injury Admissions") +
xlab("Number of Deaths (log10)") +
scale_x_log10(limits = c(),
breaks = c(0, 1, 10, 30, 100, 300)) +
scale_y_log10(name = "Number Of Admissions (log10)",
limits = c(),
breaks = c(1, 10, 30, 100, 300, 1000, 10000)) +
facet_wrap(~sex, ncol = 0) +
coord_flip() +
guides(color=guide_legend(title = "Age Group"))
#Need to include the Death Data
clean_full_ui_filtered %>%
ggplot(aes(x=numberof_deaths, y=number_of_admissions, color=age_group)) +
geom_point(alpha = 1, size = 1.7) +
geom_rug(col="steelblue",alpha=0.1, size=1.5) +
geom_smooth(method=lm , color="black", fill="#69b3a2", se=TRUE) +
scale_color_brewer(palette = "Paired") +
theme_dark() +
ggtitle("Rate of Deaths in Falling Injury Admissions") +
xlab("Number of Deaths (log10)") +
scale_x_log10(limits = c(),
breaks = c(0, 1, 10, 30, 100, 300)) +
scale_y_log10(name = "Number Of Admissions (log10)",
limits = c(),
breaks = c(1, 10, 30, 100, 300, 1000, 10000)) +
facet_wrap(~sex, ncol = 0) +
coord_flip() +
guides(color=guide_legend(title = "Age Group"))
#Need to include the Death Data
clean_full_ui_filtered %>%
ggplot(aes(x=numberof_deaths, y=number_of_admissions, color=age_group)) +
geom_point(alpha = 1, size = 1.7) +
geom_smooth(method=lm , color="black", fill="#69b3a2", se=TRUE) +
scale_color_brewer(palette = "Paired") +
theme_dark() +
ggtitle("Rate of Deaths in Falling Injury Admissions") +
xlab("Number of Deaths (log10)") +
scale_x_log10(limits = c(),
breaks = c(0, 1, 10, 30, 100, 300)) +
scale_y_log10(name = "Number Of Admissions (log10)",
limits = c(),
breaks = c(1, 10, 30, 100, 300, 1000, 10000)) +
facet_wrap(~sex, ncol = 0) +
coord_flip() +
guides(color=guide_legend(title = "Age Group"))
#Need to include the Death Data
clean_full_ui_filtered %>%
ggplot(aes(x=numberof_deaths, y=number_of_admissions, color=age_group)) +
geom_point(alpha = 1, size = 1.7) +
geom_smooth(method=lm , color="black", fill="#69b3a2", se=TRUE, method=loess()) +
scale_color_brewer(palette = "Paired") +
theme_dark() +
ggtitle("Rate of Deaths in Falling Injury Admissions") +
xlab("Number of Deaths (log10)") +
scale_x_log10(limits = c(),
breaks = c(0, 1, 10, 30, 100, 300)) +
scale_y_log10(name = "Number Of Admissions (log10)",
limits = c(),
breaks = c(1, 10, 30, 100, 300, 1000, 10000)) +
facet_wrap(~sex, ncol = 0) +
coord_flip() +
guides(color=guide_legend(title = "Age Group"))
clean_full_ui_filtered %>%
ggplot(aes(x=numberof_deaths, y=number_of_admissions, color=age_group)) +
geom_point(alpha = 1, size = 1.7) +
geom_smooth(method=lm , color="black", fill="#69b3a2", se=TRUE, method=loess) +
scale_color_brewer(palette = "Paired") +
theme_dark() +
ggtitle("Rate of Deaths in Falling Injury Admissions") +
xlab("Number of Deaths (log10)") +
scale_x_log10(limits = c(),
breaks = c(0, 1, 10, 30, 100, 300)) +
scale_y_log10(name = "Number Of Admissions (log10)",
limits = c(),
breaks = c(1, 10, 30, 100, 300, 1000, 10000)) +
facet_wrap(~sex, ncol = 0) +
coord_flip() +
guides(color=guide_legend(title = "Age Group"))
clean_full_ui_filtered %>%
ggplot(aes(x=numberof_deaths, y=number_of_admissions, color=age_group)) +
geom_point(alpha = 1, size = 1.7) +
geom_smooth(method=glm, color="black", fill="#69b3a2", se=TRUE,) +
scale_color_brewer(palette = "Paired") +
theme_dark() +
ggtitle("Rate of Deaths in Falling Injury Admissions") +
xlab("Number of Deaths (log10)") +
scale_x_log10(limits = c(),
breaks = c(0, 1, 10, 30, 100, 300)) +
scale_y_log10(name = "Number Of Admissions (log10)",
limits = c(),
breaks = c(1, 10, 30, 100, 300, 1000, 10000)) +
facet_wrap(~sex, ncol = 0) +
coord_flip() +
guides(color=guide_legend(title = "Age Group"))
#Need to include the Death Data
clean_full_ui_filtered %>%
ggplot(aes(x=numberof_deaths, y=number_of_admissions, color=age_group)) +
geom_point(alpha = 1, size = 1.7) +
scale_color_brewer(palette = "Paired") +
theme_dark() +
ggtitle("Rate of Deaths in Falling Injury Admissions") +
xlab("Number of Deaths (log10)") +
scale_x_log10(limits = c(),
breaks = c(0, 1, 10, 30, 100, 300)) +
scale_y_log10(name = "Number Of Admissions (log10)",
limits = c(),
breaks = c(1, 10, 30, 100, 300, 1000, 10000)) +
facet_wrap(~sex, ncol = 0) +
coord_flip() +
guides(color=guide_legend(title = "Age Group"))
#Need to include the Death Data
clean_full_ui_filtered %>%
ggplot(aes(x=numberof_deaths, y=number_of_admissions, color=age_group)) +
geom_point(alpha = 1, size = 1.7) +
scale_color_brewer(palette = "Paired") +
theme_dark() +
ggtitle("Rate of Deaths in Falling Injury Admissions") +
xlab("Number of Deaths (log10)") +
scale_x_continuous(limits = c(),
breaks = c(0, 1, 10, 30, 100, 300)) +
scale_y_log10(name = "Number Of Admissions (log10)",
limits = c(),
breaks = c(1, 10, 30, 100, 300, 1000, 10000)) +
facet_wrap(~sex, ncol = 0) +
coord_flip() +
guides(color=guide_legend(title = "Age Group"))
clean_full_ui_filtered %>%
ggplot(aes(x=numberof_deaths, y=number_of_admissions, color=age_group)) +
geom_point(alpha = 1, size = 1.7) +
scale_color_brewer(palette = "Paired") +
theme_dark() +
ggtitle("Rate of Deaths in Falling Injury Admissions") +
xlab("Number of Deaths") +
scale_x_continuous(limits = c(),
breaks = c(0, 1, 10, 50, 100, 150, 200, 250, 300) +
scale_y_log10(name = "Number Of Admissions (log10)",
limits = c(),
breaks = c(1, 10, 30, 100, 300, 1000, 10000)) +
facet_wrap(~sex, ncol = 0) +
coord_flip() +
guides(color=guide_legend(title = "Age Group"))
clean_full_ui_filtered %>%
ggplot(aes(x=numberof_deaths, y=number_of_admissions, color=age_group)) +
geom_point(alpha = 1, size = 1.7) +
scale_color_brewer(palette = "Paired") +
theme_dark() +
ggtitle("Rate of Deaths in Falling Injury Admissions") +
xlab("Number of Deaths") +
scale_x_continuous(limits = c(),
breaks = c(0, 1, 10, 50, 100, 150, 200, 250, 300)) +
scale_y_log10(name = "Number Of Admissions (log10)",
limits = c(),
breaks = c(1, 10, 30, 100, 300, 1000, 10000)) +
facet_wrap(~sex, ncol = 0) +
coord_flip() +
guides(color=guide_legend(title = "Age Group"))
clean_full_ui_filtered %>%
ggplot(aes(x=numberof_deaths, y=number_of_admissions, color=age_group)) +
geom_point(alpha = 1, size = 1.7) +
scale_color_brewer(palette = "Paired") +
theme_dark() +
ggtitle("Rate of Deaths in Falling Injury Admissions") +
xlab("Number of Deaths") +
scale_x_continuous(limits = c(),
breaks = c(0, 10, 50, 100, 150, 200, 250, 300),
minor_breaks = waiver()) +
scale_y_log10(name = "Number Of Admissions (log10)",
limits = c(),
breaks = c(1, 10, 30, 100, 300, 1000, 10000)) +
facet_wrap(~sex, ncol = 0) +
coord_flip() +
guides(color=guide_legend(title = "Age Group"))
#The final, cleaned and organised first dataset is piped through the ggplot function, with appropriate x and y axis parameters set to display correlations
clean_full_ui %>%
ggplot(aes(x=injury_type, y=number_of_admissions, fill=age_group)) +
geom_bar(position = "dodge",
stat = "identity",
width = 0.8) +
scale_fill_brewer(palette = "Pastel1") +
theme_dark() +
gtitle("Number of Admissions by Injury Type") +
xlab("Injury Type") +
scale_y_log10(name = "Number Of Admissions (log10)",
limits = c(),
breaks = c(1, 10, 30, 100, 300, 1000, 10000)) +
facet_wrap(~sex, ncol =10) +
coord_flip() +
guides(fill=guide_legend(title = "Age Group"))
#The final, cleaned and organised first dataset is piped through the ggplot function, with appropriate x and y axis parameters set to display correlations
clean_full_ui %>%
ggplot(aes(x=injury_type, y=number_of_admissions, fill=age_group)) +
geom_bar(position = "dodge",
stat = "identity",
width = 0.8) +
scale_fill_brewer(palette = "Pastel1") +
theme_dark() +
ggtitle("Number of Admissions by Injury Type") +
xlab("Injury Type") +
scale_y_log10(name = "Number Of Admissions (log10)",
limits = c(),
breaks = c(1, 10, 30, 100, 300, 1000, 10000)) +
facet_wrap(~sex, ncol =10) +
coord_flip() +
guides(fill=guide_legend(title = "Age Group"))
library(tidyverse)
library(lubridate)
library(magrittr)
library(dplyr)
library(ggplot2)
library(janitor)
library(kableExtra)
library(scales)
library(viridis)
#Calling necessary packages for RMarkdown.
#getwd() if necessary
#setwd() if necessary
orig_hb = read.csv('hb_lookup.csv')
orig_uiadmissions = read.csv('ui_admissions_2022.csv')
orig_uideaths = read.csv('ui_deaths_2022.csv')
#CSV files read into RMarkdown and assigned to object names for ease of use.
glimpse(orig_hb)
#Rows: 18 Columns: 5, HB numbers + name(location in Scotland). Couple of "NA"s in column 4, consistent result in 3
glimpse(orig_uiadmissions)
#Rows: 391,104 Columns: 14, Breakdown of HB, Year, Age, Sex, Injurylocation, Injurytype, Number of Admissions | Data is raw and aggregated. Columns with repeated results (c(3,5,7,9,11,13)), all stating "QF". Summarised values in columns (c(6,8,10,12)), stating "All".
glimpse(orig_uideaths)
#Rows: 182,502 Columns: 14, Breakdown of HB, Year, Age, Sex, Injurylocation, Injurytype, Number of Deaths | Data is raw and aggregated
summary(orig_hb)
#HB and HBName will be required for joining datasets after cleaning.
summary(orig_uiadmissions)
#NumberOFAdmissions Max. 61279
#Column 1, Financial Year is a character string column not a integer: requires mutation.
#"HBR" column same as orig_hb co dataset column "HB"
summary(orig_uideaths)
#NumberofDeaths Max. 2759
#Initial observations of datasets show a few discrepancies
clean_full_ui_filtered %>%
ggplot(aes(x=numberof_deaths, y=number_of_admissions, color=age_group)) +
geom_point(alpha = 1, size = 1.7) +
scale_color_brewer(palette = "Paired") +
theme_dark() +
ggtitle("Rate of Deaths in Falling Injury Admissions") +
xlab("Number of Deaths") +
scale_x_continuous(limits = c(),
breaks = c(0, 10, 50, 100, 150, 200, 250, 300),
minor_breaks = waiver()) +
scale_y_log10(name = "Number Of Admissions (log10)",
limits = c(),
breaks = c(1, 10, 30, 100, 300, 1000, 10000)) +
facet_wrap(~sex, ncol = 0) +
coord_flip() +
guides(color=guide_legend(title = "Age Group"))
library(tidyverse)
library(lubridate)
library(magrittr)
library(dplyr)
library(ggplot2)
library(janitor)
library(kableExtra)
library(scales)
library(viridis)
#Calling necessary packages for RMarkdown.
#getwd() if necessary
#setwd() if necessary
orig_hb = read.csv('hb_lookup.csv')
orig_uiadmissions = read.csv('ui_admissions_2022.csv')
orig_uideaths = read.csv('ui_deaths_2022.csv')
#CSV files read into RMarkdown and assigned to object names for ease of use.
glimpse(orig_hb)
#Rows: 18 Columns: 5, HB numbers + name(location in Scotland). Couple of "NA"s in column 4, consistent result in 3
glimpse(orig_uiadmissions)
#Rows: 391,104 Columns: 14, Breakdown of HB, Year, Age, Sex, Injurylocation, Injurytype, Number of Admissions | Data is raw and aggregated. Columns with repeated results (c(3,5,7,9,11,13)), all stating "QF". Summarised values in columns (c(6,8,10,12)), stating "All".
glimpse(orig_uideaths)
#Rows: 182,502 Columns: 14, Breakdown of HB, Year, Age, Sex, Injurylocation, Injurytype, Number of Deaths | Data is raw and aggregated
summary(orig_hb)
#HB and HBName will be required for joining datasets after cleaning.
summary(orig_uiadmissions)
#NumberOFAdmissions Max. 61279
#Column 1, Financial Year is a character string column not a integer: requires mutation.
#"HBR" column same as orig_hb co dataset column "HB"
summary(orig_uideaths)
#NumberofDeaths Max. 2759
#Initial observations of datasets show a few discrepancies
cleaned_admission <- orig_uiadmissions %>%
select( FinancialYear,
HBR,
CA,
AgeGroup,
Sex,
InjuryType,
InjuryLocation,
NumberOfAdmissions) %>%
clean_names() %>%
separate(financial_year, into = c("year"), sep = "/") %>%
filter(injury_type != "All Diagnoses") %>%
filter(injury_location != "All") %>%
filter(age_group != "All") %>%
filter(sex != "All") %>%
mutate_at(c(1), as.numeric)
#orig_uiadmissions file assigned to new object after cleaning
#select() function used to organise and select relevant columns from dataset: info, demographic, Injury Info, No. Admissions
#clean_names() function used to make snake case, lower case names for uniformity
#separate() function to separate 2011/2012 and other values present in FinancialYear column into individual values
#filter() function used to omit (!=) information or highlight (==)
#mutate_at(c(1), as.numeric) to change character values to numeric ones as column represent "Financial Year"
cleaned_deaths <- orig_uideaths %>%
select( Year,
HBR,
CA,
AgeGroup,
Sex,
InjuryType,
InjuryLocation,
NumberofDeaths) %>%
clean_names() %>%
filter(injury_type != "All") %>%
filter(injury_location != "All") %>%
filter(age_group != "All") %>%
filter(sex != "All")
#Process from orig_uiadmissions repeated
cleaned_hb <- orig_hb %>%
select( HB,
HBName,
Country) %>%
clean_names()
#Process from orig_uiadmissions repeated
full_ui <- cleaned_admission %>%
full_join(cleaned_deaths)
#full_join() used to merge cleaned datasets together. full_join() keeps values from both, easier to observe inconsistencies.
full_ui_hb <- full_ui %>%
full_join(cleaned_hb, by = c("hbr" = "hb"))
#full_join() of final dataset. All datasets are now merged.
# glimpse(full_ui_hb)
# is.na(full_ui_hb)
clean_full_ui <- full_ui_hb %>%
select(year,
hbr,
hb_name,
age_group,
sex,
injury_type,
injury_location,
number_of_admissions,
numberof_deaths) %>%
mutate(numberof_deaths = coalesce(numberof_deaths, 0)) %>%
mutate(across(c(injury_location), na_if, "Not Applicable")) %>%
mutate(across(c(number_of_admissions), na_if, 0)) %>%
na.omit(clean_full_ui) %>%
group_by(sex, age_group, injury_type, injury_location, year, hbr, hb_name) %>%
summarise(across(c(number_of_admissions, numberof_deaths), sum)) %>%
arrange(age_group) %>%
distinct() %>%
rowid_to_column()
#Tidying and wrangling process to make data set ready for plotting.
#Final modifications and wrangling of data.
#Selected relevant columns for data plotting.
#Mutate() and Coalesce() to change NA values in numberof_deaths column to numerical value 0.
#Mutate(), across() to na_if functions to change character value "Not Applicable" to null value in column 7.
#na.omit() to omit null, incomplete data present in merged data.
#rowid_to_column() to number each line of the dataset for navigation and ease of use by adding integer column.
#group_by() to organise the columns into a desired order
#This dataset is cleaned and ready to be observed for any last discrepancies before being plotted.
clean_full_ui$age_group <- factor(clean_full_ui$age_group, levels=c("0-4 years", "5-9 years", "10-14 years", "15-24 years", "25-44 years", "45-64 years", "65-74 years", "75plus years"))
clean_full_ui_filtered <- clean_full_ui %>%
filter(injury_type == "Falls")
#Final reorganizing of data as some discrepancies were observed in previous observations, including incorrect ordering of age groups, this has been fixed by manually assign the order of the age groups by the factor() function.
#The final filtered data set is made after the penultimate data observations in the first graph have shown that "Falls" have the highest rate of admission. This filtered data set will be used to determine the rate of death in the final plot of this report.
#The final, cleaned and organised first dataset is piped through the ggplot function, with appropriate x and y axis parameters set to display correlations
clean_full_ui %>%
ggplot(aes(x=injury_type, y=number_of_admissions, fill=age_group)) +
geom_bar(position = "dodge",
stat = "identity",
width = 0.8) +
scale_fill_brewer(palette = "Pastel1") +
theme_dark() +
ggtitle("Number of Admissions by Injury Type") +
xlab("Injury Type") +
scale_y_log10(name = "Number Of Admissions (log10)",
limits = c(),
breaks = c(1, 10, 30, 100, 300, 1000, 10000)) +
facet_wrap(~sex, ncol =10) +
coord_flip() +
guides(fill=guide_legend(title = "Age Group"))