From 626beec2719399ccf3f6b4df66ea10de07085f98 Mon Sep 17 00:00:00 2001 From: Bryan Roessler Date: Thu, 12 Sep 2024 02:32:53 -0400 Subject: [PATCH] Add dynamic scaling for axes --- .../apps/r/calculate_interaction_zscores.R | 53 ++++++++++++++----- 1 file changed, 40 insertions(+), 13 deletions(-) diff --git a/qhtcp-workflow/apps/r/calculate_interaction_zscores.R b/qhtcp-workflow/apps/r/calculate_interaction_zscores.R index 193eb9ef..025d02f0 100644 --- a/qhtcp-workflow/apps/r/calculate_interaction_zscores.R +++ b/qhtcp-workflow/apps/r/calculate_interaction_zscores.R @@ -419,6 +419,20 @@ generate_and_save_plots <- function(output_dir, file_name, plot_configs, grid_la "bar" = plot + geom_bar() ) + # Conditionally apply scale_x_continuous if x_breaks, x_labels, and x_label are present + if (!is.null(config$x_breaks) && !is.null(config$x_labels) && !is.null(config$x_label)) { + plot <- plot + scale_x_continuous( + name = config$x_label, + breaks = config$x_breaks, + labels = config$x_labels + ) + } + + # Conditionally apply scale_y_continuous if ylim_vals is present + if (!is.null(config$ylim_vals)) { + plot <- plot + scale_y_continuous(limits = config$ylim_vals) + } + plot }) @@ -438,32 +452,44 @@ generate_interaction_plot_configs <- function(df, variables) { # Define common y-limits and other attributes for each variable dynamically limits_map <- list(L = c(-65, 65), K = c(-65, 65), r = c(-0.65, 0.65), AUC = c(-6500, 6500)) + # Define annotation positions based on the variable being plotted + annotation_positions <- list( + L = list(ZShift = 45, lm_ZScore = 25, NG = -25, DB = -35, SM = -45), + K = list(ZShift = 45, lm_ZScore = 25, NG = -25, DB = -35, SM = -45), + r = list(ZShift = 0.45, lm_ZScore = 0.25, NG = -0.25, DB = -0.35, SM = -0.45), + AUC = list(ZShift = 4500, lm_ZScore = 2500, NG = -2500, DB = -3500, SM = -4500) + ) + + # Define which annotations to include for each plot + annotation_labels <- list( + ZShift = function(df, var) paste("ZShift =", round(df[[paste0("Z_Shift_", var)]], 2)), + lm_ZScore = function(df, var) paste("lm ZScore =", round(df[[paste0("Z_lm_", var)]], 2)), + NG = function(df, var) paste("NG =", df$NG), + DB = function(df, var) paste("DB =", df$DB), + SM = function(df, var) paste("SM =", df$SM) + ) + for (variable in variables) { # Dynamically generate the names of the columns var_info <- list( ylim = limits_map[[variable]], lm_model = df[[paste0("lm_", variable)]][[1]], # Access the precomputed linear model sd_col = paste0("WT_sd_", variable), - delta_var = paste0("Delta_", variable), - z_shift = paste0("Z_Shift_", variable), - z_lm = paste0("Z_lm_", variable) + delta_var = paste0("Delta_", variable) ) - + # Extract the precomputed linear model coefficients lm_line <- list( intercept = coef(var_info$lm_model)[1], slope = coef(var_info$lm_model)[2] ) - # Set annotations dynamically for ZShift, Z lm Score, NG, DB, SM - base_y <- if (variable == "L" || variable == "K") 45 else if (variable == "r") 0.45 else 4500 - annotations <- list( - list(x = 1, y = base_y, label = paste("ZShift =", round(df[[var_info$z_shift]], 2))), - list(x = 1, y = base_y - 20, label = paste("lm ZScore =", round(df[[var_info$z_lm]], 2))), - list(x = 1, y = base_y - 70, label = paste("NG =", df$NG)), - list(x = 1, y = base_y - 80, label = paste("DB =", df$DB)), - list(x = 1, y = base_y - 90, label = paste("SM =", df$SM)) - ) + # Dynamically create annotations based on variable + annotations <- lapply(names(annotation_positions[[variable]]), function(annotation_name) { + y_pos <- annotation_positions[[variable]][[annotation_name]] + label <- annotation_labels[[annotation_name]](df, variable) + list(x = 1, y = y_pos, label = label) + }) # Add scatter plot configuration for this variable configs[[length(configs) + 1]] <- list( @@ -506,6 +532,7 @@ generate_interaction_plot_configs <- function(df, variables) { return(configs) } + # Adjust missing values and calculate ranks adjust_missing_and_rank <- function(df, variables) {