From d61c7219d77d44aa6a8b3559cf64ef502c379e6b Mon Sep 17 00:00:00 2001 From: Bryan Roessler Date: Sun, 1 Sep 2024 16:16:07 -0400 Subject: [PATCH] Add horizontal legend support to plotly --- workflow/apps/r/calculate_interaction_zscores5.R | 16 ++++++++++++---- 1 file changed, 12 insertions(+), 4 deletions(-) diff --git a/workflow/apps/r/calculate_interaction_zscores5.R b/workflow/apps/r/calculate_interaction_zscores5.R index b6c87c1b..8e96c4d9 100644 --- a/workflow/apps/r/calculate_interaction_zscores5.R +++ b/workflow/apps/r/calculate_interaction_zscores5.R @@ -61,7 +61,6 @@ args <- parse_arguments() dir.create(file.path(args$out_dir, "zscores"), showWarnings = FALSE) dir.create(file.path(args$out_dir, "zscores", "qc"), showWarnings = FALSE) -# Define themes and scales # Define themes and scales theme_publication <- function(base_size = 14, base_family = "sans", legend_position = "bottom") { theme_foundation <- ggplot2::theme_grey(base_size = base_size, base_family = base_family) @@ -175,7 +174,11 @@ generate_plot <- function(df, x_var, y_var = NULL, plot_type, color_var = "conc_ } else if (plot_type == "density") { plot <- plot + geom_density() } else if (plot_type == "bar") { - plot <- plot + geom_bar(stat = "identity") + if (!is.null(y_var)) { + plot <- plot + geom_bar(stat = "identity") # Use y aesthetic if provided + } else { + plot <- plot + geom_bar() # Default to counting occurrences + } } # Add titles and labels @@ -188,6 +191,7 @@ generate_plot <- function(df, x_var, y_var = NULL, plot_type, color_var = "conc_ return(plot) } + generate_and_save_plots <- function(df, output_dir, prefix, variables, include_qc = FALSE) { plots <- list() @@ -264,16 +268,20 @@ save_plots <- function(file_name, plot_list, output_dir) { }) dev.off() - # Save to HTML + # Save to HTML with horizontal legend orientation lapply(names(plot_list), function(plot_name) { + pgg <- suppressWarnings(ggplotly(plot_list[[plot_name]]) %>% + layout(legend = list(orientation = "h"))) + saveWidget( - ggplotly(plot_list[[plot_name]]), + pgg, file = file.path(output_dir, paste0(file_name, "_", plot_name, ".html")), selfcontained = TRUE) }) } + # Calculate background strain mean values calculate_background_means <- function(df_stats_by_l, df_stats_by_k, df_stats_by_r, df_stats_by_auc) { list(