Switch from tooltip to text aes for more control

This commit is contained in:
2024-09-18 02:29:11 -04:00
parent d42dd71b97
commit 7bfe5c337e

View File

@@ -370,16 +370,33 @@ generate_and_save_plots <- function(out_dir, file_name, plot_configs, grid_layou
config <- plot_configs[[i]] config <- plot_configs[[i]]
df <- config$df df <- config$df
# Initialize tooltip_text
tooltip_text <- NULL
# Define aes_mapping based on plot type # Define aes_mapping based on plot type
if (config$plot_type == "scatter") { if (config$plot_type == "scatter") {
# Check if y_var is provided
if (is.null(config$y_var)) {
warning(paste("Plot", i, "of type 'scatter' is missing 'y_var'. Skipping this plot."))
next
}
# Construct tooltip_text based on configuration flags
if (!is.null(config$delta_bg_point) && config$delta_bg_point) { if (!is.null(config$delta_bg_point) && config$delta_bg_point) {
# Ensure 'delta_bg' exists
if (!"delta_bg" %in% names(df)) {
warning(paste("Plot", i, "requires 'delta_bg' column for tooltip, but it's missing."))
tooltip_text <- paste("OrfRep:", df$OrfRep, "<br>Gene:", df$Gene)
} else {
tooltip_text <- paste("OrfRep:", df$OrfRep, "<br>Gene:", df$Gene, "<br>delta_bg:", df$delta_bg) tooltip_text <- paste("OrfRep:", df$OrfRep, "<br>Gene:", df$Gene, "<br>delta_bg:", df$delta_bg)
}
} else if (!is.null(config$gene_point) && config$gene_point) { } else if (!is.null(config$gene_point) && config$gene_point) {
tooltip_text <- paste("OrfRep:", df$OrfRep, "<br>Gene:", df$Gene) tooltip_text <- paste("OrfRep:", df$OrfRep, "<br>Gene:", df$Gene)
} else { } else {
tooltip_text <- paste("x:", df[[config$x_var]], "<br>y:", df[[config$y_var]]) tooltip_text <- paste("x:", df[[config$x_var]], "<br>y:", df[[config$y_var]])
} }
# Define aesthetic mapping with or without color_var
aes_mapping <- if (is.null(config$color_var)) { aes_mapping <- if (is.null(config$color_var)) {
aes(x = .data[[config$x_var]], y = .data[[config$y_var]], text = tooltip_text) aes(x = .data[[config$x_var]], y = .data[[config$y_var]], text = tooltip_text)
} else { } else {
@@ -387,15 +404,36 @@ generate_and_save_plots <- function(out_dir, file_name, plot_configs, grid_layou
color = as.factor(.data[[config$color_var]]), text = tooltip_text) color = as.factor(.data[[config$color_var]]), text = tooltip_text)
} }
} else { } else {
# Define aes_mapping for other plot types without 'text' aesthetic # Handle other plot types
# For 'box' plots, y_var is required
if (config$plot_type == "box") {
if (is.null(config$y_var)) {
warning(paste("Plot", i, "of type 'box' is missing 'y_var'. Skipping this plot."))
next
}
}
# Define aes_mapping for non-scatter plots
aes_mapping <- if (is.null(config$color_var)) { aes_mapping <- if (is.null(config$color_var)) {
if (config$plot_type %in% c("density", "bar")) {
aes(x = .data[[config$x_var]])
} else {
aes(x = .data[[config$x_var]], y = .data[[config$y_var]]) aes(x = .data[[config$x_var]], y = .data[[config$y_var]])
}
} else {
if (config$plot_type %in% c("density", "bar")) {
aes(x = .data[[config$x_var]],
color = as.factor(.data[[config$color_var]]))
} else { } else {
aes(x = .data[[config$x_var]], y = .data[[config$y_var]], aes(x = .data[[config$x_var]], y = .data[[config$y_var]],
color = as.factor(.data[[config$color_var]])) color = as.factor(.data[[config$color_var]]))
} }
} }
# No tooltip for non-scatter plots
tooltip_text <- NULL
}
# Start building the plot with aes_mapping # Start building the plot with aes_mapping
plot_base <- ggplot(df, aes_mapping) plot_base <- ggplot(df, aes_mapping)
@@ -405,7 +443,10 @@ generate_and_save_plots <- function(out_dir, file_name, plot_configs, grid_layou
"box" = generate_box_plot(plot_base, config), "box" = generate_box_plot(plot_base, config),
"density" = plot_base + geom_density(), "density" = plot_base + geom_density(),
"bar" = plot_base + geom_bar(), "bar" = plot_base + geom_bar(),
plot_base # default case if no type matches {
warning(paste("Unknown plot_type:", config$plot_type, "- using base plot"))
plot_base
}
) )
# Apply additional settings if provided # Apply additional settings if provided
@@ -429,7 +470,6 @@ generate_and_save_plots <- function(out_dir, file_name, plot_configs, grid_layou
plotly_plot <- ggplotly(plot, tooltip = "text") plotly_plot <- ggplotly(plot, tooltip = "text")
} else { } else {
# For non-scatter plots, decide if tooltips are needed # For non-scatter plots, decide if tooltips are needed
# If not, you can set tooltip to NULL or specify relevant aesthetics
plotly_plot <- ggplotly(plot, tooltip = "none") plotly_plot <- ggplotly(plot, tooltip = "none")
} }