-
Notifications
You must be signed in to change notification settings - Fork 0
/
PHS_NASSS_Cardiac_WebPlotDigitizerGraphs.R
336 lines (309 loc) · 13.6 KB
/
PHS_NASSS_Cardiac_WebPlotDigitizerGraphs.R
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
setwd(GraphsDirectory)
####################
###########################################################
# Graph WebPlotDigitizer data extracted from NASSS graphs #
###########################################################
GraphSubtitle <- "Graph by @bouncingkitten | https://www.drowningindata.blog"
# Custom date formatting to get JFMAMJ... labels
# http://www.jasonfabris.com/blog/dteformat/
dte_formatter <- function(x) {
#formatter for axis labels: J, F, M, etc...
mth <- substr(format(x, "%b"),1,1)
mth
}
# Create labels and positions for vertical lines which mark the start of each year
vlines.cardiac.calls <- data.frame(Date = c(as.Date("2019-01-01"),
as.Date("2020-01-01"),
as.Date("2021-01-01"),
as.Date("2022-01-01"),
as.Date("2023-01-01")),
Year = c(2019,2020,2021,2022,2023),
Type = rep("SevenDayAverage",5),
Calls = rep(10,5))
cardiac.linetypes <- c("Baseline" = "dashed",
"SevenDayAverage" = "solid")
cardiac.labels <- c("Baseline" = "Baseline",
"SevenDayAverage" = "7 day average")
cardiac.colours <- c("Baseline" = "grey50",
"SevenDayAverage" = "black")
# Graph cardiac calls (all years)
cardiac.calls.graph.basic <- ggplot(data = cardiac.calls,
aes(x = Date,
y = Calls,
colour = Type,
linetype = Type)
)+
ggtitle("National Ambulance Syndromic Surveillance System: England\nCardiac/respiratory arrest calls ",
subtitle = GraphSubtitle)+
labs(caption = "Graph source: UK Health Security Agency/Public Health England")+
theme_tufte()+
theme(text = element_text(family=""),
plot.caption = element_text(hjust = 0),
panel.background = element_rect(fill = 'white', color = 'white'),
plot.background = element_rect(fill = 'white', color = 'white'),
legend.position="bottom")+
scale_x_date(name = "Date",
breaks = "1 month",
labels = dte_formatter,
expand = c(0.03,0.03))+
scale_y_continuous(name = "Calls",
labels = label_comma(accuracy = 1),
limits = c(0,500),
expand = c(0.06,0))
cardiac.calls.graph.original <- cardiac.calls.graph.basic +
geom_vline(data = vlines.cardiac.calls,
aes(xintercept = Date),
colour = "grey75",
show.legend = FALSE)+
geom_text(data = vlines.cardiac.calls,
aes(label = Year,
x = Date,
y = Calls),
nudge_x = 15,
nudge_y = 5,
colour="grey25",
angle = 90,
show.legend = FALSE)+
geom_line(show.legend = TRUE,
size = 0.65,
na.rm = TRUE)+
scale_linetype_manual(name = "Call type",
values = cardiac.linetypes,
labels = cardiac.labels)+
scale_colour_manual(name = "Call type",
values = cardiac.colours,
labels = cardiac.labels)
ggsave(cardiac.calls.graph.original,filename = paste(GraphFileNameRoot," NASSS Cardiac Calls (original y range).png",sep=""),
device = png,
units = "px",
width = 2300,
height = 1600,
bg = "white"
)
# Regraph with closer y axis to show more detail
vlines.cardiac.calls.high <- data.frame(Date = c(as.Date("2019-01-01"),
as.Date("2020-01-01"),
as.Date("2021-01-01"),
as.Date("2022-01-01"),
as.Date("2023-01-01")),
Year = c(2019,2020,2021,2022,2023),
Type = rep("SevenDayAverage",5),
Calls = rep(220,5))
cardiac.calls.graph.closer <- cardiac.calls.graph.basic +
scale_y_continuous(name = "Calls",
labels = label_comma(accuracy = 1),
limits = c(NA,NA),
breaks = c(250,300,350,400,450,500),
expand = c(0.06,0))+
geom_vline(data = vlines.cardiac.calls.high,
aes(xintercept = Date),
colour = "grey75",
show.legend = FALSE)+
geom_text(data = vlines.cardiac.calls.high,
aes(label = Year,
x = Date,
y = Calls),
nudge_x = 15,
nudge_y = 5,
colour="grey25",
angle = 90,
show.legend = FALSE)+
geom_line(show.legend = TRUE,
size = 0.65,
na.rm = TRUE)+
scale_linetype_manual(name = "Call type",
values = cardiac.linetypes,
labels = cardiac.labels)+
scale_colour_manual(name = "Call type",
values = cardiac.colours,
labels = cardiac.labels)
ggsave(cardiac.calls.graph.closer,filename = paste(GraphFileNameRoot," NASSS Cardiac Calls (reduced y range).png",sep=""),
device = png,
units = "px",
width = 2300,
height = 1600,
bg = "white"
)
######################################################################################
# Graph WebPlotDigitizer data extracted from NASSS graphs for each report separately #
######################################################################################
cardiac.calls.report.graphs.separate <- ggplot(data = cardiac.calls.report.graphs,
aes(x = Date,
y = Calls,
colour = Type,
linetype = Type,
group = Type)
)+
ggtitle("National Ambulance Syndromic Surveillance System: England\nCardiac/respiratory arrest calls ",
subtitle = GraphSubtitle)+
labs(caption = "Graph source: UK Health Security Agency/Public Health England")+
theme_tufte()+
theme(text = element_text(family=""),
plot.caption = element_text(hjust = 0),
panel.background = element_rect(fill = 'white', color = 'white'),
plot.background = element_rect(fill = 'white', color = 'white'),
legend.position="bottom")+
scale_x_date(name = "Date",
breaks = "1 month",
labels = dte_formatter,
expand = c(0.03,0.03))+
scale_y_continuous(name = "Calls",
labels = label_comma(accuracy = 1),
limits = c(0,500),
expand = c(0.06,0))+
geom_vline(data = vlines.cardiac.calls.high,
aes(xintercept = Date),
colour = "grey75",
show.legend = FALSE)+
geom_text(data = vlines.cardiac.calls.high,
aes(label = Year,
x = Date,
y = Calls),
nudge_x = 15,
nudge_y = 5,
colour="grey25",
angle = 90,
show.legend = FALSE)+
geom_line(show.legend = TRUE,
size = 0.65,
na.rm = TRUE)+
scale_linetype_manual(name = "Call type",
values = cardiac.linetypes,
labels = cardiac.labels)+
scale_colour_manual(name = "Call type",
values = cardiac.colours,
labels = cardiac.labels)+
facet_grid(rows = vars(Report),scales = "free_y")
ggsave(cardiac.calls.report.graphs.separate,filename = paste(GraphFileNameRoot," NASSS Cardiac Calls (separates).png",sep=""),
device = png,
units = "px",
width = 2300,
height = 1600,
bg = "white"
)
################################################################################################
# Graph WebPlotDigitizer data extracted from NASSS graphs for each report in different colours #
################################################################################################
cardiac.calls.report.graphs.separate.colours <- ggplot(data = cardiac.calls.report.graphs,
aes(x = Date,
y = Calls,
colour = Report,
linetype = Type)
)+
ggtitle("National Ambulance Syndromic Surveillance System: England\nCardiac/respiratory arrest calls ",
subtitle = GraphSubtitle)+
labs(caption = "Graph source: UK Health Security Agency/Public Health England")+
theme_tufte()+
theme(text = element_text(family=""),
plot.caption = element_text(hjust = 0),
panel.background = element_rect(fill = 'white', color = 'white'),
plot.background = element_rect(fill = 'white', color = 'white'),
legend.position="bottom")+
scale_x_date(name = "Date",
breaks = "1 month",
labels = dte_formatter,
expand = c(0.03,0.03))+
scale_y_continuous(name = "Calls",
labels = label_comma(accuracy = 1),
#limits = c(0,500),
expand = c(0.06,0))+
geom_vline(data = vlines.cardiac.calls.high,
aes(xintercept = Date),
colour = "grey75",
show.legend = FALSE)+
geom_text(data = vlines.cardiac.calls.high,
aes(label = Year,
x = Date,
y = Calls),
nudge_x = 15,
nudge_y = 20,
colour="grey25",
angle = 90,
show.legend = FALSE)+
geom_line(show.legend = TRUE,
size = 0.65,
na.rm = TRUE)+
scale_linetype_manual(name = "Call type",
values = cardiac.linetypes,
labels = cardiac.labels)#+
# scale_colour_manual(name = "Call type",
# values = cardiac.colours,
# labels = cardiac.labels)
ggsave(cardiac.calls.report.graphs.separate.colours,filename = paste(GraphFileNameRoot," NASSS Cardiac Calls (separate colours).png",sep=""),
device = png,
units = "px",
width = 2300,
height = 1600,
bg = "white"
)
################################################################################################
# Graph WebPlotDigitizer data extracted from NASSS graphs for each report with coloured points #
################################################################################################
cardiac.calls.report.graphs.points <- ggplot(data = cardiac.calls.report.graphs,
aes(x = Date,
y = Calls,
colour = Report,
shape = Type)
)+
ggtitle("National Ambulance Syndromic Surveillance System: England\nCardiac/respiratory arrest calls ",
subtitle = GraphSubtitle)+
labs(caption = "The coloured points were read directly from the graphs using WebPlotDigitizer. The grey points show interpolated data.\n\nGraphs source: UK Health Security Agency/Public Health England")+
theme_tufte()+
theme(text = element_text(family=""),
plot.caption = element_text(hjust = 0),
panel.background = element_rect(fill = 'white', color = 'white'),
plot.background = element_rect(fill = 'white', color = 'white'),
legend.position="bottom")+
scale_x_date(name = "Date",
breaks = "1 month",
labels = dte_formatter,
expand = c(0.03,0.03))+
scale_y_continuous(name = "Calls",
labels = label_comma(accuracy = 1),
#limits = c(0,500),
expand = c(0.06,0))+
geom_vline(data = vlines.cardiac.calls.high,
aes(xintercept = Date),
colour = "grey75",
show.legend = FALSE)+
geom_text(data = vlines.cardiac.calls.high,
aes(label = Year,
x = Date,
y = Calls),
nudge_x = 15,
nudge_y = 20,
colour="grey25",
angle = 90,
show.legend = FALSE)+
geom_point(data = cardiac.calls,
aes(x = Date,
y = Calls,
shape = Type),
#shape = 1,
size = 0.5,
fill = "grey50",
colour = "grey50")+
geom_point(show.legend = TRUE,
size = 0.5,
na.rm = TRUE)+
geom_line(show.legend = TRUE,
size = 0.15,
alpha = 0.5,
colour = "grey50",
na.rm = TRUE)#+
# scale_linetype_manual(name = "Call type",
# values = cardiac.linetypes,
# labels = cardiac.labels)#+
# scale_colour_manual(name = "Call type",
# values = cardiac.colours,
# labels = cardiac.labels)
ggsave(cardiac.calls.report.graphs.points,filename = paste(GraphFileNameRoot," NASSS Cardiac Calls (points).png",sep=""),
device = png,
units = "px",
width = 2300,
height = 1600,
bg = "white"
)
####################
####################
setwd(RootDirectory)