Skip to content

Commit 141b2fe

Browse files
authored
Merge pull request #45 from epiforecasts/modify-data-as-of-plot
Add all-US data_as_of plot variants with multiple layouts
2 parents 867c4e4 + d15ece2 commit 141b2fe

File tree

5 files changed

+329
-0
lines changed

5 files changed

+329
-0
lines changed

R/fig_data_as_of.R

Lines changed: 166 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -321,3 +321,169 @@ get_second_data_fig <- function(seq_counts_as_of1,
321321
)
322322
return(fig_eval)
323323
}
324+
325+
#' Make data comparing data as of nowcast date vs when evaluating -
326+
#' All US version
327+
#'
328+
#' @param seq_counts_as_of A plot showing sequence counts as of nowcast date
329+
#' @param seq_counts_eval B plot showing sequence counts for evaluation
330+
#' @param eval_freq C plot showing clade frequencies
331+
#' @inheritParams get_plot_obs_clade_freq
332+
#'
333+
#' @returns patchwork figure
334+
get_data_fig_all_us <- function(seq_counts_as_of,
335+
seq_counts_eval,
336+
eval_freq,
337+
plot_name,
338+
output_fp = file.path(
339+
"output", "figs",
340+
"as_of_data_figs", "final"
341+
)) {
342+
fig_layout <- "
343+
AAA
344+
BBB
345+
CCC"
346+
347+
fig_eval <- (seq_counts_as_of +
348+
ylab("Sequence counts as of\nthe nowcast date")) +
349+
(seq_counts_eval + ylab("Sequence counts\nfor evaluation")) +
350+
eval_freq +
351+
plot_layout(
352+
design = fig_layout,
353+
axes = "collect",
354+
guides = "collect"
355+
) +
356+
plot_annotation(
357+
tag_levels = "A",
358+
tag_suffix = "",
359+
tag_sep = "",
360+
theme = theme(
361+
legend.position = "top",
362+
legend.title = element_text(hjust = 0.5),
363+
legend.justification = "center",
364+
plot.tag = element_text(size = 20)
365+
)
366+
)
367+
368+
dir_create(output_fp, recurse = TRUE)
369+
ggsave(file.path(output_fp, glue::glue("{plot_name}.png")),
370+
plot = fig_eval,
371+
width = 10,
372+
height = 10
373+
)
374+
return(fig_eval)
375+
}
376+
377+
#' Make data comparing data as of nowcast date vs when evaluating -
378+
#' Horizontal layout
379+
#'
380+
#' @param seq_counts_as_of A plot showing sequence counts as of nowcast date
381+
#' @param seq_counts_eval B plot showing sequence counts for evaluation
382+
#' @param eval_freq C plot showing clade frequencies
383+
#' @inheritParams get_plot_obs_clade_freq
384+
#'
385+
#' @returns patchwork figure
386+
get_data_fig_all_us_horizontal <- function(seq_counts_as_of,
387+
seq_counts_eval,
388+
eval_freq,
389+
plot_name,
390+
output_fp = file.path(
391+
"output", "figs",
392+
"as_of_data_figs", "final"
393+
)) {
394+
fig_layout <- "ABC"
395+
396+
# Add x-axis text to all plots
397+
seq_counts_as_of <- seq_counts_as_of +
398+
theme(axis.text.x = element_text(angle = 45, hjust = 1, size = 10))
399+
400+
seq_counts_eval <- seq_counts_eval +
401+
theme(axis.text.x = element_text(angle = 45, hjust = 1, size = 10))
402+
403+
fig_eval <- (seq_counts_as_of +
404+
ylab("Sequence counts as of\nthe nowcast date")) +
405+
(seq_counts_eval +
406+
ylab("Sequence counts\nfor evaluation")) +
407+
eval_freq +
408+
plot_layout(
409+
design = fig_layout,
410+
guides = "collect"
411+
) +
412+
plot_annotation(
413+
tag_levels = "A",
414+
tag_suffix = "",
415+
tag_sep = "",
416+
theme = theme(
417+
legend.position = "top",
418+
legend.title = element_text(hjust = 0.5),
419+
legend.justification = "center",
420+
plot.tag = element_text(size = 20)
421+
)
422+
)
423+
424+
dir_create(output_fp, recurse = TRUE)
425+
ggsave(file.path(output_fp, glue::glue("{plot_name}.png")),
426+
plot = fig_eval,
427+
width = 15,
428+
height = 5
429+
)
430+
return(fig_eval)
431+
}
432+
433+
#' Make data comparing data as of nowcast date vs when evaluating -
434+
#' Triangular layout
435+
#'
436+
#' @param seq_counts_as_of A plot showing sequence counts as of nowcast date
437+
#' @param seq_counts_eval B plot showing sequence counts for evaluation
438+
#' @param eval_freq C plot showing clade frequencies
439+
#' @inheritParams get_plot_obs_clade_freq
440+
#'
441+
#' @returns patchwork figure
442+
get_data_fig_all_us_triangular <- function(seq_counts_as_of,
443+
seq_counts_eval,
444+
eval_freq,
445+
plot_name,
446+
output_fp = file.path(
447+
"output", "figs",
448+
"as_of_data_figs", "final"
449+
)) {
450+
# Inverted triangle: two plots on top, one wide plot on bottom
451+
fig_layout <- "
452+
AAABBB
453+
CCCCCC"
454+
455+
# Add x-axis text to top plots (they were set to remove it)
456+
seq_counts_as_of <- seq_counts_as_of +
457+
theme(axis.text.x = element_text(angle = 45, hjust = 1, size = 10))
458+
459+
seq_counts_eval <- seq_counts_eval +
460+
theme(axis.text.x = element_text(angle = 45, hjust = 1, size = 10))
461+
462+
fig_eval <- (seq_counts_as_of +
463+
ylab("Sequence counts as of\nthe nowcast date")) +
464+
(seq_counts_eval + ylab("Sequence counts\nfor evaluation")) +
465+
eval_freq +
466+
plot_layout(
467+
design = fig_layout,
468+
guides = "collect"
469+
) +
470+
plot_annotation(
471+
tag_levels = "A",
472+
tag_suffix = "",
473+
tag_sep = "",
474+
theme = theme(
475+
legend.position = "top",
476+
legend.title = element_text(hjust = 0.5),
477+
legend.justification = "center",
478+
plot.tag = element_text(size = 20)
479+
)
480+
)
481+
482+
dir_create(output_fp, recurse = TRUE)
483+
ggsave(file.path(output_fp, glue::glue("{plot_name}.png")),
484+
plot = fig_eval,
485+
width = 12,
486+
height = 8
487+
)
488+
return(fig_eval)
489+
}

