Rework plotting code, add auto-grid layout
This commit is contained in:
@@ -370,68 +370,18 @@ 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
|
aes_mapping <- if (is.null(config$color_var)) {
|
||||||
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)) {
|
if (is.null(config$y_var)) {
|
||||||
warning(paste("Plot", i, "of type 'scatter' is missing 'y_var'. Skipping this plot."))
|
aes(x = .data[[config$x_var]])
|
||||||
next
|
|
||||||
}
|
|
||||||
|
|
||||||
# Construct tooltip_text based on configuration flags
|
|
||||||
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)
|
|
||||||
}
|
|
||||||
} else if (!is.null(config$gene_point) && config$gene_point) {
|
|
||||||
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]])
|
aes(x = .data[[config$x_var]], y = .data[[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 {
|
|
||||||
aes(x = .data[[config$x_var]], y = .data[[config$y_var]],
|
|
||||||
color = as.factor(.data[[config$color_var]]), text = tooltip_text)
|
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
# Handle other plot types
|
if (is.null(config$y_var)) {
|
||||||
# For 'box' plots, y_var is required
|
aes(x = .data[[config$x_var]], color = as.factor(.data[[config$color_var]]))
|
||||||
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 {
|
} else {
|
||||||
if (config$plot_type %in% c("density", "bar")) {
|
aes(x = .data[[config$x_var]], y = .data[[config$y_var]], color = as.factor(.data[[config$color_var]]))
|
||||||
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
|
# Start building the plot with aes_mapping
|
||||||
@@ -443,10 +393,7 @@ 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
|
||||||
@@ -465,11 +412,27 @@ generate_and_save_plots <- function(out_dir, file_name, plot_configs, grid_layou
|
|||||||
plot <- plot + ylab(config$y_label)
|
plot <- plot + ylab(config$y_label)
|
||||||
}
|
}
|
||||||
|
|
||||||
# Convert to plotly object
|
# Apply scale_color_discrete(guide = FALSE) when color_var is NULL
|
||||||
|
if (is.null(config$color_var)) {
|
||||||
|
plot <- plot + scale_color_discrete(guide = FALSE)
|
||||||
|
}
|
||||||
|
|
||||||
|
# Add interactive tooltips for plotly
|
||||||
|
tooltip_vars <- c()
|
||||||
if (config$plot_type == "scatter") {
|
if (config$plot_type == "scatter") {
|
||||||
plotly_plot <- ggplotly(plot, tooltip = "text")
|
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 if (!is.null(config$y_var) && !is.null(config$x_var)) {
|
||||||
|
tooltip_vars <- c(config$x_var, config$y_var)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
# Convert to plotly object
|
||||||
|
if (length(tooltip_vars) > 0) {
|
||||||
|
plotly_plot <- ggplotly(plot, tooltip = tooltip_vars)
|
||||||
} else {
|
} else {
|
||||||
# For non-scatter plots, decide if tooltips are needed
|
|
||||||
plotly_plot <- ggplotly(plot, tooltip = "none")
|
plotly_plot <- ggplotly(plot, tooltip = "none")
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -489,9 +452,16 @@ generate_and_save_plots <- function(out_dir, file_name, plot_configs, grid_layou
|
|||||||
dev.off()
|
dev.off()
|
||||||
|
|
||||||
# Combine and save interactive HTML plots
|
# Combine and save interactive HTML plots
|
||||||
combined_plot <- subplot(plotly_plots,
|
combined_plot <- subplot(
|
||||||
nrows = ifelse(is.null(grid_layout$nrow), length(plotly_plots), grid_layout$nrow),
|
plotly_plots,
|
||||||
margin = 0.05)
|
nrows = if (!is.null(grid_layout) && !is.null(grid_layout$nrow)) {
|
||||||
|
grid_layout$nrow
|
||||||
|
} else {
|
||||||
|
# Calculate nrow based on the length of plotly_plots (default 1 row if only one plot)
|
||||||
|
ceiling(length(plotly_plots) / ifelse(!is.null(grid_layout) && !is.null(grid_layout$ncol), grid_layout$ncol, 1))
|
||||||
|
},
|
||||||
|
margin = 0.05
|
||||||
|
)
|
||||||
saveWidget(combined_plot, file = file.path(out_dir, paste0(file_name, ".html")), selfcontained = TRUE)
|
saveWidget(combined_plot, file = file.path(out_dir, paste0(file_name, ".html")), selfcontained = TRUE)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user