Before generate_and_save_plots() refactor

This commit is contained in:
2024-09-10 19:12:17 -04:00
parent 5518005d03
commit 2a7f460897

View File

@@ -409,7 +409,7 @@ generate_and_save_plots <- function(output_dir, file_name, plot_configs, grid_la
# Setting legend position, titles, and labels # Setting legend position, titles, and labels
legend_position <- config$legend_position %||% "bottom" legend_position <- config$legend_position %||% "bottom"
plot <- plot + ggtitle(config$title) + theme_Publication(legend_position = legend_position) plot <- plot + ggtitle(config$title) + theme_publication(legend_position = legend_position)
if (!is.null(config$x_label)) plot <- plot + xlab(config$x_label) if (!is.null(config$x_label)) plot <- plot + xlab(config$x_label)
if (!is.null(config$y_label)) plot <- plot + ylab(config$y_label) if (!is.null(config$y_label)) plot <- plot + ylab(config$y_label)
@@ -439,12 +439,11 @@ generate_and_save_plots <- function(output_dir, file_name, plot_configs, grid_la
dev.off() dev.off()
# Save as HTML (optional)
plotly_plots <- lapply(plots, function(plot) { plotly_plots <- lapply(plots, function(plot) {
suppressWarnings(ggplotly(plot) %>% layout(legend = list(orientation = "h"))) suppressWarnings(ggplotly(plot) %>% layout(legend = list(orientation = "h")))
}) })
combined_plot <- subplot(plotly_plots, nrows = grid_layout$nrow, margin = 0.05) combined_plot <- subplot(plotly_plots, nrows = grid_layout$nrow, margin = 0.05)
saveWidget(combined_plot, file = file.path(output_dir, paste0(file_name, "_grid.html")), selfcontained = TRUE) saveWidget(combined_plot, file = file.path(output_dir, paste0(file_name, ".html")), selfcontained = TRUE)
} else { } else {
# Save individual plots to PDF # Save individual plots to PDF
@@ -603,7 +602,8 @@ generate_correlation_plot_configs <- function(df, variables) {
plot_type = "correlation", plot_type = "correlation",
title = paste("Avg Zscore vs lm", variable), title = paste("Avg Zscore vs lm", variable),
color_var = "Overlap", color_var = "Overlap",
correlation_text = paste("R-squared =", round(df[[lm_r_squared_col]][1], 2)) correlation_text = paste("R-squared =", round(df[[lm_r_squared_col]][1], 2)),
legend_position = "right"
) )
} }
@@ -966,6 +966,7 @@ main <- function() {
group_by(across(all_of(group_vars))) %>% group_by(across(all_of(group_vars))) %>%
filter(!is.na(Z_lm_L) | !is.na(Avg_Zscore_L)) filter(!is.na(Z_lm_L) | !is.na(Avg_Zscore_L))
# Final filtered correaltion calculations and plots
zscores_interactions_filtered <- zscores_interactions_filtered %>% zscores_interactions_filtered <- zscores_interactions_filtered %>%
mutate( mutate(
Overlap = case_when( Overlap = case_when(
@@ -988,7 +989,6 @@ main <- function() {
generate_rank_plot_configs(zscores_interactions_filtered, "Rank_L", "Avg_Zscore_L", "L"), generate_rank_plot_configs(zscores_interactions_filtered, "Rank_L", "Avg_Zscore_L", "L"),
generate_rank_plot_configs(zscores_interactions_filtered, "Rank_K", "Avg_Zscore_K", "K") generate_rank_plot_configs(zscores_interactions_filtered, "Rank_K", "Avg_Zscore_K", "K")
) )
generate_and_save_plots(output_dir = out_dir, file_name = "RankPlots", generate_and_save_plots(output_dir = out_dir, file_name = "RankPlots",
plot_configs = rank_plot_configs, grid_layout = list(ncol = 3, nrow = 2)) plot_configs = rank_plot_configs, grid_layout = list(ncol = 3, nrow = 2))
@@ -996,14 +996,12 @@ main <- function() {
generate_rank_plot_configs(zscores_interactions_filtered, "Rank_lm_L", "Z_lm_L", "L", is_lm = TRUE), generate_rank_plot_configs(zscores_interactions_filtered, "Rank_lm_L", "Z_lm_L", "L", is_lm = TRUE),
generate_rank_plot_configs(zscores_interactions_filtered, "Rank_lm_K", "Z_lm_K", "K", is_lm = TRUE) generate_rank_plot_configs(zscores_interactions_filtered, "Rank_lm_K", "Z_lm_K", "K", is_lm = TRUE)
) )
generate_and_save_plots(output_dir = out_dir, file_name = "RankPlots_lm", generate_and_save_plots(output_dir = out_dir, file_name = "RankPlots_lm",
plot_configs = rank_lm_plot_configs, grid_layout = list(ncol = 3, nrow = 2)) plot_configs = rank_lm_plot_configs, grid_layout = list(ncol = 3, nrow = 2))
correlation_plot_configs <- generate_correlation_plot_configs(zscores_interactions_filtered, variables) correlation_plot_configs <- generate_correlation_plot_configs(zscores_interactions_filtered, variables)
generate_and_save_plots(output_dir = out_dir, file_name = "Avg_Zscore_vs_lm_NA_rm", generate_and_save_plots(output_dir = out_dir, file_name = "Avg_Zscore_vs_lm_NA_rm",
plot_configs = correlation_plot_configs, grid_layout = list(ncol = 2, nrow = 2)) plot_configs = correlation_plot_configs, grid_layout = list(ncol = 2, nrow = 2))
}) })
}) })
} }