Switch from tooltip to text aes for more control

Tento commit je obsažen v:
2024-09-18 02:29:11 -04:00
rodič d42dd71b97
revize 7bfe5c337e

Zobrazit soubor

@@ -370,16 +370,33 @@ generate_and_save_plots <- function(out_dir, file_name, plot_configs, grid_layou
config <- plot_configs[[i]]
df <- config$df
# Initialize tooltip_text
tooltip_text <- NULL
# Define aes_mapping based on plot type
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) {
tooltip_text <- paste("OrfRep:", df$OrfRep, "<br>Gene:", df$Gene, "<br>delta_bg:", df$delta_bg)
# 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)
}
} else if (!is.null(config$gene_point) && config$gene_point) {
tooltip_text <- paste("OrfRep:", df$OrfRep, "<br>Gene:", df$Gene)
} else {
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(x = .data[[config$x_var]], y = .data[[config$y_var]], text = tooltip_text)
} else {
@@ -387,13 +404,34 @@ generate_and_save_plots <- function(out_dir, file_name, plot_configs, grid_layou
color = as.factor(.data[[config$color_var]]), text = tooltip_text)
}
} else {
# Define aes_mapping for other plot types without 'text' aesthetic
aes_mapping <- if (is.null(config$color_var)) {
aes(x = .data[[config$x_var]], y = .data[[config$y_var]])
} else {
aes(x = .data[[config$x_var]], y = .data[[config$y_var]],
color = as.factor(.data[[config$color_var]]))
# 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)) {
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]])
}
} else {
if (config$plot_type %in% c("density", "bar")) {
aes(x = .data[[config$x_var]],
color = as.factor(.data[[config$color_var]]))
} else {
aes(x = .data[[config$x_var]], y = .data[[config$y_var]],
color = as.factor(.data[[config$color_var]]))
}
}
# No tooltip for non-scatter plots
tooltip_text <- NULL
}
# Start building the plot with 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),
"density" = plot_base + geom_density(),
"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
@@ -429,7 +470,6 @@ generate_and_save_plots <- function(out_dir, file_name, plot_configs, grid_layou
plotly_plot <- ggplotly(plot, tooltip = "text")
} else {
# 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")
}