-
Notifications
You must be signed in to change notification settings - Fork 1
/
plotting.R
2613 lines (2306 loc) · 81.2 KB
/
plotting.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
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
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
###########################################################
# PLOTTING
#
# All plotting functionality in one place.
#
###########################################################
# **** Data visualisation ****
# ---------------------------------------------------------
# Plot methodology figure to be used in paper
# ---------------------------------------------------------
plot_scope = function() {
message(" - Plotting country-disease scope")
# Manually set tidy y axis limit
y_max = 8 # In billions
# Linear time interpolation for less pixilated figures
smoothness = 10 # Higher value for smoother plot
# Dictionary for full impact source descriptions
impact_dict = c(
extern = "Transmission modelling (Form 1)",
vimc = "Transmission modelling (Form 2)",
static = "Static modelling (Form 3)",
impute = "Geographic imputation model",
extrap = "Temporal extrapolation model")
# Associated colours
impact_colours = c("#EB7D5B", "#FED23F", "#B5D33D", "#6CA2EA", "#442288")
# Alter figure dimensions
save_height = 18
# ---- Number of FVPs by pathogen ----
# Number of FVPs by country
fvps_dt = table("coverage") %>%
filter(coverage > 0) %>%
# Append disease details...
left_join(y = table("d_v_a"),
by = "d_v_a_id") %>%
# Concept here is FVP, so remove birth dose & boosters...
filter(!grepl("_(bd|bx|px)$", vaccine)) %>%
# Summarise over age...
group_by(disease, country, year) %>%
summarise(fvps = sum(fvps)) %>%
ungroup() %>%
as.data.table()
# Total cumulative FVPs per disease
total_dt = fvps_dt %>%
# Total by disease...
group_by(disease, year) %>%
summarise(total = sum(fvps)) %>%
ungroup() %>%
# Cumulative over time...
group_by(disease) %>%
mutate(cum_total = cumsum(total)) %>%
ungroup() %>%
as.data.table()
# ---- Source of impact estimates ----
# Static model settings
static_dt = table("gbd_estimates") %>%
select(disease, country, year) %>%
unique() %>%
filter(year %in% o$gbd_estimate_years) %>%
mutate(class = "static")
# External model settings
extern_dt = table("extern_estimates") %>%
left_join(y = table("d_v_a"),
by = "d_v_a_id") %>%
select(disease, country, year) %>%
unique() %>%
mutate(class = "extern")
# VIMC settings
vimc_dt = table("vimc_estimates") %>%
left_join(y = table("d_v_a"),
by = "d_v_a_id") %>%
select(disease, country, year) %>%
unique() %>%
mutate(class = "vimc")
# VIMC country imputation settings
impute_dt = table("d_v_a") %>%
filter(source == "vimc") %>%
select(disease) %>%
unique() %>%
expand_grid(
country = all_countries(),
year = unique(vimc_dt$year)) %>%
left_join(y = vimc_dt,
by = c("disease", "country", "year")) %>%
filter(is.na(class)) %>%
mutate(class = "impute") %>%
as.data.table()
# ---- Construct plotting datatable ----
# Combine all impact sources
plot_dt = rbind(extern_dt, static_dt, vimc_dt, impute_dt) %>%
# Append FVPs...
right_join(y = fvps_dt,
by = c("disease", "country", "year")) %>%
# Anything not yet specified is time-exrapolated...
mutate(class = ifelse(is.na(class) & year <= 2000, "extrap.1", class),
class = ifelse(is.na(class) & year > 2000, "extrap.2", class)) %>%
# Summarise over all countries...
group_by(disease, class, year) %>%
summarise(fvps = sum(fvps)) %>%
ungroup() %>%
# Append cumulative total...
left_join(y = total_dt,
by = c("disease", "year")) %>%
# Share of cumulative FVPs by class over time...
mutate(share = fvps / total,
value = share * cum_total / 1e9) %>%
# Expand for a more granular timescale...
expand_grid(time = seq(
from = 0 ,
to = 1 - 1 / smoothness,
by = 1 / smoothness)) %>%
mutate(time = year + time) %>%
filter(time <= max(o$years)) %>%
# Interpolate annual values for smoother plot...
mutate(value = ifelse(time == year, value, NA)) %>%
group_by(disease, class) %>%
mutate(value = na_interpolation(value)) %>%
ungroup() %>%
# Use full disease names...
left_join(y = table("disease_name"),
by = "disease") %>%
mutate(class = str_remove(class, "\\.[1-9]+$")) %>%
select(disease = disease_name, class, time, value) %>%
# Use impact source descriptions...
mutate(class = recode(class, !!!impact_dict),
class = factor(class, rev(impact_dict))) %>%
# Set disease order...
arrange(desc(value)) %>%
mutate(disease = fct_inorder(disease)) %>%
# Tidy up...
arrange(disease, class, time) %>%
as.data.table()
# ---- Construct label datatable ----
# Year range of analysis
year1 = min(o$years)
year2 = max(o$years)
# Label description string
label = paste0("Total (", year1, "-", year2, "):")
# Construct labels: total FVPs over analysis timeframe
label_dt = plot_dt %>%
filter(time == year2) %>%
# Total cumulative FVPs by disease...
group_by(disease) %>%
summarise(total = sum(value)) %>%
ungroup() %>%
# Construct labels...
mutate(total = round(total, 2),
label = paste(label, total, "billion")) %>%
# Set coordinates...
mutate(time = year1 + 0.01 * (year2 - year1),
value = y_max * 0.9) %>%
as.data.table()
# ---- Produce plot ----
# Plot FVP over time per pathogen and impact source
g = ggplot(plot_dt) +
aes(x = time,
y = value) +
geom_bar(
mapping = aes(fill = class),
stat = "identity",
position = "stack",
width = 1 / smoothness) +
# Add total labels...
geom_text(
data = label_dt,
mapping = aes(label = label),
size = 4.5,
hjust = 0,
vjust = 1) +
# Some intricate faceting...
facet_rep_wrap(
facets = vars(disease),
ncol = 1,
labeller = label_wrap_gen(width = 20),
strip.position = "right",
repeat.tick.labels = FALSE) +
# Set colours and legend title...
scale_fill_manual(
values = impact_colours,
name = "Source of impact estimates") +
guides(fill = guide_legend(
reverse = TRUE,
byrow = TRUE,
nrow = 2)) +
# Prettify x axis...
scale_x_continuous(
limits = c(year1 - 1 / smoothness,
year2 + 1 / smoothness),
expand = expansion(mult = c(0, 0)),
breaks = seq(year1, year2, by = 5)) +
# Prettify y axis...
scale_y_continuous(
name = paste("Cumulative number of people receiving",
"final primary dose (in billions)"),
limits = c(0, y_max),
breaks = seq(2, y_max, by = 2),
labels = comma,
expand = expansion(mult = c(0, 0)))
# Prettify theme
g = g + theme_classic() +
theme(strip.text = element_text(size = 14),
strip.text.y = element_text(angle = 0, hjust = 0),
strip.background = element_blank(),
axis.title.x = element_blank(),
axis.title.y = element_text(
size = 18, margin = margin(l = 10, r = 20)),
axis.text.x = element_text(size = 12),
axis.text.y = element_text(size = 10),
axis.ticks = element_blank(),
axis.line = element_line(linewidth = 0.25),
panel.spacing = unit(-0.6, "lines"),
panel.grid.major.y = element_line(linewidth = 0.25),
legend.title = element_text(size = 14),
legend.text = element_text(size = 12),
legend.key = element_blank(),
legend.position = "bottom",
legend.key.height = unit(2, "lines"),
legend.key.width = unit(2, "lines"))
# Save to file
save_fig(g, "S06", height = save_height)
}
# ---------------------------------------------------------
# Plot total number of FVP over time for each d_v_a
# ---------------------------------------------------------
plot_total_fvps = function() {
message(" - Plotting total number of FVP")
# Flag for whether to plot FVPs cumulatively over time
cumulative = TRUE
# String to define total FVPs
total = "All source total"
# Total FVPs (sum of all sources)
#
# NOTE: Not necessarily equal to sum of all sources as
# SIA are assumed to be only partially targeted
total_dt = table("coverage") %>%
# Summarise over countries and age...
group_by(d_v_a_id, year) %>%
summarise(fvps = sum(fvps) / 1e9) %>%
ungroup() %>%
# Cumulative FVPs...
group_by(d_v_a_id) %>%
mutate(fvps_cum = cumsum(fvps)) %>%
ungroup() %>%
# Append d-v-a details...
left_join(y = table("d_v_a"),
by = "d_v_a_id") %>%
mutate(type = total) %>%
# Tidy up...
select(d_v_a_name, disease, type,
year, fvps, fvps_cum) %>%
as.data.table()
# Polio is a special case
polio_dt = total_dt %>%
filter(disease == "polio") %>%
mutate(source = "POLIS") %>%
select(d_v_a_name, source, year, fvps, fvps_cum)
# Map external model vaccines to d-v-a ID
extern_map = table("d_v_a_extern") %>%
filter(disease != "polio") %>%
select(disease, id = d_v_a_id) %>%
left_join(y = table("d_v_a"),
by = "disease") %>%
select(disease, id, d_v_a_id)
# Number of FVPs by source of data
source_dt = table("coverage_source") %>%
mutate(source = toupper(source)) %>%
# Map external vaccines to d-v-a ID...
rename(id = d_v_a_id) %>%
left_join(y = extern_map,
by = "id") %>%
mutate(d_v_a_id = ifelse(
test = is.na(d_v_a_id),
yes = id,
no = d_v_a_id)) %>%
filter(d_v_a_id %in% table("d_v_a")$d_v_a_id) %>%
# Summarise over countries and age...
group_by(d_v_a_id, source, year) %>%
summarise(fvps = sum(fvps) / 1e9) %>%
ungroup() %>%
# Cumulative FVPs...
group_by(d_v_a_id, source) %>%
mutate(fvps_cum = cumsum(fvps)) %>%
ungroup() %>%
# Append polio...
format_d_v_a_name() %>%
select(all_names(polio_dt)) %>%
rbind(polio_dt) %>%
as.data.table()
# All sources being plotted
sources = unique(source_dt$source)
# Concatenate into plotting datatable
plot_dt = total_dt %>%
select(-disease) %>%
bind_rows(source_dt) %>%
replace_na(list(
source = "NA",
type = "NA")) %>%
mutate(source = factor(source, c(sources, "NA")),
type = factor(type, c(total, "NA")))
# Metric to use for y axis
y = ifelse(cumulative, "fvps_cum", "fvps")
# Colours: named vector
colours = colour_scheme(
map = "brewer::set1",
n = length(sources)) %>%
c("black") %>%
setNames(c(sources, "NA"))
# Line types: named vector
types = c("dashed", "solid") %>%
setNames(c(total, "NA"))
# Plot FVPs over time for each d_v_a
g = ggplot(plot_dt) +
aes(x = year,
y = !!sym(y),
colour = source,
linetype = type) +
geom_line(
linewidth = 1.5) +
# Facet with strip text wrapping...
facet_wrap(
facets = vars(d_v_a_name),
labeller = label_wrap_gen(width = 24),
scales = "free_y") +
# Set colour scheme...
scale_color_manual(
breaks = sources,
values = colours) +
# Set line types...
scale_linetype_manual(
breaks = total,
values = types) +
# Prettify x axis...
scale_x_continuous(
limits = range(o$years),
expand = expansion(mult = c(0, 0)),
breaks = seq(
from = min(o$years),
to = max(o$years),
by = 10)) +
# Prettify y axis...
scale_y_continuous(
name = "Total receiving full schedule (in billions)",
labels = comma,
expand = expansion(mult = c(0, NA)))
# Prettify theme
g = g + theme_classic() +
theme(axis.title.x = element_blank(),
axis.title.y = element_text(
size = 20, margin = margin(l = 10, r = 20)),
axis.text = element_text(size = 10),
axis.text.x = element_text(hjust = 1, angle = 50),
axis.line = element_blank(),
strip.text = element_text(size = 12),
strip.background = element_blank(),
panel.border = element_rect(
linewidth = 0.5, fill = NA),
panel.spacing = unit(1, "lines"),
panel.grid.major.y = element_line(linewidth = 0.5),
legend.title = element_blank(),
legend.text = element_text(size = 14),
legend.key = element_blank(),
legend.position = "bottom",
legend.key.height = unit(2, "lines"),
legend.key.width = unit(3, "lines"))
# Save to file
save_fig(g, "S07")
}
# ---------------------------------------------------------
# Plot age targets as defined by WIISE and VIMC coverage data
# ---------------------------------------------------------
plot_coverage_age_density = function() {
message(" - Plotting coverage data density by age")
# Plot upto 2^x age
log2_max = 6
# Construct plotting datatable
plot_dt = table("coverage_source") %>%
mutate(trans_age = pmax(age, 1), .after = age) %>%
filter(trans_age <= 2 ^ log2_max) %>%
format_d_v_a_name() %>%
filter(!is.na(d_v_a_name))
# Plot age density of coverage data by source
g = ggplot(plot_dt) +
aes(x = trans_age,
y = after_stat(scaled),
colour = source,
fill = source) +
geom_density(alpha = 0.2) +
# Facet with strip text wrapping...
facet_wrap(
facets = vars(d_v_a_name),
labeller = label_wrap_gen(width = 24)) +
# Prettify x axis...
scale_x_continuous(
name = "Age (log2 scale)",
trans = "log2",
limits = c(1, 2 ^ log2_max),
expand = c(0, 0),
breaks = 2 ^ (0 : log2_max)) +
# Prettify y axis...
scale_y_continuous(
name = "Density",
limits = c(0, 1),
expand = expansion(mult = c(0, 0.1)),
breaks = pretty_breaks())
# Prettify theme
g = g + theme_classic() +
theme(axis.text = element_text(size = 10),
axis.title.x = element_text(
size = 20, margin = margin(b = 10, t = 20)),
axis.title.y = element_text(
size = 20, margin = margin(l = 10, r = 20)),
axis.line = element_blank(),
strip.text = element_text(size = 12),
strip.background = element_blank(),
panel.border = element_rect(
linewidth = 0.5, fill = NA),
panel.spacing = unit(1, "lines"),
legend.title = element_blank(),
legend.text = element_text(size = 12),
legend.key = element_blank(),
legend.position = "right",
legend.key.height = unit(2, "lines"),
legend.key.width = unit(2, "lines"))
# Save to file
save_fig(g, "S08")
}
# **** Static models ****
# ---------------------------------------------------------
# Plot Global Burden of Disease burden estimates by age
# ---------------------------------------------------------
plot_gbd_estimates = function() {
message(" - Plotting GBD burden estimates by age")
# Flag for plotting recent extrapolation
plot_extrap = FALSE
# Define meaningful metric names
metric_dict = c(
deaths = "Deaths",
dalys = "Disability-adjusted life years")
# Define age groups and associated upper bounds
age_groups = c(
"Neonates" = -1,
"Other infants" = 0,
"1-4 years" = 5,
"5-14" = 15,
"15-49" = 50,
"50-69" = 70,
"70+ years" = max(o$ages))
# Map each age bin to respective age group
age_group_dt = data.table(age = c(-1, o$ages)) %>%
mutate(group_idx = match(age, age_groups),
group = names(age_groups[group_idx])) %>%
fill(group, .direction = "up") %>%
select(age, age_group = group)
# Plot up to this year
plot_to = ifelse(plot_extrap, max(o$years), max(o$gbd_estimate_years))
# Load GBD estimates and categorise into age groups
gbd_dt = table("gbd_estimates") %>%
filter(year <= plot_to) %>%
append_d_v_t_name() %>%
# Append age group details...
left_join(y = age_group_dt,
by = "age") %>%
# Summarise for these broad age groups...
group_by(disease, year, age_group) %>%
summarise(deaths = sum(deaths_disease),
dalys = sum(dalys_disease)) %>%
ungroup() %>%
# Melt to long format...
pivot_longer(cols = c(deaths, dalys),
names_to = "metric") %>%
replace_na(list(value = 0)) %>%
# Append metric names...
mutate(metric = recode(metric, !!!metric_dict),
metric = factor(metric, metric_dict)) %>%
# Set age group factors for meaningful plotting order...
mutate(age_group = factor(age_group, names(age_groups))) %>%
select(disease, metric, year, age_group, value) %>%
arrange(disease, metric, year, age_group) %>%
as.data.table()
# Plot deaths over time by age group
g = ggplot(gbd_dt) +
aes(x = year,
y = value,
fill = age_group) +
geom_bar(stat = "identity") +
# Facet by disease...
facet_grid2(
cols = vars(disease),
rows = vars(metric),
scales = "free_y",
independent = "y") +
# Set colour scheme...
scale_fill_manual(
name = "Age group",
values = colour_scheme(
map = "brewer::paired",
n = length(age_groups))) +
# Prettify y axis...
scale_y_continuous(
labels = comma,
expand = expansion(mult = c(0, 0.05)),
breaks = pretty_breaks())
# Prettify theme
g = g + theme_classic() +
theme(axis.title = element_blank(),
axis.text = element_text(size = 10),
axis.line = element_blank(),
strip.text = element_text(size = 14),
strip.background = element_blank(),
panel.border = element_rect(
linewidth = 0.5, fill = NA),
panel.spacing = unit(1, "lines"),
legend.title = element_blank(),
legend.text = element_text(size = 14),
legend.key = element_blank(),
legend.position = "bottom",
legend.key.height = unit(2, "lines"),
legend.key.width = unit(2, "lines"))
# Save to file
save_fig(g, "S12")
}
# ---------------------------------------------------------
# Plot static model pathogen vaccine efficacy with immunity decay
# ---------------------------------------------------------
plot_vaccine_efficacy = function() {
message(" - Plotting vaccine efficacy profiles")
# Dictionary for each vaccine schedule
schedule_dict = c(
x = "Primary schedule",
bx = "Booster schedule",
px = "Pregnancy schedule")
# Function to extract vaccine efficacy data
extract_data_fn = function(dt) {
# Extract and format into datatable
data = dt$data %>%
unlist() %>%
as_named_dt("value") %>%
mutate(name = c("y", "x"),
vaccine = dt$vaccine) %>%
pivot_wider(id_cols = vaccine) %>%
as.data.table()
return(data)
}
# Function to group similar vaccines but split by dose
schedule_fn = function(dt) {
# Append descriptive columns
shedule_dt = dt %>%
# Primary schedule or booster dose...
separate(col = vaccine,
into = c("type", "schedule"),
sep = "_",
fill = "right",
remove = FALSE) %>%
replace_na(list(schedule = "x")) %>%
mutate(schedule = recode(schedule, !!!schedule_dict),
schedule = factor(schedule, schedule_dict)) %>%
# Append vaccine type name
mutate(type = str_remove(type, "[0-9]")) %>%
left_join(y = table("type_name"),
by = "type") %>%
select(-type) %>%
rename(type = type_name) %>%
mutate(type = fct_inorder(type)) %>%
as.data.table()
return(shedule_dt)
}
# Extract all vaccine efficacy data points
data_dt = table("vaccine_efficacy") %>%
dtapply(extract_data_fn) %>%
rbindlist() %>%
inner_join(y = table("d_v_a"),
by = "vaccine") %>%
schedule_fn() %>%
select(type, schedule, time = x, value = y)
# Load vaccine efficacy profiles
profile_dt = table("vaccine_efficacy_profiles") %>%
schedule_fn() %>%
select(type, schedule, time, value = profile)
# Plot vaccine efficacy with waning immunity (if any)
g = ggplot(profile_dt) +
aes(x = time,
y = value) +
# Plot immunity profile...
geom_line(
colour = "black",
linewidth = 1.5) +
# Plot coloured data points...
geom_point(
data = data_dt,
mapping = aes(colour = type),
size = 2.5) +
# Facet by vaccine and schedule...
facet_grid(
rows = vars(type),
cols = vars(schedule),
labeller = label_wrap_gen(width = 20)) +
# Prettify x axis...
scale_x_continuous(
name = "Years after completion of full schedule",
expand = expansion(mult = c(0.05, 0.05)),
breaks = pretty_breaks()) +
# Prettify y axis...
scale_y_continuous(
name = "Vaccine efficacy (death reduction)",
labels = percent,
limits = c(0, 1),
expand = expansion(mult = c(0, 0.05)),
breaks = pretty_breaks()) +
# Prettify legend (needed for y spacing to take effect)...
guides(fill = guide_legend(byrow = TRUE))
# Prettify theme
g = g + theme_classic() +
theme(axis.text = element_text(size = 9),
axis.title.x = element_text(
size = 16, margin = margin(b = 10, t = 20)),
axis.title.y = element_text(
size = 16, margin = margin(l = 10, r = 20)),
axis.line = element_blank(),
strip.text.x = element_text(size = 13),
strip.text.y = element_text(size = 11),
strip.background = element_blank(),
panel.border = element_rect(
linewidth = 0.5, fill = NA),
panel.spacing = unit(0.5, "lines"),
panel.grid.major.y = element_line(linewidth = 0.25),
legend.position = "none")
# Save figure to file
save_fig(g, "S09")
}
# ---------------------------------------------------------
# Plot effective coverage with waning immunity for static model pathogens
# ---------------------------------------------------------
plot_effective_coverage = function() {
message(" - Plotting effective coverage by year and age")
# Plot only up to a certain age
age_max = 50
# Manually define appropriate number of colours
colours = list(
disease = "blues",
type = "greens")
# Number of breaks in continuous colour bar
col_breaks = 40
# Repeat for disease and vaccine type
for (by in c("disease", "type")) {
# Load previously calculated total coverage file
effective_dt = read_rds("static", "effective_coverage", by)
# Population weight over all countries
plot_dt = effective_dt %>%
append_d_v_t_name() %>%
select(country, by = !!by, year, age, coverage) %>%
filter(age >= 0, # For simplicity, do not plot neonate immunity
age <= age_max) %>%
# Append population size...
left_join(y = table("wpp_pop"),
by = c("country", "year", "age")) %>%
mutate(n = pop * coverage) %>%
# Population weighted coverage...
lazy_dt() %>%
group_by(by, year, age) %>%
summarise(effective_coverage = sum(n / sum(pop))) %>%
ungroup() %>%
as.data.table()
# Construct continuous colour vector
col_map = paste0("pals::brewer.", colours[by])
col_vec = colour_scheme(col_map, n = col_breaks - 1)
# Plot each pathogen by year ana age
g = ggplot(plot_dt) +
aes(x = year, y = age, fill = effective_coverage) +
geom_tile() +
facet_wrap(~by) +
# Set continuous colour bar...
scale_fill_gradientn(
colours = c("#FFFFFF", col_vec),
limits = c(0, 1),
breaks = pretty_breaks(),
label = percent,
guide = guide_colourbar(
title = "Effective coverage")) +
# Prettify x axis...
scale_x_continuous(
expand = c(0, 0),
breaks = seq(
from = min(o$years),
to = max(o$years),
by = 5)) +
# Prettify y axis...
scale_y_continuous(
name = "Age (in years)",
expand = c(0, 0),
breaks = pretty_breaks())
# Prettify theme
g = g + theme_classic() +
theme(axis.title.x = element_blank(),
axis.title.y = element_text(
size = 20, margin = margin(l = 10, r = 20)),
axis.text = element_text(size = 10),
axis.text.x = element_text(hjust = 1, angle = 50),
axis.line = element_blank(),
strip.text = element_text(size = 14),
strip.background = element_blank(),
panel.border = element_rect(
linewidth = 0.5, fill = NA),
panel.spacing = unit(1, "lines"),
legend.position = "top",
legend.title = element_text(
size = 14, margin = margin(r = 20)),
legend.text = element_text(size = 10),
legend.key.height = unit(1.5, "lines"),
legend.key.width = unit(6, "lines"))
# Save figure to file
if (by == "type") save_fig(g, "S10")
if (by == "disease") save_fig(g, "S11")
}
}
# ---------------------------------------------------------
# Plot deaths and DALYs averted for static model pathogens
# ---------------------------------------------------------
plot_static = function() {
message(" - Plotting static model impact results")
# Flag for plotting recent extrapolation
plot_extrap = FALSE
# Disease burden / burden averted dictionary
metric_dict = c(
burden = "Estimated disease-specific burden (GBD 2021)",
averted = "Estimated burden averted from static model")
# Associated colours
metric_colours = c("darkred", "navyblue")
# ---- Plot by disease ----
# Ensure consistent years of plotting
plot_years = table("gbd_estimates") %>%
lazy_dt() %>%
pivot_longer(cols = -c(disease, country, year, age),
names_to = "metric") %>%
group_by(metric, disease, year) %>%
summarise(value = sum(value)) %>%
ungroup() %>%
# Only years for which we have all data...
filter(value > 0) %>%
count(year) %>%
filter(n == max(n)) %>%
pull(year) %>%
intersect(o$gbd_estimate_years)
# Repeat for each metric
for (metric in o$metrics) {
# Load previously calculated total coverage file
averted_dt = read_rds("static", metric, "averted_disease")
# Summarise results over country and age
disease_dt = averted_dt %>%
lazy_dt() %>%
filter(year %in% plot_years) %>%
pivot_longer(cols = c(burden, averted),
names_to = "metric") %>%
# Summarise over countries...
group_by(disease, year, metric) %>%
summarise(value = sum(value) / 1e6) %>%
ungroup() %>%
# Recode deaths disease/averted...
left_join(y = table("disease_name"),
by = "disease") %>%
mutate(metric = recode(metric, !!!metric_dict),
metric = factor(metric, metric_dict)) %>%
# Tidy up...
select(metric, disease = disease_name, year, value) %>%
arrange(metric, disease, year) %>%
as.data.table()
# Plot deaths and deaths averted by disease
g = ggplot(disease_dt) +
aes(x = year,
y = value,
colour = metric) +
geom_line(linewidth = 2) +
facet_wrap(~disease) +
# Set colours and prettify legend...
scale_colour_manual(values = metric_colours) +
guides(color = guide_legend(
byrow = TRUE, ncol = 1)) +
# Prettify x axis...
scale_x_continuous(
# limits = c(min(o$years), max(o$years)),
expand = expansion(mult = c(0, 0)),
breaks = pretty_breaks()) +
# Prettify y axis...
scale_y_continuous(
name = "Number of people (in millions)",
labels = comma,
expand = expansion(mult = c(0, 0.05)))
# Set a figure title explaining metric
g = g + ggtitle(
label = table("metric_dict") %>%
filter(metric == !!metric) %>%
pull(metric_impact))
# Prettify theme
g = g + theme_classic() +
theme(plot.title = element_text(size = 20, hjust = 0.5),
axis.title.x = element_blank(),
axis.title.y = element_text(
size = 20, margin = margin(l = 10, r = 20)),
axis.text = element_text(size = 10),
axis.text.x = element_text(hjust = 1, angle = 50),
axis.line = element_blank(),
strip.text = element_text(size = 14),
strip.background = element_blank(),
panel.border = element_rect(
linewidth = 0.5, fill = NA),
panel.spacing = unit(1, "lines"),
panel.grid.major.y = element_line(linewidth = 0.5),
legend.title = element_blank(),
legend.text = element_text(size = 14),
legend.key = element_blank(),
legend.position = "bottom",
legend.key.height = unit(2, "lines"),
legend.key.width = unit(3, "lines"))
# Save figures to file
if (metric == "deaths") save_fig(g, "S13")
if (metric == "dalys") save_fig(g, "S14")
}
}
# **** Regression (impute and infer) ****
# ---------------------------------------------------------
# Plot truth vs predicted for imputation training data
# ---------------------------------------------------------
plot_impute_quality = function(metric) {
message(" - Plotting imputation quality of fit: ", metric)
# ---- Load results from fitting ----
# Function to load imputation results
load_results_fn = function(id) {
# Load file and extract model details
result = try_load(
pth = o$pth$impute,
file = paste1("impute", metric, id),
throw_error = FALSE) %>%
pluck("result")
return(result)
}
# Load imputation results for all d-v-a
results_dt = table("d_v_a") %>%
filter(source == "vimc") %>%
pull(d_v_a_id) %>%
lapply(load_results_fn) %>%
rbindlist()
# ---- Construct plotting datatables ----
# Prepare datatable for plotting
plot_dt = results_dt %>%
filter(target > 0, prediction > 0) %>%
format_d_v_a_name() %>%
select(d_v_a_name, target, prediction)
# Maximum value in each facet (target or prediction)
blank_dt = plot_dt %>%
mutate(max_value = pmax(target, prediction)) %>%
group_by(d_v_a_name) %>%
summarise(max_value = max(max_value)) %>%
ungroup() %>%
expand_grid(type = c("target", "prediction")) %>%
pivot_wider(names_from = type,
values_from = max_value) %>%
as.data.table()
# Colour scheme
colours = colour_scheme(
map = "brewer::set1",
n = n_unique(plot_dt$d_v_a_name))
# ---- Produce plot ----
# Single plot with multiple facets
g = ggplot(plot_dt) +
aes(x = target,
y = prediction,
color = d_v_a_name) +
# Plot truth vs predicted...
geom_point(
alpha = 0.5,
shape = 16,
show.legend = FALSE) +
# For square axes...
geom_blank(data = blank_dt) +
# To see quality of predict vs target...
geom_abline(colour = "black") +
# Simple faceting with wrap labelling...
facet_wrap(
facets = vars(d_v_a_name),
labeller = label_wrap_gen(width = 30),
scales = "free") +
# Set colour scheme...
scale_colour_manual(
values = colours) +
# Prettify x axis...
scale_x_continuous(
name = "Imputation target",