Use as.factor(conc_num) for plots

This commit is contained in:
2024-09-01 15:40:29 -04:00
parent a4de405b75
commit 1fa8fc1552

View File

@@ -158,13 +158,10 @@ 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 with conditional y mapping # Base ggplot object with conditional y mapping
if (plot_type == "box") { if (!is.null(y_var)) {
# Ensure x_var is treated as a factor for box plots plot <- ggplot(df, aes(x = !!sym(x_var), y = !!sym(y_var), color = as.factor(!!sym(color_var))))
plot <- ggplot(df, aes(x = as.factor(!!sym(x_var)), y = !!sym(y_var), color = !!sym(color_var)))
} else if (!is.null(y_var)) {
plot <- ggplot(df, aes(x = !!sym(x_var), y = !!sym(y_var), color = !!sym(color_var)))
} else { } else {
plot <- ggplot(df, aes(x = !!sym(x_var), color = !!sym(color_var))) plot <- ggplot(df, aes(x = !!sym(x_var), color = as.factor(!!sym(color_var))))
} }
# Handle different plot types # Handle different plot types
@@ -176,7 +173,7 @@ generate_plot <- function(df, x_var, y_var = NULL, plot_type, color_var = "conc_
} 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") {
plot <- plot + geom_density() # Density plot automatically maps density to y plot <- plot + geom_density()
} else if (plot_type == "bar") { } else if (plot_type == "bar") {
plot <- plot + geom_bar(stat = "identity") plot <- plot + geom_bar(stat = "identity")
} }