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

Vedi File

@@ -556,37 +556,71 @@ generate_and_save_plots <- function(out_dir, filename, plot_configs) {
# Add error bars if specified
if (!is.null(config$error_bar) && config$error_bar) {
error_bar_color <- if (!is.null(config$error_bar_params$color)) {
config$error_bar_params$color
} else {
"red"
}
# Check if a fixed color is provided or if it should come from a data column
error_bar_color <- config$error_bar_params$color
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
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,
ymax = config$error_bar_params$ymax,
color = error_bar_color)
ymax = config$error_bar_params$ymax
)
# Apply fixed color if specified
if (!is.null(error_bar_color)) {
plot <- plot + scale_color_manual(values = error_bar_color)
}
} else {
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 = error_bar_color)
# 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]],
ymin = .data[[config$error_bar_params$ymin]],
ymax = .data[[config$error_bar_params$ymax]]
)
)
}
}
} else {
# Ensure the mean and sd columns exist
y_mean_col <- paste0("mean_", config$y_var)
y_sd_col <- paste0("sd_", config$y_var)
if (y_mean_col %in% colnames(df) && y_sd_col %in% colnames(df)) {
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 = error_bar_color)
# 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]],
ymin = .data[[y_mean_col]] - .data[[y_sd_col]],
ymax = .data[[y_mean_col]] + .data[[y_sd_col]]
)
)
}
# 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
if (!is.null(config$ylim_vals)) {
plot <- plot + scale_y_continuous(limits = config$ylim_vals)
@@ -746,8 +779,8 @@ generate_scatter_plot <- function(plot, config) {
plot <- plot +
annotate(
"text",
x = annotation$x,
y = annotation$y,
x = ifelse(is.null(annotation$x), 0, annotation$x),
y = ifelse(is.null(annotation$y), 0, annotation$y),
label = annotation$label,
hjust = ifelse(is.null(annotation$hjust), 0.5, annotation$hjust),
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,
title = paste("Plate analysis by Drug Conc for", var, stage, "quality control"),
color_var = "conc_num_factor_factor",
position = if (plot_type == "scatter") "jitter" else NULL,
size = 0.2,
error_bar = (plot_type == "scatter")
error_bar = (plot_type == "scatter"),
legend_position = "bottom"
)
# Add config to plots list
@@ -1340,6 +1373,7 @@ main <- function() {
list(
df = df_na_l_outside_2sd_k_stats,
x_var = "delta_bg",
x_label = "Delta Background",
y_var = "K",
plot_type = "scatter",
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"),
annotations = list(
list(
x = median(df_na_l_outside_2sd_k_stats$delta_bg, na.rm = TRUE) / 2,
y = median(df_na_l_outside_2sd_k_stats$K, na.rm = TRUE) / 2,
label = paste("Total strains:", nrow(df_na_l_outside_2sd_k_stats))
x = 0.05,
y = 0.95,
hjust = 0,
vjust = 1,
label = paste("Total strains:", nrow(df_na_l_outside_2sd_k_stats)),
size = 5
)
),
error_bar = FALSE,
@@ -1384,9 +1421,9 @@ main <- function() {
plot_configs = delta_bg_outside_2sd_k_plot_configs)
)
# furrr::future_map(plot_configs, function(config) {
# generate_and_save_plots(config$out_dir, config$filename, config$plot_configs)
# }, .options = furrr_options(seed = TRUE))
furrr::future_map(plot_configs, function(config) {
generate_and_save_plots(config$out_dir, config$filename, config$plot_configs)
}, .options = furrr_options(seed = TRUE))
bg_strains <- c("YDL227C")
lapply(bg_strains, function(strain) {