From 7bfe5c337e07f833dfae899f78725875e8dc1ab4 Mon Sep 17 00:00:00 2001 From: Bryan Roessler Date: Wed, 18 Sep 2024 02:29:11 -0400 Subject: [PATCH] Switch from tooltip to text aes for more control --- .../apps/r/calculate_interaction_zscores.R | 58 ++++++++++++++++--- 1 file changed, 49 insertions(+), 9 deletions(-) diff --git a/qhtcp-workflow/apps/r/calculate_interaction_zscores.R b/qhtcp-workflow/apps/r/calculate_interaction_zscores.R index 2e14c5df..af25da6e 100644 --- a/qhtcp-workflow/apps/r/calculate_interaction_zscores.R +++ b/qhtcp-workflow/apps/r/calculate_interaction_zscores.R @@ -370,16 +370,33 @@ generate_and_save_plots <- function(out_dir, file_name, plot_configs, grid_layou config <- plot_configs[[i]] df <- config$df + # Initialize tooltip_text + tooltip_text <- NULL + # Define aes_mapping based on plot type if (config$plot_type == "scatter") { + # Check if y_var is provided + if (is.null(config$y_var)) { + warning(paste("Plot", i, "of type 'scatter' is missing 'y_var'. Skipping this plot.")) + next + } + + # Construct tooltip_text based on configuration flags if (!is.null(config$delta_bg_point) && config$delta_bg_point) { - tooltip_text <- paste("OrfRep:", df$OrfRep, "
Gene:", df$Gene, "
delta_bg:", df$delta_bg) + # Ensure 'delta_bg' exists + if (!"delta_bg" %in% names(df)) { + warning(paste("Plot", i, "requires 'delta_bg' column for tooltip, but it's missing.")) + tooltip_text <- paste("OrfRep:", df$OrfRep, "
Gene:", df$Gene) + } else { + tooltip_text <- paste("OrfRep:", df$OrfRep, "
Gene:", df$Gene, "
delta_bg:", df$delta_bg) + } } else if (!is.null(config$gene_point) && config$gene_point) { tooltip_text <- paste("OrfRep:", df$OrfRep, "
Gene:", df$Gene) } else { tooltip_text <- paste("x:", df[[config$x_var]], "
y:", df[[config$y_var]]) } + # Define aesthetic mapping with or without color_var aes_mapping <- if (is.null(config$color_var)) { aes(x = .data[[config$x_var]], y = .data[[config$y_var]], text = tooltip_text) } else { @@ -387,13 +404,34 @@ generate_and_save_plots <- function(out_dir, file_name, plot_configs, grid_layou color = as.factor(.data[[config$color_var]]), text = tooltip_text) } } else { - # Define aes_mapping for other plot types without 'text' aesthetic - aes_mapping <- if (is.null(config$color_var)) { - aes(x = .data[[config$x_var]], y = .data[[config$y_var]]) - } else { - aes(x = .data[[config$x_var]], y = .data[[config$y_var]], - color = as.factor(.data[[config$color_var]])) + # Handle other plot types + # For 'box' plots, y_var is required + if (config$plot_type == "box") { + if (is.null(config$y_var)) { + warning(paste("Plot", i, "of type 'box' is missing 'y_var'. Skipping this plot.")) + next + } } + + # Define aes_mapping for non-scatter plots + aes_mapping <- if (is.null(config$color_var)) { + if (config$plot_type %in% c("density", "bar")) { + aes(x = .data[[config$x_var]]) + } else { + aes(x = .data[[config$x_var]], y = .data[[config$y_var]]) + } + } else { + if (config$plot_type %in% c("density", "bar")) { + aes(x = .data[[config$x_var]], + color = as.factor(.data[[config$color_var]])) + } else { + aes(x = .data[[config$x_var]], y = .data[[config$y_var]], + color = as.factor(.data[[config$color_var]])) + } + } + + # No tooltip for non-scatter plots + tooltip_text <- NULL } # Start building the plot with aes_mapping @@ -405,7 +443,10 @@ generate_and_save_plots <- function(out_dir, file_name, plot_configs, grid_layou "box" = generate_box_plot(plot_base, config), "density" = plot_base + geom_density(), "bar" = plot_base + geom_bar(), - plot_base # default case if no type matches + { + warning(paste("Unknown plot_type:", config$plot_type, "- using base plot")) + plot_base + } ) # Apply additional settings if provided @@ -429,7 +470,6 @@ generate_and_save_plots <- function(out_dir, file_name, plot_configs, grid_layou plotly_plot <- ggplotly(plot, tooltip = "text") } else { # For non-scatter plots, decide if tooltips are needed - # If not, you can set tooltip to NULL or specify relevant aesthetics plotly_plot <- ggplotly(plot, tooltip = "none") }