Add dynamic scaling for axes

This commit is contained in:
2024-09-12 02:32:53 -04:00
parent 53d4695428
commit 626beec271

View File

@@ -419,6 +419,20 @@ generate_and_save_plots <- function(output_dir, file_name, plot_configs, grid_la
"bar" = plot + geom_bar() "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 plot
}) })
@@ -438,15 +452,30 @@ generate_interaction_plot_configs <- function(df, variables) {
# Define common y-limits and other attributes for each variable dynamically # 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)) 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) { for (variable in variables) {
# Dynamically generate the names of the columns # Dynamically generate the names of the columns
var_info <- list( var_info <- list(
ylim = limits_map[[variable]], ylim = limits_map[[variable]],
lm_model = df[[paste0("lm_", variable)]][[1]], # Access the precomputed linear model lm_model = df[[paste0("lm_", variable)]][[1]], # Access the precomputed linear model
sd_col = paste0("WT_sd_", variable), sd_col = paste0("WT_sd_", variable),
delta_var = paste0("Delta_", variable), delta_var = paste0("Delta_", variable)
z_shift = paste0("Z_Shift_", variable),
z_lm = paste0("Z_lm_", variable)
) )
# Extract the precomputed linear model coefficients # Extract the precomputed linear model coefficients
@@ -455,15 +484,12 @@ generate_interaction_plot_configs <- function(df, variables) {
slope = coef(var_info$lm_model)[2] slope = coef(var_info$lm_model)[2]
) )
# Set annotations dynamically for ZShift, Z lm Score, NG, DB, SM # Dynamically create annotations based on variable
base_y <- if (variable == "L" || variable == "K") 45 else if (variable == "r") 0.45 else 4500 annotations <- lapply(names(annotation_positions[[variable]]), function(annotation_name) {
annotations <- list( y_pos <- annotation_positions[[variable]][[annotation_name]]
list(x = 1, y = base_y, label = paste("ZShift =", round(df[[var_info$z_shift]], 2))), label <- annotation_labels[[annotation_name]](df, variable)
list(x = 1, y = base_y - 20, label = paste("lm ZScore =", round(df[[var_info$z_lm]], 2))), list(x = 1, y = y_pos, label = label)
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))
)
# Add scatter plot configuration for this variable # Add scatter plot configuration for this variable
configs[[length(configs) + 1]] <- list( configs[[length(configs) + 1]] <- list(
@@ -506,6 +532,7 @@ generate_interaction_plot_configs <- function(df, variables) {
return(configs) return(configs)
} }
# Adjust missing values and calculate ranks # Adjust missing values and calculate ranks
adjust_missing_and_rank <- function(df, variables) { adjust_missing_and_rank <- function(df, variables) {