From 625612727977e1f63155600b1cc7b81d4c120208 Mon Sep 17 00:00:00 2001 From: Bryan Roessler Date: Wed, 25 Sep 2024 22:45:39 -0400 Subject: [PATCH] Break out aes mappings --- .../apps/r/calculate_interaction_zscores.R | 50 ++++++++++++------- 1 file changed, 31 insertions(+), 19 deletions(-) diff --git a/qhtcp-workflow/apps/r/calculate_interaction_zscores.R b/qhtcp-workflow/apps/r/calculate_interaction_zscores.R index 2d05e2e9..d46c39a4 100644 --- a/qhtcp-workflow/apps/r/calculate_interaction_zscores.R +++ b/qhtcp-workflow/apps/r/calculate_interaction_zscores.R @@ -381,19 +381,27 @@ generate_and_save_plots <- function(out_dir, filename, plot_configs, grid_layout config <- plot_configs[[i]] df <- config$df - aes_mapping <- if (config$plot_type == "bar" || config$plot_type == "density") { - if (is.null(config$color_var)) { - aes(x = .data[[config$x_var]]) + # Create the base plot + aes_mapping <- if (config$plot_type == "bar") { + if (!is.null(config$color_var)) { + aes(x = .data[[config$x_var]], fill = .data[[config$color_var]], color = .data[[config$color_var]]) } else { - aes(x = .data[[config$x_var]], color = as.factor(.data[[config$color_var]])) + aes(x = .data[[config$x_var]]) + } + } else if (config$plot_type == "density") { + if (!is.null(config$color_var)) { + aes(x = .data[[config$x_var]], color = .data[[config$color_var]]) + } else { + aes(x = .data[[config$x_var]]) } - } else 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]])) + if (!is.null(config$color_var)) { + aes(x = .data[[config$x_var]], y = .data[[config$y_var]], color = .data[[config$color_var]]) + } else { + aes(x = .data[[config$x_var]], y = .data[[config$y_var]]) + } } - # Start building the plot with aes_mapping plot_base <- ggplot(df, aes_mapping) # Use appropriate helper function based on plot type @@ -437,12 +445,14 @@ generate_and_save_plots <- function(out_dir, filename, plot_configs, grid_layout } } - # Convert to plotly object - if (length(tooltip_vars) > 0) { - plotly_plot <- ggplotly(plot, tooltip = tooltip_vars) - } else { - plotly_plot <- ggplotly(plot, tooltip = "none") - } + # Convert to plotly object and suppress warnings here + plotly_plot <- suppressWarnings({ + if (length(tooltip_vars) > 0) { + ggplotly(plot, tooltip = tooltip_vars) + } else { + ggplotly(plot, tooltip = "none") + } + }) # Adjust legend position if specified if (!is.null(config$legend_position) && config$legend_position == "bottom") { @@ -454,22 +464,24 @@ generate_and_save_plots <- function(out_dir, filename, plot_configs, grid_layout plotly_plots[[i]] <- plotly_plot } - # Save static PDF plots + # Save static PDF plot(s) pdf(file.path(out_dir, paste0(filename, ".pdf")), width = 14, height = 9) lapply(static_plots, print) dev.off() - # Combine and save interactive HTML plots + # Combine and save interactive HTML plot(s) combined_plot <- subplot( plotly_plots, nrows = if (!is.null(grid_layout) && !is.null(grid_layout$nrow)) { grid_layout$nrow } else { - # Calculate nrow based on the length of plotly_plots (default 1 row if only one plot) + # Calculate nrow based on the length of plotly_plots ceiling(length(plotly_plots) / ifelse(!is.null(grid_layout) && !is.null(grid_layout$ncol), grid_layout$ncol, 1)) }, margin = 0.05 ) + + # Save combined html plot(s) saveWidget(combined_plot, file = file.path(out_dir, paste0(filename, ".html")), selfcontained = TRUE) } @@ -1241,9 +1253,9 @@ main <- function() { plot_configs = plate_analysis_no_zeros_plot_configs), list(out_dir = out_dir_qc, filename = "plate_analysis_no_zeros_boxplots", plot_configs = plate_analysis_no_zeros_boxplot_configs), - list(out_dir = out_dir_qc, name = "L_vs_K_for_strains_2SD_outside_mean_K", + list(out_dir = out_dir_qc, filename = "L_vs_K_for_strains_2SD_outside_mean_K", plot_configs = l_outside_2sd_k_plot_configs), - list(out_dir = out_dir_qc, name = "delta_background_vs_K_for_strains_2sd_outside_mean_K", + list(out_dir = out_dir_qc, filename = "delta_background_vs_K_for_strains_2sd_outside_mean_K", plot_configs = delta_bg_outside_2sd_k_plot_configs) )