Add boxplots to interaction plots

This commit is contained in:
2024-09-09 23:34:18 -04:00
parent 356b47106e
commit 4e15fb80fb

View File

@@ -320,22 +320,22 @@ generate_and_save_plots <- function(output_dir, file_name, plot_configs, grid_la
if (config$y_var == "delta_bg" && config$plot_type == "scatter") { if (config$y_var == "delta_bg" && config$plot_type == "scatter") {
plot <- plot + geom_point(shape = 3, size = 0.2, position = "jitter") + plot <- plot + geom_point(shape = 3, size = 0.2, position = "jitter") +
geom_errorbar(aes(ymin = !!sym(y_mean_col) - !!sym(y_sd_col), geom_errorbar(aes(ymin = !!sym(y_mean_col) - !!sym(y_sd_col),
ymax = !!sym(y_mean_col) + !!sym(y_sd_col)), width = 0.1) + ymax = !!sym(y_mean_col) + !!sym(y_sd_col)), width = 0.1) +
geom_point(aes(y = !!sym(y_mean_col)), size = 0.6) geom_point(aes(y = !!sym(y_mean_col)), size = 0.6)
} else if (config$error_bar %||% FALSE) { } else if (config$error_bar %||% FALSE) {
plot <- plot + plot <- plot +
geom_point(shape = 3, size = 0.2) + geom_point(shape = 3, size = 0.2) +
geom_errorbar(aes(ymin = !!sym(y_mean_col) - !!sym(y_sd_col), geom_errorbar(aes(ymin = !!sym(y_mean_col) - !!sym(y_sd_col),
ymax = !!sym(y_mean_col) + !!sym(y_sd_col)), width = 0.1) + ymax = !!sym(y_mean_col) + !!sym(y_sd_col)), width = 0.1) +
geom_point(aes(y = !!sym(y_mean_col)), size = 0.6) geom_point(aes(y = !!sym(y_mean_col)), size = 0.6)
} }
} }
plot <- switch(config$plot_type, plot <- switch(config$plot_type,
"box" = plot + geom_boxplot(), "box" = plot + geom_boxplot(),
"density" = plot + geom_density(), "density" = plot + geom_density(),
"bar" = plot + geom_bar(stat = "identity"), "bar" = plot + geom_bar(stat = "identity"),
plot + geom_point() + geom_smooth(method = "lm", se = FALSE)) plot + geom_point() + geom_smooth(method = "lm", se = FALSE))
if (!is.null(config$ylim_vals)) { if (!is.null(config$ylim_vals)) {
plot <- plot + coord_cartesian(ylim = config$ylim_vals) plot <- plot + coord_cartesian(ylim = config$ylim_vals)
@@ -358,7 +358,7 @@ generate_and_save_plots <- function(output_dir, file_name, plot_configs, grid_la
# If grid_layout is provided, arrange plots in a grid and save in a single PDF # If grid_layout is provided, arrange plots in a grid and save in a single PDF
if (!is.null(grid_layout)) { if (!is.null(grid_layout)) {
pdf(file.path(output_dir, paste0(file_name, "_grid.pdf")), width = 14, height = 9) pdf(file.path(output_dir, paste0(file_name, ".pdf")), width = 14, height = 9)
# Loop through plots in chunks defined by ncol and nrow # Loop through plots in chunks defined by ncol and nrow
for (start_idx in seq(1, length(plots), by = grid_layout$ncol * grid_layout$nrow)) { for (start_idx in seq(1, length(plots), by = grid_layout$ncol * grid_layout$nrow)) {
@@ -426,7 +426,7 @@ interaction_plot_configs <- function(df, variables) {
ifelse(variable == "r", -0.45, -4500))), label = paste("SM =", df$SM)) ifelse(variable == "r", -0.45, -4500))), label = paste("SM =", df$SM))
) )
# Append a new plot configuration for each variable # Add scatter plot configuration for this variable
plot_configs[[length(plot_configs) + 1]] <- list( plot_configs[[length(plot_configs) + 1]] <- list(
df = df, df = df,
x_var = "conc_num_factor", x_var = "conc_num_factor",
@@ -443,11 +443,27 @@ interaction_plot_configs <- function(df, variables) {
x_labels = unique(as.character(df$conc_num)), x_labels = unique(as.character(df$conc_num)),
x_label = unique(df$Drug[1]) x_label = unique(df$Drug[1])
) )
# Add box plot configuration for this variable
plot_configs[[length(plot_configs) + 1]] <- list(
df = df,
x_var = "conc_num_factor",
y_var = variable,
plot_type = "box",
title = sprintf("%s %s (Boxplot)", df$OrfRep[1], df$Gene[1]),
ylim_vals = ylim_vals,
annotations = annotations,
error_bar = FALSE, # Boxplots typically don't need error bars
x_breaks = unique(df$conc_num_factor),
x_labels = unique(as.character(df$conc_num)),
x_label = unique(df$Drug[1])
)
} }
return(plot_configs) return(plot_configs)
} }
correlation_plot_configs <- function(df, lm_list, lm_summaries) { correlation_plot_configs <- function(df, lm_list, lm_summaries) {
lapply(seq_along(lm_list), function(i) { lapply(seq_along(lm_list), function(i) {
r_squared <- round(lm_summaries[[i]]$r.squared, 3) r_squared <- round(lm_summaries[[i]]$r.squared, 3)