diff --git a/qhtcp-workflow/apps/r/calculate_interaction_zscores.R b/qhtcp-workflow/apps/r/calculate_interaction_zscores.R index c1bf42e0..65e2ffb1 100644 --- a/qhtcp-workflow/apps/r/calculate_interaction_zscores.R +++ b/qhtcp-workflow/apps/r/calculate_interaction_zscores.R @@ -391,20 +391,13 @@ generate_and_save_plots <- function(output_dir, file_name, plot_configs, grid_la # "delta_bg_tolerance", "delta_bg", "Gene", "L", "K", "r", "AUC", "NG", "DB"))), n = 5) # Plots are testy about missing aesthetics, so handle them here - aes_mapping <- if (is.null(config$color_var)) { - if (is.null(config$y_var)) { - aes(x = !!sym(config$x_var)) - } else { - aes(x = !!sym(config$x_var), y = !!sym(config$y_var)) - } - } else { - if (is.null(config$y_var)) { - aes(x = !!sym(config$x_var), color = as.factor(!!sym(config$color_var))) - } else { - aes(x = !!sym(config$x_var), y = !!sym(config$y_var), color = as.factor(!!sym(config$color_var))) - } - } + aes_mapping <- aes( + x = !!sym(config$x_var), + y = if (!is.null(config$y_var)) !!sym(config$y_var) else NULL, + color = if (!is.null(config$color_var)) as.factor(!!sym(config$color_var)) else NULL + ) + # Start building the plot plot <- ggplot(df, aes_mapping) # Use appropriate helper function based on plot type @@ -428,7 +421,7 @@ generate_and_save_plots <- function(output_dir, file_name, plot_configs, grid_la # HTML saving logic plotly_plots <- lapply(plots, function(plot) { - config <- plot$labels$config + config <- plot$config if (!is.null(config$legend_position) && config$legend_position == "bottom") { suppressWarnings(ggplotly(plot, tooltip = "text") %>% layout(legend = list(orientation = "h"))) } else { @@ -440,28 +433,24 @@ generate_and_save_plots <- function(output_dir, file_name, plot_configs, grid_la } generate_scatter_plot <- function(plot, config, interactive = FALSE) { - - # Determine the base aesthetics - aes_params <- aes( - x = !!sym(config$x_var), - y = !!sym(config$y_var), - color = as.factor(!!sym(config$color_var))) # Add the interactive `text` aesthetic if `interactive` is TRUE if (interactive) { - if (!is.null(config$delta_bg_point) && config$delta_bg_point) { - aes_params$text <- paste("ORF:", OrfRep, "Gene:", Gene, "delta_bg:", delta_bg) + plot <- if (!is.null(config$delta_bg_point) && config$delta_bg_point) { + plot + geom_point(aes(text = paste("ORF:", OrfRep, "Gene:", Gene, "delta_bg:", delta_bg)), + shape = config$shape %||% 3, size = config$size %||% 0.2) } else if (!is.null(config$gene_point) && config$gene_point) { - aes_params$text <- paste("ORF:", OrfRep, "Gene:", Gene) + plot + geom_point(aes(text = paste("ORF:", OrfRep, "Gene:", Gene)), + shape = config$shape %||% 3, size = config$size %||% 0.2, position = "jitter") + } else { + plot + geom_point(shape = config$shape %||% 3, size = config$size %||% 0.2) } + } else { + # For non-interactive plots, just add `geom_point` + plot <- plot + geom_point(shape = config$shape %||% 3, size = config$size %||% 0.2, + position = if (!is.null(config$position) && config$position == "jitter") "jitter" else "identity") } - # Add the base geom_point layer - plot <- plot + geom_point( - aes_params, shape = config$shape %||% 3, - size = config$size %||% 0.2, - position = if (!is.null(config$position) && config$position == "jitter") "jitter" else "identity") - # Add smooth line if specified if (!is.null(config$add_smooth) && config$add_smooth) { plot <- if (!is.null(config$lm_line)) { @@ -788,7 +777,7 @@ main <- function() { { non_finite_rows <- filter(., if_any(c(L), ~ !is.finite(.))) if (nrow(non_finite_rows) > 0) { - message("Removed the following non-finite rows:") + message("Filtering out the following non-finite rows:") print(non_finite_rows %>% select(any_of(print_vars)), n = 200) } filter(., if_all(c(L), is.finite))