diff --git a/src/plot_summary_graphs.R b/src/plot_summary_graphs.R index eb06a87..6f572d8 100644 --- a/src/plot_summary_graphs.R +++ b/src/plot_summary_graphs.R @@ -132,7 +132,8 @@ plot_summary_graphs <- function( suppressMessages(save_plots(model_output_data = model_output_logit, target_data = target_logit, save_path = save_path_daily_logit, - page_by_location = page_by_location)) + page_by_location = page_by_location, + plot_loess = FALSE)) } @@ -148,12 +149,14 @@ plot_summary_graphs <- function( save_plots <- function(model_output_data, target_data, save_path, - page_by_location = TRUE) { + page_by_location = TRUE, + plot_loess = TRUE) { if(page_by_location){ plots <- lapply(unique(model_output_data$location), function(.x) plot_one_location(this_location = .x, model_output_data = model_output_data, - target_data = target_data)) + target_data = target_data, + plot_loess = plot_loess)) } else { plots <- lapply(unique(model_output_data$clade), function(.x) plot_one_clade(this_clade = .x, @@ -172,11 +175,10 @@ save_plots <- function(model_output_data, #' @param model_output_data formatted model output data #' @param target_data formatted target data #' -plot_one_location <- function(this_location, model_output_data, target_data){ +plot_one_location <- function(this_location, model_output_data, target_data, plot_loess = TRUE){ require(dplyr) require(ggplot2) theme_set(theme_bw()) - mean_data_loc <- model_output_data |> filter(location == this_location) |> group_by(target_date, clade, location) |> @@ -196,19 +198,39 @@ plot_one_location <- function(this_location, model_output_data, target_data){ ylim <- c(0, 1) transftitle <- "" } - - p <- mean_data_loc |> - ggplot(aes(x=date, y=value)) + - geom_point(data = target_data_loc, aes(size = total))+ - geom_smooth(data = target_data_loc, se=FALSE, aes(weight = total))+ - geom_line(color = "red") + - geom_ribbon(aes(ymin = q10, ymax = q90), fill="red", alpha = .5) + - scale_y_continuous(name = "clade frequency") + - coord_cartesian(ylim = ylim) + - scale_x_date(NULL, date_breaks = "3 months", date_minor_breaks = "1 month") + - scale_size(name = "# of sequences") + - facet_wrap(~clade) + - ggtitle(paste(transftitle, "Observed and predicted frequencies of SARS-CoV-2 clades in", this_location)) + + if(plot_loess){ + p <- mean_data_loc |> + ggplot(aes(x=date, y=value)) + + geom_point(data = target_data_loc, aes(size = total), alpha = 0.6) + + geom_smooth(data = target_data_loc, se=FALSE, size = 0.8, alpha = 0.6, + aes(weight = total), + method = loess, method.args = list(span = 0.5, degree = 1))+ + geom_line(color = "red") + + geom_ribbon(aes(ymin = q10, ymax = q90), fill="red", alpha = .5) + + scale_y_continuous(name = "clade frequency") + + coord_cartesian(ylim = ylim) + + scale_x_date(NULL, date_breaks = "3 months", date_minor_breaks = "1 month") + + scale_size(name = "# of sequences") + + facet_wrap(~clade) + + ggtitle(paste(transftitle, "Observed and predicted frequencies of SARS-CoV-2 clades in", this_location)) + + } + else{ + p <- mean_data_loc |> + ggplot(aes(x=date, y=value)) + + geom_point(data = target_data_loc, aes(size = total), alpha = 0.6) + + geom_line(color = "red") + + geom_ribbon(aes(ymin = q10, ymax = q90), fill="red", alpha = .5) + + scale_y_continuous(name = "clade frequency") + + coord_cartesian(ylim = ylim) + + scale_x_date(NULL, date_breaks = "3 months", date_minor_breaks = "1 month") + + scale_size(name = "# of sequences") + + facet_wrap(~clade) + + ggtitle(paste(transftitle, "Observed and predicted frequencies of SARS-CoV-2 clades in", this_location)) + } + + return(p) }