Begin scatter plot refactoring
This commit is contained in:
@@ -411,20 +411,24 @@ 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)
|
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
|
# Add the interactive `text` aesthetic if `interactive` is TRUE
|
||||||
plot <-
|
if (interactive) {
|
||||||
if (!is.null(config$delta_bg_point) && config$delta_bg_point) {
|
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)),
|
plot + geom_point(aes(text = paste("ORF:", OrfRep, "Gene:", Gene, "delta_bg:", delta_bg)),
|
||||||
shape = config$shape %||% 3, size = config$size %||% 0.2)
|
shape = config$shape %||% 3, size = config$size %||% 0.2)
|
||||||
} else if (!is.null(config$gene_point) && config$gene_point) {
|
} else if (!is.null(config$gene_point) && config$gene_point) {
|
||||||
plot + geom_point(aes(text = paste("ORF:", OrfRep, "Gene:", Gene)),
|
plot + geom_point(aes(text = paste("ORF:", OrfRep, "Gene:", Gene)),
|
||||||
shape = config$shape %||% 3, size = config$size %||% 0.2, position = "jitter")
|
shape = config$shape %||% 3, size = config$size %||% 0.2, position = "jitter")
|
||||||
} else {
|
} else {
|
||||||
plot + geom_point(shape = config$shape %||% 3, size = config$size %||% 0.2,
|
plot + geom_point(shape = config$shape %||% 3, size = config$size %||% 0.2)
|
||||||
position = config$position %||% "identity")
|
|
||||||
}
|
}
|
||||||
|
} 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
|
# Add smooth line if specified
|
||||||
if (!is.null(config$add_smooth) && config$add_smooth) {
|
if (!is.null(config$add_smooth) && config$add_smooth) {
|
||||||
@@ -572,18 +576,29 @@ generate_interaction_plot_configs <- function(df, variables) {
|
|||||||
slope = coef(var_info$lm_model)[2]
|
slope = coef(var_info$lm_model)[2]
|
||||||
)
|
)
|
||||||
|
|
||||||
|
# Dynamically create annotations based on variable
|
||||||
# Dynamically create annotations based on variable
|
# Dynamically create annotations based on variable
|
||||||
annotations <- lapply(names(annotation_positions[[variable]]), function(annotation_name) {
|
annotations <- lapply(names(annotation_positions[[variable]]), function(annotation_name) {
|
||||||
y_pos <- annotation_positions[[variable]][[annotation_name]]
|
y_pos <- annotation_positions[[variable]][[annotation_name]]
|
||||||
label <- annotation_labels[[annotation_name]](df, variable)
|
|
||||||
list(x = 1, y = y_pos, label = label)
|
# 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
|
# Add scatter plot configuration for this variable
|
||||||
configs[[length(configs) + 1]] <- list(
|
configs[[length(configs) + 1]] <- list(
|
||||||
df = df,
|
df = df,
|
||||||
x_var = "conc_num_factor",
|
x_var = "conc_num_factor",
|
||||||
y_var = var_info$delta_var,
|
y_var = variable,
|
||||||
plot_type = "scatter",
|
plot_type = "scatter",
|
||||||
title = sprintf("%s %s", df$OrfRep[1], df$Gene[1]),
|
title = sprintf("%s %s", df$OrfRep[1], df$Gene[1]),
|
||||||
ylim_vals = var_info$ylim,
|
ylim_vals = var_info$ylim,
|
||||||
@@ -593,8 +608,6 @@ generate_interaction_plot_configs <- function(df, variables) {
|
|||||||
x_breaks = unique(df$conc_num_factor),
|
x_breaks = unique(df$conc_num_factor),
|
||||||
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]),
|
||||||
shape = 3,
|
|
||||||
size = 0.6,
|
|
||||||
position = "jitter",
|
position = "jitter",
|
||||||
coord_cartesian = c(0, max(var_info$ylim)) # You can customize this per plot as needed
|
coord_cartesian = c(0, max(var_info$ylim)) # You can customize this per plot as needed
|
||||||
)
|
)
|
||||||
|
|||||||
Reference in New Issue
Block a user