-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdisability-figures.R
More file actions
272 lines (242 loc) · 12.3 KB
/
disability-figures.R
File metadata and controls
272 lines (242 loc) · 12.3 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
# Set-up
library(tidyverse)
# Parameters
point.size <- 6
font.family <- "Aptos Narrow"
font.size <- 10
line.width <- 0.2
line.color <- "black"
text.color <- "black"
# -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
# Figure 1
# -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
# Reading dataset - disability and difficulties
data.disability <- readxl::read_xlsx("raw_data/Monograph template - tables for graphs.xlsx", sheet = "Disability and difficulties") |>
mutate(theme = factor(theme, levels = unique(theme), labels = toupper(unique(theme))),
indicator = factor(indicator, levels = rev(indicator)))
# Themes
data.disability.themes <- data.disability |>
group_by(theme) |>
summarise(n = n())
# Creating the bar plot - Disability and difficulties
ggplot(data.disability) +
geom_col(aes(x = percentage, y = indicator), width = 0.6, fill = "#64C9D8") +
geom_text(aes(x = percentage + 1, y = indicator, label = paste0(sprintf(percentage, fmt = '%#.1f'), "%")),
size = font.size/.pt, color = "black", family = font.family, hjust = 0) +
# Facets
ggforce::facet_col(facets = vars(theme), scales = "free_y", space = "free") +
# Y axis text
geom_text(data = data.disability.themes, aes(x = -42, y = n + 1, label = theme),
size = font.size/.pt, color = "black", family = font.family, hjust = 0, fontface = "bold") +
geom_text(aes(x = -42, y = indicator, label = indicator),
size = font.size/.pt, color = "black", family = font.family, hjust = 0) +
# Number of people
geom_rect(data = data.disability.themes, aes(xmin = 101, xmax = 114, ymin = -Inf, ymax = n + 0.5),
fill = "gray93") +
geom_text(data = data.disability |> slice(1), x = 113, y = 2, label = "People",
size = font.size/.pt, color = "black", family = font.family, hjust = 1, fontface = "bold") +
geom_text(data = data.disability, aes(x = 113, y = indicator, label = scales::comma(n)),
size = font.size/.pt, color = "black", family = font.family, hjust = 1) +
# Cosmetic lines bellow strips
geom_segment(data = data.disability.themes, aes(x = -42, xend = 114, y = n + 0.5),
linewidth = line.width, color = "#64C9D8") +
# Scales
scale_x_continuous(breaks = seq(0,100,20), expand = expansion(add = 0)) +
coord_cartesian(clip = "off", xlim = c(0,100)) +
theme(# Axis:
axis.ticks.y = element_blank(),
axis.ticks.x = element_line(color = line.color, linewidth = line.width),
axis.line.x = element_line(color = line.color, linewidth = line.width),
axis.text.x = element_text(size = font.size, family = font.family, color = text.color),
axis.text.y = element_blank(),
axis.title.y = element_blank(),
axis.title.x = element_text(size = font.size, family = font.family, color = text.color),
# Legend:
legend.text = element_text(size = font.size, family = font.family, color = text.color),
legend.title = element_blank(),
legend.position = "bottom",
# Strip:
strip.text = element_blank(),
strip.background = element_blank(),
# Panel:
panel.background = element_blank(),
panel.grid = element_blank(),
panel.spacing.y = unit(20, "pt"),
# Plot
plot.margin = margin(10,40,5,118)) +
labs(x = "Percentage of the population aged 5 years or older")
# Saving the figure for report
ggsave("output/figure_1.pdf", height = 4, width = 6, device = cairo_pdf)
# -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
# Figure 2
# -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
# Reading dataset - life outcomes
data.outcomes <- readxl::read_xlsx("raw_data/Monograph template - tables for graphs.xlsx", sheet = "Life outcomes") |>
mutate(theme = factor(theme, levels = unique(theme), labels = toupper(unique(theme))),
indicator = factor(indicator, levels = rev(indicator)))
# Long format
data.outcomes.long <- data.outcomes |>
pivot_longer(4:5, names_to = "disability", values_to = "percentage")
# Themes
data.outcomes.themes <- data.outcomes |>
group_by(theme) |>
summarise(n = n())
# Creating the equiplot - Life outcomes
ggplot(data.outcomes.long) +
# Equiplot lines and points
geom_line(aes(x = percentage, y = indicator, group = indicator),
linewidth = point.size, color = "gray93") +
geom_point(data = data.outcomes, aes(x = nodisability, y = indicator, color = "Persons without disability"),
stroke = 0, size = point.size) +
geom_text(data = data.outcomes, aes(x = nodisability, y = indicator, label = round(nodisability)),
size = 9/.pt, color = "white", family = font.family) +
geom_point(data = data.outcomes, aes(x = disability, y = indicator, color = "Persons with disability"),
stroke = 0, size = point.size) +
geom_text(data = data.outcomes, aes(x = disability, y = indicator, label = round(disability)),
size = 9/.pt, color = "black", family = font.family) +
# Y axis text
geom_text(data = data.outcomes.themes, aes(x = -42, y = n + 1, label = theme),
size = font.size/.pt, color = "black", family = font.family, hjust = 0, fontface = "bold") +
geom_text(data = data.outcomes, aes(x = -42, y = indicator, label = indicator),
size = font.size/.pt, color = "black", family = font.family, hjust = 0) +
# Age
geom_rect(data = data.outcomes.themes, aes(xmin = 101, xmax = 111, ymin = -Inf, ymax = n + 0.5),
fill = "gray93") +
geom_text(data = data.outcomes |> slice(1), x = 106, y = 9, label = "Age",
size = font.size/.pt, color = "black", family = font.family, hjust = 0.5, fontface = "bold") +
geom_text(data = data.outcomes, aes(x = 106, y = indicator, label = age),
size = font.size/.pt, color = "black", family = font.family, hjust = 0.5) +
# Cosmetic lines bellow strips
geom_segment(data = data.outcomes.themes, aes(x = -42, xend = 111, y = n + 0.5),
linewidth = line.width, color = "#64C9D8") +
# Facets
ggforce::facet_col(facets = vars(theme), scales = "free_y", space = "free") +
# Scales
scale_x_continuous(breaks = seq(0,100,20), expand = expansion(add = 0)) +
scale_color_manual(values = c("Persons with disability" = "#64C9D8",
"Persons without disability" = "#004EC5")) +
coord_cartesian(clip = "off", xlim = c(0,100)) +
theme(# Axis:
axis.ticks.y = element_blank(),
axis.ticks.x = element_line(color = line.color, linewidth = line.width),
axis.line.x = element_line(color = line.color, linewidth = line.width),
axis.text.x = element_text(size = font.size, family = font.family, color = text.color),
axis.text.y = element_blank(),
axis.title.y = element_blank(),
axis.title.x = element_text(size = font.size, family = font.family, color = text.color),
# Legend:
legend.text = element_text(size = font.size, family = font.family, color = text.color),
legend.title = element_blank(),
legend.position = "bottom",
# Strip:
strip.text = element_blank(),
strip.background = element_blank(),
# Panel:
panel.background = element_blank(),
panel.grid = element_blank(),
panel.spacing.y = unit(20, "pt"),
# Plot
plot.margin = margin(10,33,5,120)) +
labs(x = "Percentage of the population")
# Saving the figure for report
ggsave("output/figure_2.pdf", height = 9, width = 6, device = cairo_pdf)
# -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
# Figure 3
# -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
# Reading dataset - disability and age
data.age <- readxl::read_xlsx("raw_data/Monograph template - tables for graphs.xlsx", sheet = "Disability and age") |>
mutate(age = factor(age, levels = unique(age))) |>
pivot_longer(2:4, names_to = "group", values_to = "percentage") |>
mutate(group = factor(group, levels = c("total", "female", "male"),
labels = toupper(c("Total population", "Women", "Men"))))
# Creating the bar plot - disability and age
ggplot(data.age |> filter(age != "National")) +
aes(x = age, y = percentage, fill = group) +
geom_col(width = 0.5, show.legend = FALSE) +
geom_hline(data = data.age |> filter(age == "National"),
aes(yintercept = percentage, color = group),
linewidth = line.width*.stroke/.pt, linetype = "dashed", show.legend = FALSE) +
geom_text(data = data.age |> filter(age == "National"),
aes(y = percentage + 2, x = 0.7,
label = paste0("National prevalence: ", sprintf(percentage, fmt = '%#.1f'), "%")),
size = font.size/.pt, color = "black", family = font.family, vjust = 0, hjust = 0) +
geom_text(aes(y = percentage + 1, label = sprintf(percentage, fmt = '%#.1f')),
size = 9/.pt, color = "black", family = font.family, vjust = 0) +
# x Axis
geom_segment(aes(x = -Inf, xend = Inf, y = 0),
linewidth = line.width, color = line.color) +
# Facets
ggforce::facet_col(facets = vars(group)) +
# Scales
scale_fill_manual(values = c("gray50", "#64C9D8", "#004EC5")) +
scale_color_manual(values = c("gray50", "#64C9D8", "#004EC5")) +
scale_y_continuous(breaks = seq(0,60,15), expand = expansion(add = 0), limits = c(0,60)) +
scale_x_discrete(expand = expansion(add = c(0.4, 0.25))) +
coord_cartesian(clip = "off") +
# Theme
theme(# Axis:
axis.ticks = element_line(color = line.color, linewidth = line.width),
axis.line = element_line(color = line.color, linewidth = line.width),
axis.text = element_text(size = font.size, family = font.family, color = text.color),
axis.title = element_text(size = font.size, family = font.family, color = text.color),
# Strip:
strip.text = element_text(size = font.size, family = font.family, color = text.color, face = "bold"),
strip.background = element_blank(),
# Panel:
panel.background = element_blank(),
panel.grid = element_blank(),
panel.spacing.y = unit(15, "pt")) +
labs(x = "Age group (years)",
y = "Prevalence of disability")
# Saving the figure for report
ggsave("output/figure_3.pdf", height = 6, width = 6, device = cairo_pdf)
# -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
# Figure 4
# -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
# Reading dataset - disability and age
data.school <- readxl::read_xlsx("raw_data/Monograph template - tables for graphs.xlsx", sheet = "School attendance")
gap.y <- (sum(data.school$nodisability[data.school$age %in% 11:12]) + sum(data.school$disability[data.school$age %in% 11:12]))/4
# Creating the bar plot - disability and school attendance
ggplot(data.school) +
aes(x = age) +
geom_ribbon(aes(ymin = disability, ymax = nodisability),
fill = "gray80", alpha = 0.5) +
# Attendance gap
annotate("text", x = 11.5, y = gap.y,
label = "ATTENDANCE GAP", hjust = 0.5, fontface = "bold",
size = font.size/.pt, color = "black", family = font.family) +
# Lines
geom_line(aes(y = nodisability), color = "#004EC5", linewidth = 0.6) +
annotate("text", x = 11.5, y = 105,
label = "Children without disabilities", hjust = 0.5,
size = font.size/.pt, color = "#004EC5", family = font.family) +
geom_line(aes(y = disability), color = "#64C9D8", linewidth = 0.6) +
annotate("text", x = 11.5, y = 55,
label = "Children with disabilities", hjust = 0.5,
size = font.size/.pt, color = "#00818F", family = font.family) +
# Scales
scale_x_continuous(breaks = 5:18, expand = expansion(add = 0)) +
scale_y_continuous(breaks = seq(0,100,20), expand = expansion(add = 0)) +
coord_cartesian(clip = "off", xlim = c(5,18), ylim = c(0,100)) +
# Theme
theme(# Axis:
axis.ticks.x = element_line(color = line.color, linewidth = line.width),
axis.ticks.y = element_blank(),
axis.line.x = element_line(color = line.color, linewidth = line.width),
axis.line.y = element_blank(),
axis.text = element_text(size = font.size, family = font.family, color = text.color),
axis.title = element_text(size = font.size, family = font.family, color = text.color),
# Legend:
legend.text = element_text(size = font.size, family = font.family, color = text.color),
legend.title = element_blank(),
legend.position = "bottom",
# Panel:
panel.background = element_blank(),
panel.grid.major.x = element_blank(),
panel.grid.major.y = element_line(color = "gray", linewidth = line.width),
panel.grid.minor = element_blank(),
# Plot
plot.margin = margin(15,5,5,5)) +
labs(x = "Age (years)", y = "Percentage of children currently attending school")
# Saving the figure for report
ggsave("output/figure_4.pdf", height = 3, width = 6, device = cairo_pdf)