-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path12 Raincloud chart AE Attendances.R
More file actions
161 lines (126 loc) · 4.42 KB
/
12 Raincloud chart AE Attendances.R
File metadata and controls
161 lines (126 loc) · 4.42 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
# 12 Raincloud chart AE Attendances
# Reference: ggdist: Make a Raincloud Plot to Visualize Distribution in ggplot2 | R-bloggers
# https://www.r-bloggers.com/2021/07/ggdist-make-a-raincloud-plot-to-visualize-distribution-in-ggplot2/
# Load required packages at once (readxl,here,dplyr,janitor)
pacman::p_load(readxl,here,tidyverse,janitor)
# Load England AE Attendances
# Import (Type 1, Type 2 and Type 3 AE Attendances)
AE_ATT <- read_excel(here("data","AE_England_data.xls"),
sheet = 1,skip =17, range = "C18:G123",na = "") %>%
clean_names()
AE_ATT
names(AE_ATT)
# [1] "period"
# [2] "type_1_departments_major_a_e"
# [3] "type_2_departments_single_specialty"
# [4] "type_3_departments_other_a_e_minor_injury_unit"
# [5] "total_attendances"
AE_ATT <- AE_ATT %>%
select(
Period = period,
Major_att = type_1_departments_major_a_e ,
Single_spec_att = type_2_departments_single_specialty,
Other_att = type_3_departments_other_a_e_minor_injury_unit
)
AE_ATT
# Create variable for year
AE_ATT <- AE_ATT %>%
mutate(
year = as.numeric(format(Period, "%Y"))
)
AE_ATT
AE_ATT_arrange <- AE_ATT %>%
select(Period,year,Major_att,Single_spec_att,Other_att)
AE_ATT_arrange
# Prep.1 Pivot long the initial data set
AE_ATT_long<- AE_ATT_arrange %>%
pivot_longer(names_to = "Metrics",
cols = 3:ncol(AE_ATT_arrange))
AE_ATT_long
# Prep.2 Group value by year and Metric
AE_ATT_long_year <- AE_ATT_long %>%
select(year,Metrics,value) %>%
group_by(year,Metrics) %>%
summarise(Value = sum(value))
AE_ATT_long_year
## Start building raincloud plot
## 1. MAJOR ATTENDANCES RAINCLOUD
# Filter data to account for 2010-2014 years
# 1.1 Create boilerplate chart for value by period split by Metrics
# Include all years in the visualization
AE_ATT_sel_major <- AE_ATT_long %>% select(year,Metrics,value)
AE_ATT_sel_major
names(AE_ATT_sel_major)
# Include all years in the visualization
# Subset data for 2010-2014 period
AE_ATT_period <- AE_ATT_long %>% select(year,Metrics,value) %>%
filter(Metrics == "Major_att" &
(year == 2010 | year == 2011 |year == 2012 | year == 2013 )
) %>%
select(year,value)
AE_ATT_period
table(AE_ATT_period$year)
# 1.2 Create Raincloud visualization for 2010=2014 period
MAJOR_ATT <- ggplot(AE_ATT_period,aes(x = factor(year), y = value,
fill = factor(year))) +
# Include half-violin from {ggdist} package
# Function: stat_halfeye() from ggdist package
ggdist::stat_halfeye(
adjust = 0.5,
justification = -.2,
.width = 0,
point_colour = NA
) +
# Function: geom_boxplot() from ggplot package
geom_boxplot(
width = .12,
outlier.color = NA,
alpha = 0.5
) +
# DOTS chart. Function: stat_dots() from ggdis package
ggdist::stat_dots(
side = "left", # left orientation
justification = 1.1,
binwidth = .25
)
MAJOR_ATT
# 2. Improve plot layout feel and look
# functions: scale_fill_tq(), theme_tq()
# Improve theme and plot layout
library(tidyquant)
MAJOR_ATT_layout <- ggplot(AE_ATT_period,aes(x = factor(year), y = value,
fill = factor(year))) +
# Include half-violin from {ggdist} package
# Function: stat_halfeye() from ggdist package
ggdist::stat_halfeye(
adjust = 0.5,
justification = -.2,
.width = 0,
point_colour = NA
) +
# Function: geom_boxplot() from ggplot package to be used
geom_boxplot(
width = .12,
outlier.color = NA,
alpha = 0.5
) +
# DOTS chart. Function: stat_dots() from ggdis package
ggdist::stat_dots(
side = "left", # left orientation
justification = 1.1,
binwidth = .25
) +
# Improve theme and plot layout
# library(tidyquant)
scale_fill_tq() +
theme_tq() + # Apply theme
labs (
title = "Major A&E Attendances. England 2010-2013",
subtitle = "Raincloud plot example",
x = "Years",
y = "Major Attendances",
fill = "Years"
) +
coord_flip() ## Flip coordinates
MAJOR_ATT_layout
ggsave("plots/24_AE_Attendances_Raincloud_chart.png", width = 6, height = 4)