Add pre-calculated error bars to scatter plots
This commit is contained in:
@@ -560,18 +560,30 @@ generate_scatter_plot <- function(plot, config) {
|
|||||||
|
|
||||||
# Add error bars if specified
|
# Add error bars if specified
|
||||||
if (!is.null(config$error_bar) && config$error_bar && !is.null(config$y_var)) {
|
if (!is.null(config$error_bar) && config$error_bar && !is.null(config$y_var)) {
|
||||||
y_mean_col <- paste0("mean_", config$y_var)
|
if (!is.null(config$error_bar_params)) {
|
||||||
y_sd_col <- paste0("sd_", config$y_var)
|
plot <- plot +
|
||||||
|
geom_errorbar(
|
||||||
plot <- plot +
|
aes(
|
||||||
geom_errorbar(
|
ymin = config$error_bar_params$ymin,
|
||||||
aes(
|
ymax = config$error_bar_params$ymax
|
||||||
ymin = !!sym(y_mean_col) - !!sym(y_sd_col),
|
),
|
||||||
ymax = !!sym(y_mean_col) + !!sym(y_sd_col)
|
alpha = 0.3,
|
||||||
),
|
linewidth = 0.5
|
||||||
alpha = 0.3,
|
)
|
||||||
linewidth = 0.5
|
} else {
|
||||||
)
|
y_mean_col <- paste0("mean_", config$y_var)
|
||||||
|
y_sd_col <- paste0("sd_", config$y_var)
|
||||||
|
|
||||||
|
plot <- plot +
|
||||||
|
geom_errorbar(
|
||||||
|
aes(
|
||||||
|
ymin = !!sym(y_mean_col) - !!sym(y_sd_col),
|
||||||
|
ymax = !!sym(y_mean_col) + !!sym(y_sd_col)
|
||||||
|
),
|
||||||
|
alpha = 0.3,
|
||||||
|
linewidth = 0.5
|
||||||
|
)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
# Customize X-axis if specified
|
# Customize X-axis if specified
|
||||||
@@ -675,9 +687,10 @@ generate_interaction_plot_configs <- function(df, limits_map = NULL, plot_type =
|
|||||||
df_filtered <- df %>%
|
df_filtered <- df %>%
|
||||||
mutate(OrfRepCombined = if (plot_type == "reference") paste(OrfRep, Gene, num, sep = "_") else paste(OrfRep, Gene, sep = "_"))
|
mutate(OrfRepCombined = if (plot_type == "reference") paste(OrfRep, Gene, num, sep = "_") else paste(OrfRep, Gene, sep = "_"))
|
||||||
|
|
||||||
plots <- list()
|
# Separate the plots into two groups: overall variables and delta comparisons
|
||||||
|
overall_plots <- list()
|
||||||
|
delta_plots <- list()
|
||||||
|
|
||||||
# Generate plots for overall variables (L, K, r, AUC)
|
|
||||||
for (var in c("L", "K", "r", "AUC")) {
|
for (var in c("L", "K", "r", "AUC")) {
|
||||||
y_limits <- limits_map[[var]]
|
y_limits <- limits_map[[var]]
|
||||||
|
|
||||||
@@ -692,14 +705,12 @@ generate_interaction_plot_configs <- function(df, limits_map = NULL, plot_type =
|
|||||||
error_bar = TRUE,
|
error_bar = TRUE,
|
||||||
x_breaks = unique(df_filtered$conc_num_factor_factor),
|
x_breaks = unique(df_filtered$conc_num_factor_factor),
|
||||||
x_labels = as.character(unique(df_filtered$conc_num)),
|
x_labels = as.character(unique(df_filtered$conc_num)),
|
||||||
grid_layout = list(ncol = 2, nrow = 2),
|
|
||||||
position = "jitter",
|
position = "jitter",
|
||||||
smooth = TRUE
|
smooth = TRUE
|
||||||
)
|
)
|
||||||
plots <- append(plots, list(config))
|
overall_plots <- append(overall_plots, list(config))
|
||||||
}
|
}
|
||||||
|
|
||||||
# Generate Delta comparison plots
|
|
||||||
unique_groups <- df_filtered %>% select(all_of(group_vars)) %>% distinct()
|
unique_groups <- df_filtered %>% select(all_of(group_vars)) %>% distinct()
|
||||||
|
|
||||||
for (i in seq_len(nrow(unique_groups))) {
|
for (i in seq_len(nrow(unique_groups))) {
|
||||||
@@ -713,13 +724,13 @@ generate_interaction_plot_configs <- function(df, limits_map = NULL, plot_type =
|
|||||||
for (var in c("Delta_L", "Delta_K", "Delta_r", "Delta_AUC")) {
|
for (var in c("Delta_L", "Delta_K", "Delta_r", "Delta_AUC")) {
|
||||||
y_limits <- limits_map[[var]]
|
y_limits <- limits_map[[var]]
|
||||||
y_span <- y_limits[2] - y_limits[1]
|
y_span <- y_limits[2] - y_limits[1]
|
||||||
|
|
||||||
# Error bars
|
# Error bars
|
||||||
WT_sd_var <- paste0("WT_sd_", sub("Delta_", "", var))
|
WT_sd_var <- paste0("WT_sd_", sub("Delta_", "", var))
|
||||||
WT_sd_value <- group_data[[WT_sd_var]][1]
|
WT_sd_value <- group_data[[WT_sd_var]][1]
|
||||||
error_bar_ymin <- 0 - (2 * WT_sd_value)
|
error_bar_ymin <- 0 - (2 * WT_sd_value)
|
||||||
error_bar_ymax <- 0 + (2 * WT_sd_value)
|
error_bar_ymax <- 0 + (2 * WT_sd_value)
|
||||||
|
|
||||||
# Annotations
|
# Annotations
|
||||||
Z_Shift_value <- round(group_data[[paste0("Z_Shift_", sub("Delta_", "", var))]][1], 2)
|
Z_Shift_value <- round(group_data[[paste0("Z_Shift_", sub("Delta_", "", var))]][1], 2)
|
||||||
Z_lm_value <- round(group_data[[paste0("Z_lm_", sub("Delta_", "", var))]][1], 2)
|
Z_lm_value <- round(group_data[[paste0("Z_lm_", sub("Delta_", "", var))]][1], 2)
|
||||||
@@ -752,14 +763,16 @@ generate_interaction_plot_configs <- function(df, limits_map = NULL, plot_type =
|
|||||||
smooth = TRUE,
|
smooth = TRUE,
|
||||||
x_breaks = unique(group_data$conc_num_factor_factor),
|
x_breaks = unique(group_data$conc_num_factor_factor),
|
||||||
x_labels = as.character(unique(group_data$conc_num)),
|
x_labels = as.character(unique(group_data$conc_num)),
|
||||||
ylim_vals = y_limits,
|
ylim_vals = y_limits
|
||||||
grid_layout = list(ncol = 4, nrow = 3)
|
|
||||||
)
|
)
|
||||||
plots <- append(plots, list(config))
|
delta_plots <- append(delta_plots, list(config))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return(list(grid_layout = list(ncol = 2, nrow = 2), plots = plots))
|
return(list(
|
||||||
|
overall_plots = list(grid_layout = list(ncol = 2, nrow = 2), plots = overall_plots),
|
||||||
|
delta_plots = list(grid_layout = list(ncol = 4, nrow = 3), plots = delta_plots)
|
||||||
|
))
|
||||||
}
|
}
|
||||||
|
|
||||||
generate_rank_plot_configs <- function(df, variables, is_lm = FALSE, adjust = FALSE, overlap_color = FALSE) {
|
generate_rank_plot_configs <- function(df, variables, is_lm = FALSE, adjust = FALSE, overlap_color = FALSE) {
|
||||||
@@ -1046,7 +1059,6 @@ main <- function() {
|
|||||||
|
|
||||||
# Each plots list corresponds to a file
|
# Each plots list corresponds to a file
|
||||||
l_vs_k_plot_configs <- list(
|
l_vs_k_plot_configs <- list(
|
||||||
grid_layout = list(ncol = 1, nrow = 1),
|
|
||||||
plots = list(
|
plots = list(
|
||||||
list(
|
list(
|
||||||
df = df,
|
df = df,
|
||||||
@@ -1063,7 +1075,6 @@ main <- function() {
|
|||||||
)
|
)
|
||||||
|
|
||||||
frequency_delta_bg_plot_configs <- list(
|
frequency_delta_bg_plot_configs <- list(
|
||||||
grid_layout = list(ncol = 1, nrow = 1),
|
|
||||||
plots = list(
|
plots = list(
|
||||||
list(
|
list(
|
||||||
df = df_stats,
|
df = df_stats,
|
||||||
@@ -1093,7 +1104,6 @@ main <- function() {
|
|||||||
)
|
)
|
||||||
|
|
||||||
above_threshold_plot_configs <- list(
|
above_threshold_plot_configs <- list(
|
||||||
grid_layout = list(ncol = 1, nrow = 1),
|
|
||||||
plots = list(
|
plots = list(
|
||||||
list(
|
list(
|
||||||
df = df_above_tolerance,
|
df = df_above_tolerance,
|
||||||
@@ -1145,7 +1155,6 @@ main <- function() {
|
|||||||
)
|
)
|
||||||
|
|
||||||
l_outside_2sd_k_plot_configs <- list(
|
l_outside_2sd_k_plot_configs <- list(
|
||||||
grid_layout = list(ncol = 1, nrow = 1), # Ensures it's compatible with generate_and_save_plots
|
|
||||||
plots = list(
|
plots = list(
|
||||||
list(
|
list(
|
||||||
df = df_na_l_outside_2sd_k_stats,
|
df = df_na_l_outside_2sd_k_stats,
|
||||||
@@ -1170,7 +1179,6 @@ main <- function() {
|
|||||||
)
|
)
|
||||||
|
|
||||||
delta_bg_outside_2sd_k_plot_configs <- list(
|
delta_bg_outside_2sd_k_plot_configs <- list(
|
||||||
grid_layout = list(ncol = 1, nrow = 1), # Ensures it's compatible with generate_and_save_plots
|
|
||||||
plots = list(
|
plots = list(
|
||||||
list(
|
list(
|
||||||
df = df_na_l_outside_2sd_k_stats,
|
df = df_na_l_outside_2sd_k_stats,
|
||||||
@@ -1195,37 +1203,34 @@ main <- function() {
|
|||||||
)
|
)
|
||||||
|
|
||||||
message("Generating quality control plots in parallel")
|
message("Generating quality control plots in parallel")
|
||||||
|
|
||||||
generate_and_save_plots(out_dir_qc, "L_vs_K_before_quality_control", l_vs_k_plot_configs)
|
|
||||||
quit()
|
|
||||||
# # future::plan(future::multicore, workers = parallel::detectCores())
|
# # future::plan(future::multicore, workers = parallel::detectCores())
|
||||||
# future::plan(future::multisession, workers = 3) # generate 3 plots in parallel
|
future::plan(future::multisession, workers = 3) # generate 3 plots in parallel
|
||||||
|
|
||||||
# plot_configs <- list(
|
plot_configs <- list(
|
||||||
# list(out_dir = out_dir_qc, filename = "L_vs_K_before_quality_control",
|
list(out_dir = out_dir_qc, filename = "L_vs_K_before_quality_control",
|
||||||
# plot_configs = l_vs_k_plot_configs),
|
plot_configs = l_vs_k_plot_configs),
|
||||||
# list(out_dir = out_dir_qc, filename = "frequency_delta_background",
|
list(out_dir = out_dir_qc, filename = "frequency_delta_background",
|
||||||
# plot_configs = frequency_delta_bg_plot_configs),
|
plot_configs = frequency_delta_bg_plot_configs),
|
||||||
# list(out_dir = out_dir_qc, filename = "L_vs_K_above_threshold",
|
list(out_dir = out_dir_qc, filename = "L_vs_K_above_threshold",
|
||||||
# plot_configs = above_threshold_plot_configs),
|
plot_configs = above_threshold_plot_configs),
|
||||||
# list(out_dir = out_dir_qc, filename = "plate_analysis",
|
list(out_dir = out_dir_qc, filename = "plate_analysis",
|
||||||
# plot_configs = plate_analysis_plot_configs),
|
plot_configs = plate_analysis_plot_configs),
|
||||||
# list(out_dir = out_dir_qc, filename = "plate_analysis_boxplots",
|
list(out_dir = out_dir_qc, filename = "plate_analysis_boxplots",
|
||||||
# plot_configs = plate_analysis_boxplot_configs),
|
plot_configs = plate_analysis_boxplot_configs),
|
||||||
# list(out_dir = out_dir_qc, filename = "plate_analysis_no_zeros",
|
list(out_dir = out_dir_qc, filename = "plate_analysis_no_zeros",
|
||||||
# plot_configs = plate_analysis_no_zeros_plot_configs),
|
plot_configs = plate_analysis_no_zeros_plot_configs),
|
||||||
# list(out_dir = out_dir_qc, filename = "plate_analysis_no_zeros_boxplots",
|
list(out_dir = out_dir_qc, filename = "plate_analysis_no_zeros_boxplots",
|
||||||
# plot_configs = plate_analysis_no_zeros_boxplot_configs),
|
plot_configs = plate_analysis_no_zeros_boxplot_configs),
|
||||||
# list(out_dir = out_dir_qc, filename = "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),
|
plot_configs = l_outside_2sd_k_plot_configs),
|
||||||
# list(out_dir = out_dir_qc, filename = "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)
|
plot_configs = delta_bg_outside_2sd_k_plot_configs)
|
||||||
# )
|
)
|
||||||
|
|
||||||
# # Generating quality control plots in parallel
|
# Generating quality control plots in parallel
|
||||||
# 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))
|
||||||
|
|
||||||
# Process background strains
|
# Process background strains
|
||||||
bg_strains <- c("YDL227C")
|
bg_strains <- c("YDL227C")
|
||||||
|
|||||||
Reference in New Issue
Block a user