Make y conditional in generate_plot()

This commit is contained in:
2024-09-01 04:41:27 -04:00
parent 48311fda3b
commit 73a890cd36

View File

@@ -157,18 +157,19 @@ update_gene_names <- function(df, sgd_gene_list) {
} }
generate_plot <- function(df, x_var, y_var = NULL, plot_type, color_var = "conc_num", title, x_label = NULL, y_label = NULL) { generate_plot <- function(df, x_var, y_var = NULL, plot_type, color_var = "conc_num", title, x_label = NULL, y_label = NULL) {
# Base ggplot object # Base ggplot object with conditional y mapping
if (!is.null(y_var)) { if (!is.null(y_var)) {
plot <- ggplot(df, aes(x = {{x_var}}, y = {{y_var}}, color = {{color_var}})) plot <- ggplot(df, aes(x = !!sym(x_var), y = !!sym(y_var), color = !!sym(color_var)))
} else { } else {
plot <- ggplot(df, aes(x = {{x_var}}, color = {{color_var}})) plot <- ggplot(df, aes(x = !!sym(x_var), color = !!sym(color_var)))
} }
# Handle different plot types # Handle different plot types
if (plot_type == "scatter") { if (plot_type == "scatter") {
plot <- plot + geom_point(shape = 3, size = 0.2, position = "jitter") + plot <- plot +
stat_summary(fun.data = mean_sdl, geom = "errorbar", aes(y = {{y_var}})) + geom_point(shape = 3, size = 0.2, position = "jitter") +
stat_summary(fun = mean, geom = "point", size = 0.6, aes(y = {{y_var}})) stat_summary(fun.data = mean_sdl, geom = "errorbar") +
stat_summary(fun = mean, geom = "point", size = 0.6)
} else if (plot_type == "box") { } else if (plot_type == "box") {
plot <- plot + geom_boxplot() plot <- plot + geom_boxplot()
} else if (plot_type == "density") { } else if (plot_type == "density") {
@@ -188,6 +189,7 @@ generate_plot <- function(df, x_var, y_var = NULL, plot_type, color_var = "conc_
} }
generate_and_save_plots <- function(df, output_dir, prefix, variables, include_qc = FALSE) { generate_and_save_plots <- function(df, output_dir, prefix, variables, include_qc = FALSE) {
plots <- list() plots <- list()