Add horizontal legend support to plotly

This commit is contained in:
2024-09-01 16:16:07 -04:00
parent 1fa8fc1552
commit d61c7219d7

View File

@@ -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"), showWarnings = FALSE)
dir.create(file.path(args$out_dir, "zscores", "qc"), showWarnings = FALSE) dir.create(file.path(args$out_dir, "zscores", "qc"), showWarnings = FALSE)
# Define themes and scales
# Define themes and scales # Define themes and scales
theme_publication <- function(base_size = 14, base_family = "sans", legend_position = "bottom") { 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) 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") { } else if (plot_type == "density") {
plot <- plot + geom_density() plot <- plot + geom_density()
} else if (plot_type == "bar") { } 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 # Add titles and labels
@@ -188,6 +191,7 @@ generate_plot <- function(df, x_var, y_var = NULL, plot_type, color_var = "conc_
return(plot) return(plot)
} }
generate_and_save_plots <- function(df, output_dir, prefix, variables, include_qc = FALSE) { generate_and_save_plots <- function(df, output_dir, prefix, variables, include_qc = FALSE) {
plots <- list() plots <- list()
@@ -264,16 +268,20 @@ save_plots <- function(file_name, plot_list, output_dir) {
}) })
dev.off() dev.off()
# Save to HTML # Save to HTML with horizontal legend orientation
lapply(names(plot_list), function(plot_name) { lapply(names(plot_list), function(plot_name) {
pgg <- suppressWarnings(ggplotly(plot_list[[plot_name]]) %>%
layout(legend = list(orientation = "h")))
saveWidget( saveWidget(
ggplotly(plot_list[[plot_name]]), pgg,
file = file.path(output_dir, file = file.path(output_dir,
paste0(file_name, "_", plot_name, ".html")), paste0(file_name, "_", plot_name, ".html")),
selfcontained = TRUE) selfcontained = TRUE)
}) })
} }
# Calculate background strain mean values # 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) { calculate_background_means <- function(df_stats_by_l, df_stats_by_k, df_stats_by_r, df_stats_by_auc) {
list( list(