More aes tweaks for color_var

This commit is contained in:
2024-09-12 23:44:48 -04:00
parent 1a2cd1a6c2
commit a018656f7f

View File

@@ -391,20 +391,13 @@ generate_and_save_plots <- function(output_dir, file_name, plot_configs, grid_la
# "delta_bg_tolerance", "delta_bg", "Gene", "L", "K", "r", "AUC", "NG", "DB"))), n = 5) # "delta_bg_tolerance", "delta_bg", "Gene", "L", "K", "r", "AUC", "NG", "DB"))), n = 5)
# Plots are testy about missing aesthetics, so handle them here # Plots are testy about missing aesthetics, so handle them here
aes_mapping <- if (is.null(config$color_var)) { aes_mapping <- aes(
if (is.null(config$y_var)) { x = !!sym(config$x_var),
aes(x = !!sym(config$x_var)) y = if (!is.null(config$y_var)) !!sym(config$y_var) else NULL,
} else { color = if (!is.null(config$color_var)) as.factor(!!sym(config$color_var)) else NULL
aes(x = !!sym(config$x_var), y = !!sym(config$y_var)) )
}
} else {
if (is.null(config$y_var)) {
aes(x = !!sym(config$x_var), color = as.factor(!!sym(config$color_var)))
} else {
aes(x = !!sym(config$x_var), y = !!sym(config$y_var), color = as.factor(!!sym(config$color_var)))
}
}
# Start building the plot
plot <- ggplot(df, aes_mapping) plot <- ggplot(df, aes_mapping)
# Use appropriate helper function based on plot type # Use appropriate helper function based on plot type
@@ -428,7 +421,7 @@ generate_and_save_plots <- function(output_dir, file_name, plot_configs, grid_la
# HTML saving logic # HTML saving logic
plotly_plots <- lapply(plots, function(plot) { plotly_plots <- lapply(plots, function(plot) {
config <- plot$labels$config config <- plot$config
if (!is.null(config$legend_position) && config$legend_position == "bottom") { if (!is.null(config$legend_position) && config$legend_position == "bottom") {
suppressWarnings(ggplotly(plot, tooltip = "text") %>% layout(legend = list(orientation = "h"))) suppressWarnings(ggplotly(plot, tooltip = "text") %>% layout(legend = list(orientation = "h")))
} else { } else {
@@ -441,26 +434,22 @@ generate_and_save_plots <- function(output_dir, file_name, plot_configs, grid_la
generate_scatter_plot <- function(plot, config, interactive = FALSE) { generate_scatter_plot <- function(plot, config, interactive = FALSE) {
# Determine the base aesthetics
aes_params <- aes(
x = !!sym(config$x_var),
y = !!sym(config$y_var),
color = as.factor(!!sym(config$color_var)))
# Add the interactive `text` aesthetic if `interactive` is TRUE # Add the interactive `text` aesthetic if `interactive` is TRUE
if (interactive) { 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) {
aes_params$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)
} else if (!is.null(config$gene_point) && config$gene_point) { } else if (!is.null(config$gene_point) && config$gene_point) {
aes_params$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")
} else {
plot + geom_point(shape = config$shape %||% 3, size = config$size %||% 0.2)
} }
} } else {
# For non-interactive plots, just add `geom_point`
# Add the base geom_point layer plot <- plot + geom_point(shape = config$shape %||% 3, size = config$size %||% 0.2,
plot <- plot + geom_point(
aes_params, shape = config$shape %||% 3,
size = config$size %||% 0.2,
position = if (!is.null(config$position) && config$position == "jitter") "jitter" else "identity") 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) {
@@ -788,7 +777,7 @@ main <- function() {
{ {
non_finite_rows <- filter(., if_any(c(L), ~ !is.finite(.))) non_finite_rows <- filter(., if_any(c(L), ~ !is.finite(.)))
if (nrow(non_finite_rows) > 0) { if (nrow(non_finite_rows) > 0) {
message("Removed the following non-finite rows:") message("Filtering out the following non-finite rows:")
print(non_finite_rows %>% select(any_of(print_vars)), n = 200) print(non_finite_rows %>% select(any_of(print_vars)), n = 200)
} }
filter(., if_all(c(L), is.finite)) filter(., if_all(c(L), is.finite))