Fix plate analysis plots, make error bar use aes color by default

This commit is contained in:
2024-10-04 03:33:07 -04:00
parent c4f398be82
commit 4a6890290e

View File

@@ -556,25 +556,41 @@ generate_and_save_plots <- function(out_dir, filename, plot_configs) {
# Add error bars if specified # Add error bars if specified
if (!is.null(config$error_bar) && config$error_bar) { if (!is.null(config$error_bar) && config$error_bar) {
error_bar_color <- if (!is.null(config$error_bar_params$color)) { # Check if a fixed color is provided or if it should come from a data column
config$error_bar_params$color error_bar_color <- config$error_bar_params$color
} else {
"red"
}
if (!is.null(config$error_bar_params$ymin) && !is.null(config$error_bar_params$ymax)) { if (!is.null(config$error_bar_params$ymin) && !is.null(config$error_bar_params$ymax)) {
# Check if ymin and ymax are constants or column names # Check if ymin and ymax are constants or column names
if (is.numeric(config$error_bar_params$ymin) && is.numeric(config$error_bar_params$ymax)) { if (is.numeric(config$error_bar_params$ymin) && is.numeric(config$error_bar_params$ymax)) {
plot <- plot + geom_errorbar(aes(x = .data[[config$x_var]]), plot <- plot + geom_errorbar(
aes(x = .data[[config$x_var]]),
ymin = config$error_bar_params$ymin, ymin = config$error_bar_params$ymin,
ymax = config$error_bar_params$ymax, ymax = config$error_bar_params$ymax
color = error_bar_color) )
# Apply fixed color if specified
if (!is.null(error_bar_color)) {
plot <- plot + scale_color_manual(values = error_bar_color)
}
} else { } else {
plot <- plot + geom_errorbar(aes( # If config$color_var exists, map the color aesthetic
if (!is.null(config$color_var)) {
plot <- plot + geom_errorbar(
aes(
x = .data[[config$x_var]],
ymin = .data[[config$error_bar_params$ymin]],
ymax = .data[[config$error_bar_params$ymax]],
color = .data[[config$color_var]] # Map color_var to data
)
)
} else {
plot <- plot + geom_errorbar(
aes(
x = .data[[config$x_var]], x = .data[[config$x_var]],
ymin = .data[[config$error_bar_params$ymin]], ymin = .data[[config$error_bar_params$ymin]],
ymax = .data[[config$error_bar_params$ymax]] ymax = .data[[config$error_bar_params$ymax]]
), color = error_bar_color) )
)
}
} }
} else { } else {
# Ensure the mean and sd columns exist # Ensure the mean and sd columns exist
@@ -582,11 +598,29 @@ generate_and_save_plots <- function(out_dir, filename, plot_configs) {
y_sd_col <- paste0("sd_", config$y_var) y_sd_col <- paste0("sd_", config$y_var)
if (y_mean_col %in% colnames(df) && y_sd_col %in% colnames(df)) { if (y_mean_col %in% colnames(df) && y_sd_col %in% colnames(df)) {
plot <- plot + geom_errorbar(aes( # If config$color_var exists, map the color aesthetic
if (!is.null(config$color_var)) {
plot <- plot + geom_errorbar(
aes(
x = .data[[config$x_var]],
ymin = .data[[y_mean_col]] - .data[[y_sd_col]],
ymax = .data[[y_mean_col]] + .data[[y_sd_col]],
color = .data[[config$color_var]] # Color based on aes()
)
)
} else {
plot <- plot + geom_errorbar(
aes(
x = .data[[config$x_var]], x = .data[[config$x_var]],
ymin = .data[[y_mean_col]] - .data[[y_sd_col]], ymin = .data[[y_mean_col]] - .data[[y_sd_col]],
ymax = .data[[y_mean_col]] + .data[[y_sd_col]] ymax = .data[[y_mean_col]] + .data[[y_sd_col]]
), color = error_bar_color) )
)
}
# Apply fixed color if specified
if (!is.null(error_bar_color)) {
plot <- plot + scale_color_manual(values = error_bar_color)
}
} }
} }
} }
@@ -734,7 +768,6 @@ generate_scatter_plot <- function(plot, config) {
} }
} }
# Set Y-axis limits if specified # Set Y-axis limits if specified
if (!is.null(config$ylim_vals)) { if (!is.null(config$ylim_vals)) {
plot <- plot + scale_y_continuous(limits = config$ylim_vals) plot <- plot + scale_y_continuous(limits = config$ylim_vals)
@@ -746,8 +779,8 @@ generate_scatter_plot <- function(plot, config) {
plot <- plot + plot <- plot +
annotate( annotate(
"text", "text",
x = annotation$x, x = ifelse(is.null(annotation$x), 0, annotation$x),
y = annotation$y, y = ifelse(is.null(annotation$y), 0, annotation$y),
label = annotation$label, label = annotation$label,
hjust = ifelse(is.null(annotation$hjust), 0.5, annotation$hjust), hjust = ifelse(is.null(annotation$hjust), 0.5, annotation$hjust),
vjust = ifelse(is.null(annotation$vjust), 0.5, annotation$vjust), vjust = ifelse(is.null(annotation$vjust), 0.5, annotation$vjust),
@@ -806,9 +839,9 @@ generate_plate_analysis_plot_configs <- function(variables, df_before = NULL, df
plot_type = plot_type, plot_type = plot_type,
title = paste("Plate analysis by Drug Conc for", var, stage, "quality control"), title = paste("Plate analysis by Drug Conc for", var, stage, "quality control"),
color_var = "conc_num_factor_factor", color_var = "conc_num_factor_factor",
position = if (plot_type == "scatter") "jitter" else NULL,
size = 0.2, size = 0.2,
error_bar = (plot_type == "scatter") error_bar = (plot_type == "scatter"),
legend_position = "bottom"
) )
# Add config to plots list # Add config to plots list
@@ -1340,6 +1373,7 @@ main <- function() {
list( list(
df = df_na_l_outside_2sd_k_stats, df = df_na_l_outside_2sd_k_stats,
x_var = "delta_bg", x_var = "delta_bg",
x_label = "Delta Background",
y_var = "K", y_var = "K",
plot_type = "scatter", plot_type = "scatter",
title = "Delta Background vs K for strains falling outside 2SD of the K mean at each Conc", title = "Delta Background vs K for strains falling outside 2SD of the K mean at each Conc",
@@ -1348,9 +1382,12 @@ main <- function() {
tooltip_vars = c("OrfRep", "Gene", "delta_bg"), tooltip_vars = c("OrfRep", "Gene", "delta_bg"),
annotations = list( annotations = list(
list( list(
x = median(df_na_l_outside_2sd_k_stats$delta_bg, na.rm = TRUE) / 2, x = 0.05,
y = median(df_na_l_outside_2sd_k_stats$K, na.rm = TRUE) / 2, y = 0.95,
label = paste("Total strains:", nrow(df_na_l_outside_2sd_k_stats)) hjust = 0,
vjust = 1,
label = paste("Total strains:", nrow(df_na_l_outside_2sd_k_stats)),
size = 5
) )
), ),
error_bar = FALSE, error_bar = FALSE,
@@ -1384,9 +1421,9 @@ main <- function() {
plot_configs = delta_bg_outside_2sd_k_plot_configs) plot_configs = delta_bg_outside_2sd_k_plot_configs)
) )
# furrr::future_map(plot_configs, function(config) { furrr::future_map(plot_configs, function(config) {
# generate_and_save_plots(config$out_dir, config$filename, config$plot_configs) generate_and_save_plots(config$out_dir, config$filename, config$plot_configs)
# }, .options = furrr_options(seed = TRUE)) }, .options = furrr_options(seed = TRUE))
bg_strains <- c("YDL227C") bg_strains <- c("YDL227C")
lapply(bg_strains, function(strain) { lapply(bg_strains, function(strain) {