man/get_data_fig_all_us.Rd

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

man/get_data_fig_all_us_horizontal.Rd

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

man/get_data_fig_all_us_triangular.Rd

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

targets/fig_data_as_of_targets.R

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,5 +99,69 @@ fig_data_as_of_targets <- list(
9999
eval_freq2 = plot_eval_freq_ex1,
100100
plot_name = "data_as_of"
101101
)
102+
),
103+
# All US versions (no CA split)
104+
tar_target(
105+
name = plot_seq_counts_as_of_all_us,
106+
command = get_bar_chart_seq_count(
107+
obs_data = clean_variant_data_as_of_nowcast_date,
108+
location = "US",
109+
date_range = date_range_as_of,
110+
temporal_granularity = "weeks",
111+
plot_name = "bar_chart_seq_as_of_all_us",
112+
log_scale = FALSE,
113+
nowcast_date_line = TRUE,
114+
remove_xticks = TRUE,
115+
title = "US"
116+
)
117+
),
118+
tar_target(
119+
name = plot_eval_seq_counts_all_us,
120+
command = get_bar_chart_comparison(
121+
obs_data = clean_variant_data_as_of_nowcast_date,
122+
final_data = clean_variant_data_final_all_states,
123+
location = "US",
124+
temporal_granularity = "weeks",
125+
date_range = date_range_as_of,
126+
log_scale = FALSE
127+
)
128+
),
129+
tar_target(
130+
name = plot_eval_freq_all_us,
131+
command = get_plot_freq_as_of_vs_eval(
132+
obs_data = clean_variant_data_as_of_nowcast_date,
133+
final_data = clean_variant_data_final_all_states,
134+
date_range = date_range_as_of,
135+
location = "US",
136+
temporal_granularity = "weeks",
137+
clades_to_plot = clades_to_plot
138+
)
139+
),
140+
tar_target(
141+
name = data_fig_all_us_vertical,
142+
command = get_data_fig_all_us(
143+
seq_counts_as_of = plot_seq_counts_as_of_all_us,
144+
seq_counts_eval = plot_eval_seq_counts_all_us,
145+
eval_freq = plot_eval_freq_all_us,
146+
plot_name = "data_as_of_all_us_vertical"
147+
)
148+
),
149+
tar_target(
150+
name = data_fig_all_us_horizontal,
151+
command = get_data_fig_all_us_horizontal(
152+
seq_counts_as_of = plot_seq_counts_as_of_all_us,
153+
seq_counts_eval = plot_eval_seq_counts_all_us,
154+
eval_freq = plot_eval_freq_all_us,
155+
plot_name = "data_as_of_all_us_horizontal"
156+
)
157+
),
158+
tar_target(
159+
name = data_fig_all_us_triangular,
160+
command = get_data_fig_all_us_triangular(
161+
seq_counts_as_of = plot_seq_counts_as_of_all_us,
162+
seq_counts_eval = plot_eval_seq_counts_all_us,
163+
eval_freq = plot_eval_freq_all_us,
164+
plot_name = "data_as_of_all_us_triangular"
165+
)
102166
)
103167
)

0 commit comments

Comments
 (0)