Skip to content

Commit 0d446cd

Browse files
committed
Adding plotting functions from rapid-model-run-impact report which plots central impact estimates, coverage and fvps. Associated documentation also added from attempts at roxygen!
1 parent 14f1829 commit 0d446cd

3 files changed

Lines changed: 304 additions & 0 deletions

File tree

R/fn_plotting_impact.R

Lines changed: 240 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,240 @@
1+
#' Plot central impact estimates by cohort and year.
2+
#' TODO: need to add importFrom ... to avoid package issues with testing?
3+
#'
4+
#' Produces faceted plots of central impact estimates for priority countries,
5+
#' stratified either by birth cohort or by year of vaccination.
6+
#' Impact metrics include cases, deaths, DALYs, and YLLs.
7+
#'
8+
#' @param data A tibble containing impact estimates.
9+
#' @param burden_type Burden metric used to evaluate impact. burden_type can be: cases, deaths, dalys, yll.
10+
#' @param title Title of the plot to be rendered
11+
#' @param view Charactar scalar. The way impact is assigned, either by birth cohort ("cohort") or by year of vaccination ("year").
12+
#'
13+
#' @return ggplot object showing central impact estimates
14+
#'
15+
#' @examples
16+
#' plot_impact(
17+
#' data = impact_data,
18+
#' burden_type = "cases",
19+
#' title = "Cases averted",
20+
#' view = "year"
21+
#' )
22+
#'
23+
#' @export
24+
plot_impact <- function(
25+
data,
26+
burden_type,
27+
title,
28+
view
29+
){
30+
checkmate::assert_tibble(data, min.rows = 1L, min.cols = 1L)
31+
checkmate::assert_character(burden_type, len = 1)
32+
checkmate::assert_character(title, len = 1)
33+
34+
checkmate::assert_choice(
35+
burden_type,
36+
choices = c("cases", "deaths", "dalys", "yll")
37+
)
38+
39+
checkmate::assert_choice(
40+
view,
41+
choices = c("cohort", "year")
42+
)
43+
44+
Impact <-
45+
data %>%
46+
dplyr::filter(.data$country %in% pine) %>%
47+
dplyr::filter(
48+
.data$burden_outcome == burden_type & .data$impact != 0) #%>%
49+
if(nrow(Impact) > 0){
50+
# ---- Cohort view ----
51+
if(view == "cohort"){
52+
Impact <- Impact %>% dplyr::rename(cohort = .data$birth_cohort) %>%
53+
dplyr::select(
54+
.data$country,
55+
.data$cohort,
56+
.data$impact,
57+
.data$short_name
58+
)
59+
p <- ggplot(
60+
Impact,
61+
aes(
62+
x = .data$cohort,
63+
y = .data$impact,
64+
ymin = .data$impact,
65+
ymax = .data$impact,
66+
fill = as.character(.data$short_name)
67+
)
68+
) +
69+
ggplot::geom_ribbon(alpha = 0.3) +
70+
ggplot::geom_line(aes(colour = .data$short_name), size = 0.5)+
71+
ggplot::geom_point(aes(colour = .data$short_name), size = 0.5)+
72+
theme_vimc() + #TODO: to check where the theme definition is saved as may not be right for this plot
73+
facet_wrap(country~., scales = "free_y") +
74+
labs(
75+
x = "Birth cohort",
76+
y = paste(burden_type, "averted"),
77+
title = title
78+
) +
79+
theme(
80+
legend.position="bottom",
81+
legend.key.size= unit(0.5, 'cm'),
82+
legend.key.width = unit(0.3, 'cm')
83+
)
84+
85+
} else { # ---- Year (non-cohort) view ----
86+
Impact <- Impact %>%
87+
dplyr::select(
88+
.data$country,
89+
.data$year,
90+
.data$impact,
91+
.data$short_name
92+
)
93+
94+
p <- ggplot (
95+
Impact,
96+
aes(
97+
x = .data$year,
98+
y = .data$impact,
99+
ymin = .data$impact,
100+
ymax = .data$impact,
101+
fill = .data$short_name
102+
)
103+
) +
104+
ggplot::geom_ribbon(alpha = 0.3)+
105+
ggplot::geom_line(aes(colour = .data$short_name), size = 0.5)+
106+
ggplot::geom_point(aes(colour = .data$short_name), size = 0.5)+
107+
theme_vimc() + #TODO: same note as above re theme definition
108+
facet_wrap(country~., scales = "free_y")+
109+
labs(
110+
x = "Year",
111+
y = paste(burden_type, "averted"),
112+
title = title
113+
) +
114+
theme(
115+
legend.position="bottom",
116+
legend.key.size= unit(0.5, 'cm'),
117+
legend.key.width = unit(0.3, 'cm')
118+
)
119+
}
120+
} else {
121+
p <- "No estimates in the data." #TODO: both here and in the below plot returning p may be an issue? Can you think of a better way?
122+
}
123+
return(p)
124+
125+
}
126+
127+
#' Plot coverage and fully vaccinated persons (FVPs)
128+
#'
129+
#' Generates plots of routine vaccine coverage and fully vaccinated
130+
#' persons (FVPs) over time for selected countries.
131+
#'
132+
#' @param fvps A tibble showing the number of fvps (fully vaccinated persons)
133+
#' by country, year and scenario/activity type.
134+
#'
135+
#' @return A named list with two ggplot objects:
136+
#' \describe{
137+
#' \item{coverage}{A plot of routine vaccine coverage over time.}
138+
#' \item{fvps}{A plot of fully vaccinated persons over time.}
139+
#' }
140+
#' @examples
141+
#' plots <- plot_coverage_fvps(fvps)
142+
#' plots$coverage
143+
#' plots$fvps
144+
#'
145+
#' @export
146+
plot_coverage_fvps <- function(fvps){
147+
checkmate::assert_tibble(fvps, min.rows = 1L, min.cols = 1L)
148+
149+
fvps <- fvps %>%
150+
dplyr::filter(.data$country %in% pine)
151+
152+
cov <- fvps %>%
153+
dplyr::filter(.data$activity_type == "routine") %>%
154+
dplyr::mutate(
155+
vaccine_delivery = paste(.data$scenario_type, .data$vaccine, sep = "_"),
156+
coverage_adjusted = round(.data$coverage_adjusted*100, 2)
157+
) %>%
158+
dplyr::select(
159+
.data$country,
160+
.data$vaccine_delivery,
161+
.data$year,
162+
.data$coverage_adjusted) %>%
163+
dplyr::rename(coverage = .data$coverage_adjusted)
164+
165+
fvp <- fvps %>%
166+
dplyr::mutate(
167+
vaccine_delivery = paste(.data$scenario_type, .data$activity_type, sep = "_")
168+
) %>%
169+
dplyr::select(
170+
.data$country,
171+
.data$vaccine_delivery,
172+
.data$year,
173+
.data$fvps
174+
) %>%
175+
dplyr::group_by(
176+
.data$country,
177+
.data$vaccine_delivery,
178+
.data$year) %>%
179+
dplyr::summarise(
180+
fvps = round(sum(.data$fvps)/1e6, 2),
181+
.groups = "drop"
182+
)
183+
if(nrow(cov) > 0){
184+
p <- ggplot(
185+
cov,
186+
aes(
187+
x = .data$year,
188+
y = .data$coverage,
189+
ymin = 0,
190+
ymax = 1,
191+
fill = .data$vaccine_delivery)
192+
) +
193+
ggplot::geom_line(aes(colour = .data$vaccine_delivery), size = 0.5) +
194+
theme_vimc() + #TODO: same note as above
195+
facet_wrap(country~., scales = "free_y")+
196+
labs(
197+
x = "Year",
198+
y = "Coverage (%)",
199+
title = "Routine vaccine coverage"
200+
) +
201+
theme(
202+
legend.position="bottom",
203+
legend.key.size= unit(0.5, 'cm'),
204+
legend.key.width = unit(0.3, 'cm')
205+
)
206+
207+
} else {
208+
p <- "There is no routine coverage in the database."
209+
}
210+
211+
212+
q <- ggplot(
213+
fvp,
214+
aes(
215+
x = .data$year,
216+
y = .data$fvps,
217+
ymin = .data$fvps,
218+
ymax = .data$fvps, #TODO: min/max both here and above seem to be the same so may be irrelevant to define
219+
fill = .data$vaccine_delivery
220+
)
221+
) +
222+
geom_point(aes(colour = .data$vaccine_delivery), size = 0.5) +
223+
theme_vimc()+ #TODO: same note above on theme
224+
facet_wrap(country~., scales = "free_y") +
225+
labs(
226+
x = "Year",
227+
y = "FVPs (in millions)",
228+
title = "FVPs"
229+
) +
230+
theme(
231+
legend.position="bottom",
232+
legend.key.size = unit(0.5, 'cm'),
233+
legend.key.width = unit(0.3, 'cm')
234+
)
235+
236+
return(list(
237+
coverage = p,
238+
fvps = q
239+
))
240+
}

man/plot_coverage_fvps.Rd

Lines changed: 29 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

man/plot_impact.Rd

Lines changed: 35 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)