Begin scatter plot refactoring

This commit is contained in:
2024-09-13 20:52:18 -04:00
parent 557d85bc85
commit 30b0f175ff

View File

@@ -411,19 +411,23 @@ generate_and_save_plots <- function(output_dir, file_name, plot_configs, grid_la
saveWidget(combined_plot, file = file.path(output_dir, paste0(file_name, ".html")), selfcontained = TRUE)
}
generate_scatter_plot <- function(plot, config) {
generate_scatter_plot <- function(plot, config, interactive = FALSE) {
# Add the interactive text aesthetics to the scatter points
plot <-
if (!is.null(config$delta_bg_point) && config$delta_bg_point) {
# Add the interactive `text` aesthetic if `interactive` is TRUE
if (interactive) {
plot <- if (!is.null(config$delta_bg_point) && config$delta_bg_point) {
plot + geom_point(aes(text = paste("ORF:", OrfRep, "Gene:", Gene, "delta_bg:", delta_bg)),
shape = config$shape %||% 3, size = config$size %||% 0.2)
} else if (!is.null(config$gene_point) && config$gene_point) {
plot + geom_point(aes(text = paste("ORF:", OrfRep, "Gene:", Gene)),
shape = config$shape %||% 3, size = config$size %||% 0.2, position = "jitter")
} else {
plot + geom_point(shape = config$shape %||% 3, size = config$size %||% 0.2,
position = config$position %||% "identity")
plot + geom_point(shape = config$shape %||% 3, size = config$size %||% 0.2)
}
} else {
# For non-interactive plots, just add `geom_point`
plot <- plot + geom_point(shape = config$shape %||% 3, size = config$size %||% 0.2,
position = if (!is.null(config$position) && config$position == "jitter") "jitter" else "identity")
}
# Add smooth line if specified
@@ -572,18 +576,29 @@ generate_interaction_plot_configs <- function(df, variables) {
slope = coef(var_info$lm_model)[2]
)
# Dynamically create annotations based on variable
# Dynamically create annotations based on variable
annotations <- lapply(names(annotation_positions[[variable]]), function(annotation_name) {
y_pos <- annotation_positions[[variable]][[annotation_name]]
# Check if the annotation_name exists in annotation_labels
if (!is.null(annotation_labels[[annotation_name]])) {
label <- annotation_labels[[annotation_name]](df, variable)
list(x = 1, y = y_pos, label = label)
} else {
message(paste("Warning: No annotation function found for", annotation_name))
NULL
}
})
# Filter out any NULL annotations
annotations <- Filter(Negate(is.null), annotations)
# Add scatter plot configuration for this variable
configs[[length(configs) + 1]] <- list(
df = df,
x_var = "conc_num_factor",
y_var = var_info$delta_var,
y_var = variable,
plot_type = "scatter",
title = sprintf("%s %s", df$OrfRep[1], df$Gene[1]),
ylim_vals = var_info$ylim,
@@ -593,8 +608,6 @@ generate_interaction_plot_configs <- function(df, variables) {
x_breaks = unique(df$conc_num_factor),
x_labels = unique(as.character(df$conc_num)),
x_label = unique(df$Drug[1]),
shape = 3,
size = 0.6,
position = "jitter",
coord_cartesian = c(0, max(var_info$ylim)) # You can customize this per plot as needed
)