Temporarily print plotly plots separately for debug
This commit is contained in:
@@ -393,9 +393,6 @@ calculate_interaction_scores <- function(df, max_conc, bg_stats,
|
|||||||
generate_and_save_plots <- function(out_dir, filename, plot_configs) {
|
generate_and_save_plots <- function(out_dir, filename, plot_configs) {
|
||||||
message("Generating ", filename, ".pdf and ", filename, ".html")
|
message("Generating ", filename, ".pdf and ", filename, ".html")
|
||||||
|
|
||||||
grid_nrow <- ifelse(is.null(plot_configs$grid_layout$nrow), 1, plot_configs$grid_layout$nrow)
|
|
||||||
grid_ncol <- ifelse(is.null(plot_configs$grid_layout$ncol), length(plot_configs$plots), plot_configs$grid_layout$ncol)
|
|
||||||
|
|
||||||
static_plots <- list()
|
static_plots <- list()
|
||||||
plotly_plots <- list()
|
plotly_plots <- list()
|
||||||
|
|
||||||
@@ -403,26 +400,35 @@ generate_and_save_plots <- function(out_dir, filename, plot_configs) {
|
|||||||
config <- plot_configs$plots[[i]]
|
config <- plot_configs$plots[[i]]
|
||||||
df <- config$df
|
df <- config$df
|
||||||
|
|
||||||
# Define aes_mapping and handle color_var defaulting
|
message("Processing plot ", i, ": ", config$title)
|
||||||
aes_mapping <- if (config$plot_type == "bar") {
|
message("Plot type: ", config$plot_type)
|
||||||
if (!is.null(config$color_var)) {
|
|
||||||
|
if (is.null(df)) {
|
||||||
|
message("Dataframe for plot ", i, " is NULL.")
|
||||||
|
next
|
||||||
|
}
|
||||||
|
|
||||||
|
# Define aes_mapping, ensuring y_var is only used when it's not NULL
|
||||||
|
aes_mapping <- switch(config$plot_type,
|
||||||
|
"bar" = if (!is.null(config$color_var)) {
|
||||||
aes(x = .data[[config$x_var]], fill = .data[[config$color_var]], color = .data[[config$color_var]])
|
aes(x = .data[[config$x_var]], fill = .data[[config$color_var]], color = .data[[config$color_var]])
|
||||||
} else {
|
} else {
|
||||||
aes(x = .data[[config$x_var]])
|
aes(x = .data[[config$x_var]])
|
||||||
}
|
},
|
||||||
} else if (config$plot_type == "density") {
|
"density" = if (!is.null(config$color_var)) {
|
||||||
if (!is.null(config$color_var)) {
|
|
||||||
aes(x = .data[[config$x_var]], color = .data[[config$color_var]])
|
aes(x = .data[[config$x_var]], color = .data[[config$color_var]])
|
||||||
} else {
|
} else {
|
||||||
aes(x = .data[[config$x_var]])
|
aes(x = .data[[config$x_var]])
|
||||||
}
|
},
|
||||||
} else {
|
# For other plot types, only include y_var if it's not NULL
|
||||||
if (!is.null(config$color_var)) {
|
if (!is.null(config$y_var) && !is.null(config$color_var)) {
|
||||||
aes(x = .data[[config$x_var]], y = .data[[config$y_var]], color = .data[[config$color_var]])
|
aes(x = .data[[config$x_var]], y = .data[[config$y_var]], color = .data[[config$color_var]])
|
||||||
} else {
|
} else if (!is.null(config$y_var)) {
|
||||||
aes(x = .data[[config$x_var]], y = .data[[config$y_var]])
|
aes(x = .data[[config$x_var]], y = .data[[config$y_var]])
|
||||||
|
} else {
|
||||||
|
aes(x = .data[[config$x_var]]) # no y_var needed for density and bar plots
|
||||||
}
|
}
|
||||||
}
|
)
|
||||||
|
|
||||||
# Apply theme_publication with legend_position
|
# Apply theme_publication with legend_position
|
||||||
plot <- ggplot(df, aes_mapping) + theme_publication(legend_position = config$legend_position)
|
plot <- ggplot(df, aes_mapping) + theme_publication(legend_position = config$legend_position)
|
||||||
@@ -432,8 +438,8 @@ generate_and_save_plots <- function(out_dir, filename, plot_configs) {
|
|||||||
"scatter" = generate_scatter_plot(plot, config),
|
"scatter" = generate_scatter_plot(plot, config),
|
||||||
"box" = generate_box_plot(plot, config),
|
"box" = generate_box_plot(plot, config),
|
||||||
"density" = plot + geom_density(),
|
"density" = plot + geom_density(),
|
||||||
"bar" = plot + geom_bar(),
|
"bar" = plot + geom_bar(stat = "count"), # count occurrences
|
||||||
plot # default case
|
plot # default (unused)
|
||||||
)
|
)
|
||||||
|
|
||||||
# Add titles and labels
|
# Add titles and labels
|
||||||
@@ -442,13 +448,17 @@ generate_and_save_plots <- function(out_dir, filename, plot_configs) {
|
|||||||
if (!is.null(config$y_label)) plot <- plot + ylab(config$y_label)
|
if (!is.null(config$y_label)) plot <- plot + ylab(config$y_label)
|
||||||
if (!is.null(config$coord_cartesian)) plot <- plot + coord_cartesian(ylim = config$coord_cartesian)
|
if (!is.null(config$coord_cartesian)) plot <- plot + coord_cartesian(ylim = config$coord_cartesian)
|
||||||
|
|
||||||
# Add interactive tooltips for plotly
|
# Convert ggplot to plotly, skipping subplot
|
||||||
tooltip_vars <- ifelse(!is.null(config$tooltip_vars), config$tooltip_vars, "none")
|
if (!is.null(config$tooltip_vars)) {
|
||||||
plotly_plot <- suppressWarnings(plotly::ggplotly(plot, tooltip = tooltip_vars))
|
plotly_plot <- suppressWarnings(plotly::ggplotly(plot, tooltip = config$tooltip_vars))
|
||||||
|
} else {
|
||||||
|
plotly_plot <- suppressWarnings(plotly::ggplotly(plot))
|
||||||
|
}
|
||||||
|
|
||||||
# Adjust legend position in plotly
|
# Adjust legend position in plotly
|
||||||
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"))
|
||||||
|
plotly_plot <- plotly_plot + theme(legend.direction = NULL)
|
||||||
}
|
}
|
||||||
|
|
||||||
# Add static and interactive plots to lists
|
# Add static and interactive plots to lists
|
||||||
@@ -458,17 +468,27 @@ generate_and_save_plots <- function(out_dir, filename, plot_configs) {
|
|||||||
|
|
||||||
# Save static PDF plots
|
# Save static PDF plots
|
||||||
pdf(file.path(out_dir, paste0(filename, ".pdf")), width = 16, height = 9)
|
pdf(file.path(out_dir, paste0(filename, ".pdf")), width = 16, height = 9)
|
||||||
|
|
||||||
|
if (is.null(plot_configs$grid_layout)) {
|
||||||
|
# Print each plot on a new page if grid_layout is not set
|
||||||
|
for (plot in static_plots) {
|
||||||
|
print(plot)
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
# Use grid.arrange if grid_layout is set
|
||||||
|
grid_nrow <- ifelse(is.null(plot_configs$grid_layout$nrow), length(plot_configs$plots), plot_configs$grid_layout$nrow)
|
||||||
|
grid_ncol <- ifelse(is.null(plot_configs$grid_layout$ncol), 1, plot_configs$grid_layout$ncol)
|
||||||
grid.arrange(grobs = static_plots, ncol = grid_ncol, nrow = grid_nrow)
|
grid.arrange(grobs = static_plots, ncol = grid_ncol, nrow = grid_nrow)
|
||||||
|
}
|
||||||
|
|
||||||
dev.off()
|
dev.off()
|
||||||
|
|
||||||
# Save combined interactive HTML plots
|
# Save individual interactive HTML plots without subplot
|
||||||
if (length(plotly_plots) == 1) {
|
for (i in seq_along(plotly_plots)) {
|
||||||
combined_plot <- plotly_plots[[1]]
|
html_file <- file.path(out_dir, paste0(filename, "_plot_", i, ".html"))
|
||||||
} else {
|
message("Saving HTML plot ", i, ": ", html_file)
|
||||||
combined_plot <- plotly::subplot(plotly_plots, nrows = grid_nrow, ncols = grid_ncol, margin = 0.05)
|
htmlwidgets::saveWidget(plotly_plots[[i]], file = html_file, selfcontained = TRUE)
|
||||||
}
|
}
|
||||||
html_file <- file.path(out_dir, paste0(filename, ".html"))
|
|
||||||
saveWidget(combined_plot, file = html_file, selfcontained = TRUE)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
generate_scatter_plot <- function(plot, config) {
|
generate_scatter_plot <- function(plot, config) {
|
||||||
|
|||||||
Reference in New Issue
Block a user