From 4e15fb80fbddf8cf65413ff19091e9914304ba07 Mon Sep 17 00:00:00 2001 From: Bryan Roessler Date: Mon, 9 Sep 2024 23:34:18 -0400 Subject: [PATCH] Add boxplots to interaction plots --- .../apps/r/calculate_interaction_zscores5.R | 32 ++++++++++++++----- 1 file changed, 24 insertions(+), 8 deletions(-) diff --git a/workflow/apps/r/calculate_interaction_zscores5.R b/workflow/apps/r/calculate_interaction_zscores5.R index d2f3bce6..2bcd7882 100644 --- a/workflow/apps/r/calculate_interaction_zscores5.R +++ b/workflow/apps/r/calculate_interaction_zscores5.R @@ -320,22 +320,22 @@ generate_and_save_plots <- function(output_dir, file_name, plot_configs, grid_la if (config$y_var == "delta_bg" && config$plot_type == "scatter") { plot <- plot + geom_point(shape = 3, size = 0.2, position = "jitter") + geom_errorbar(aes(ymin = !!sym(y_mean_col) - !!sym(y_sd_col), - ymax = !!sym(y_mean_col) + !!sym(y_sd_col)), width = 0.1) + + ymax = !!sym(y_mean_col) + !!sym(y_sd_col)), width = 0.1) + geom_point(aes(y = !!sym(y_mean_col)), size = 0.6) } else if (config$error_bar %||% FALSE) { plot <- plot + geom_point(shape = 3, size = 0.2) + geom_errorbar(aes(ymin = !!sym(y_mean_col) - !!sym(y_sd_col), - ymax = !!sym(y_mean_col) + !!sym(y_sd_col)), width = 0.1) + + ymax = !!sym(y_mean_col) + !!sym(y_sd_col)), width = 0.1) + geom_point(aes(y = !!sym(y_mean_col)), size = 0.6) } } plot <- switch(config$plot_type, - "box" = plot + geom_boxplot(), - "density" = plot + geom_density(), - "bar" = plot + geom_bar(stat = "identity"), - plot + geom_point() + geom_smooth(method = "lm", se = FALSE)) + "box" = plot + geom_boxplot(), + "density" = plot + geom_density(), + "bar" = plot + geom_bar(stat = "identity"), + plot + geom_point() + geom_smooth(method = "lm", se = FALSE)) if (!is.null(config$ylim_vals)) { plot <- plot + coord_cartesian(ylim = config$ylim_vals) @@ -358,7 +358,7 @@ generate_and_save_plots <- function(output_dir, file_name, plot_configs, grid_la # If grid_layout is provided, arrange plots in a grid and save in a single PDF if (!is.null(grid_layout)) { - pdf(file.path(output_dir, paste0(file_name, "_grid.pdf")), width = 14, height = 9) + pdf(file.path(output_dir, paste0(file_name, ".pdf")), width = 14, height = 9) # Loop through plots in chunks defined by ncol and nrow for (start_idx in seq(1, length(plots), by = grid_layout$ncol * grid_layout$nrow)) { @@ -426,7 +426,7 @@ interaction_plot_configs <- function(df, variables) { ifelse(variable == "r", -0.45, -4500))), label = paste("SM =", df$SM)) ) - # Append a new plot configuration for each variable + # Add scatter plot configuration for this variable plot_configs[[length(plot_configs) + 1]] <- list( df = df, x_var = "conc_num_factor", @@ -443,11 +443,27 @@ interaction_plot_configs <- function(df, variables) { x_labels = unique(as.character(df$conc_num)), x_label = unique(df$Drug[1]) ) + + # Add box plot configuration for this variable + plot_configs[[length(plot_configs) + 1]] <- list( + df = df, + x_var = "conc_num_factor", + y_var = variable, + plot_type = "box", + title = sprintf("%s %s (Boxplot)", df$OrfRep[1], df$Gene[1]), + ylim_vals = ylim_vals, + annotations = annotations, + error_bar = FALSE, # Boxplots typically don't need error bars + x_breaks = unique(df$conc_num_factor), + x_labels = unique(as.character(df$conc_num)), + x_label = unique(df$Drug[1]) + ) } return(plot_configs) } + correlation_plot_configs <- function(df, lm_list, lm_summaries) { lapply(seq_along(lm_list), function(i) { r_squared <- round(lm_summaries[[i]]$r.squared, 3)