Use tooltips rather than text aes for interactive plotly

This commit is contained in:
2024-09-15 12:28:52 -04:00
parent 4f0c36fd66
commit ee986fbd60

View File

@@ -393,11 +393,9 @@ generate_and_save_plots <- function(output_dir, file_name, plot_configs, grid_la
# 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)
# Function to generate the plot
generate_plot <- function(interactive) {
# Use appropriate helper function based on plot type # Use appropriate helper function based on plot type
plot <- switch(config$plot_type, plot <- switch(config$plot_type,
"scatter" = generate_scatter_plot(plot_base, config, is_interactive = interactive), "scatter" = generate_scatter_plot(plot_base, config),
"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(),
@@ -420,38 +418,44 @@ generate_and_save_plots <- function(output_dir, file_name, plot_configs, grid_la
plot <- plot + ylab(config$y_label) plot <- plot + ylab(config$y_label)
} }
# Return the plot # Add interactive tooltips for plotly plots
plot tooltip_vars <- c("x", "y") # default tooltip variables
if (!is.null(config$tooltip_vars)) {
tooltip_vars <- config$tooltip_vars
} else {
# Include default variables based on config
if (!is.null(config$delta_bg_point) && config$delta_bg_point) {
tooltip_vars <- c(tooltip_vars, "OrfRep", "Gene", "delta_bg")
} else if (!is.null(config$gene_point) && config$gene_point) {
tooltip_vars <- c(tooltip_vars, "OrfRep", "Gene")
} else {
# Include x and y variables by default
tooltip_vars <- c("x", "y")
}
} }
# Generate the static plot
static_plot <- generate_plot(interactive = FALSE)
# Generate the interactive plot
interactive_plot <- generate_plot(interactive = TRUE)
# Convert to plotly object # Convert to plotly object
plotly_plot <- ggplotly(interactive_plot, tooltip = "text") plotly_plot <- ggplotly(plot, tooltip = tooltip_vars)
if (!is.null(config$legend_position) && config$legend_position == "bottom") { if (!is.null(config$legend_position) && config$legend_position == "bottom") {
plotly_plot <- plotly_plot %>% layout(legend = list(orientation = "h")) plotly_plot <- plotly_plot %>% layout(legend = list(orientation = "h"))
} }
# Add plots to lists # Add plots to lists
static_plots[[i]] <- static_plot static_plots[[i]] <- plot
plotly_plots[[i]] <- plotly_plot plotly_plots[[i]] <- plotly_plot
} }
# PDF saving logic # Save static PDF plots
pdf(file.path(output_dir, paste0(file_name, ".pdf")), width = 14, height = 9) pdf(file.path(output_dir, paste0(file_name, ".pdf")), width = 14, height = 9)
lapply(static_plots, print) lapply(static_plots, print)
dev.off() dev.off()
# Combine and save interactive plots # Combine and save interactive HTML plots
combined_plot <- subplot(plotly_plots, nrows = grid_layout$nrow %||% length(plotly_plots), margin = 0.05) combined_plot <- subplot(plotly_plots, nrows = grid_layout$nrow %||% length(plotly_plots), margin = 0.05)
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, is_interactive = FALSE) { generate_scatter_plot <- function(plot, config) {
# Check for missing or out-of-range data # Check for missing or out-of-range data
missing_data <- config$df %>% missing_data <- config$df %>%
filter( filter(
@@ -479,33 +483,20 @@ generate_scatter_plot <- function(plot, config, is_interactive = FALSE) {
) )
} }
# Add the interactive text aesthetic if `is_interactive` is TRUE
if (is_interactive) {
if (!is.null(config$delta_bg_point) && config$delta_bg_point) { if (!is.null(config$delta_bg_point) && config$delta_bg_point) {
plot <- plot + geom_point( plot <- plot + geom_point(
aes(text = paste("ORF:", OrfRep, "Gene:", Gene, "delta_bg:", delta_bg)),
shape = config$shape %||% 3, shape = config$shape %||% 3,
size = config$size %||% 0.2 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 <- plot + geom_point( plot <- plot + geom_point(
aes(text = paste("ORF:", OrfRep, "Gene:", Gene)),
shape = config$shape %||% 3, shape = config$shape %||% 3,
size = config$size %||% 0.2, size = config$size %||% 0.2,
position = "jitter" position = "jitter"
) )
} else { } else {
plot <- plot + geom_point( plot <- plot + geom_point(
aes(text = paste("ORF:", OrfRep, "Gene:", Gene)),
shape = config$shape %||% 3, shape = config$shape %||% 3,
size = config$size %||% 0.2
)
}
} else {
# For non-interactive plots, just add `geom_point` without `text` aesthetic
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" position = if (!is.null(config$position) && config$position == "jitter") "jitter" else "identity"
) )
} }