Break out aes mappings

This commit is contained in:
2024-09-25 22:45:39 -04:00
parent a45b977b0d
commit 6256127279

View File

@@ -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)) {
# 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]])
} else {
aes(x = .data[[config$x_var]], color = as.factor(.data[[config$color_var]]))
}
} else if (is.null(config$color_var)) {
aes(x = .data[[config$x_var]], y = .data[[config$y_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]], y = .data[[config$y_var]], color = as.factor(.data[[config$color_var]]))
aes(x = .data[[config$x_var]])
}
} else {
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
# Convert to plotly object and suppress warnings here
plotly_plot <- suppressWarnings({
if (length(tooltip_vars) > 0) {
plotly_plot <- ggplotly(plot, tooltip = tooltip_vars)
ggplotly(plot, tooltip = tooltip_vars)
} else {
plotly_plot <- ggplotly(plot, tooltip = "none")
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)
)