Move annotations to global scope

This commit is contained in:
2024-10-04 10:22:51 -04:00
parent 4a6890290e
commit fdfe878774
2 changed files with 26 additions and 33 deletions

2
.gitignore vendored
View File

@@ -2,4 +2,6 @@
centos-upgrade-plan.txt
qhtcp-workflow/out/
qhtcp-workflow/scans/
qhctp-workflow/oom
last.dump.rda

View File

@@ -554,6 +554,23 @@ 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$coord_cartesian)) plot <- plot + coord_cartesian(ylim = config$coord_cartesian)
# Add annotations if specified
if (!is.null(config$annotations)) {
for (annotation in config$annotations) {
plot <- plot +
annotate(
"text",
x = ifelse(is.null(annotation$x), 0, annotation$x),
y = ifelse(is.null(annotation$y), Inf, annotation$y),
label = annotation$label,
hjust = ifelse(is.null(annotation$hjust), 0.5, annotation$hjust),
vjust = ifelse(is.null(annotation$vjust), 1, annotation$vjust),
size = ifelse(is.null(annotation$size), 3, annotation$size),
color = ifelse(is.null(annotation$color), "black", annotation$color)
)
}
}
# Add error bars if specified
if (!is.null(config$error_bar) && config$error_bar) {
# Check if a fixed color is provided or if it should come from a data column
@@ -567,19 +584,15 @@ generate_and_save_plots <- function(out_dir, filename, plot_configs) {
ymin = config$error_bar_params$ymin,
ymax = config$error_bar_params$ymax
)
# Apply fixed color if specified
if (!is.null(error_bar_color)) {
plot <- plot + scale_color_manual(values = error_bar_color)
}
} else {
# If config$color_var exists, map the color aesthetic
# Map color_var to data if available
if (!is.null(config$color_var)) {
plot <- plot + geom_errorbar(
aes(
x = .data[[config$x_var]],
ymin = .data[[config$error_bar_params$ymin]],
ymax = .data[[config$error_bar_params$ymax]],
color = .data[[config$color_var]] # Map color_var to data
color = .data[[config$color_var]]
)
)
} else {
@@ -593,19 +606,18 @@ generate_and_save_plots <- function(out_dir, filename, plot_configs) {
}
}
} else {
# Ensure the mean and sd columns exist
# Use mean and SD columns from df
y_mean_col <- paste0("mean_", config$y_var)
y_sd_col <- paste0("sd_", config$y_var)
if (y_mean_col %in% colnames(df) && y_sd_col %in% colnames(df)) {
# If config$color_var exists, map the color aesthetic
if (!is.null(config$color_var)) {
plot <- plot + geom_errorbar(
aes(
x = .data[[config$x_var]],
ymin = .data[[y_mean_col]] - .data[[y_sd_col]],
ymax = .data[[y_mean_col]] + .data[[y_sd_col]],
color = .data[[config$color_var]] # Color based on aes()
color = .data[[config$color_var]]
)
)
} else {
@@ -617,10 +629,6 @@ generate_and_save_plots <- function(out_dir, filename, plot_configs) {
)
)
}
# Apply fixed color if specified
if (!is.null(error_bar_color)) {
plot <- plot + scale_color_manual(values = error_bar_color)
}
}
}
}
@@ -773,23 +781,6 @@ generate_scatter_plot <- function(plot, config) {
plot <- plot + scale_y_continuous(limits = config$ylim_vals)
}
# Add annotations if specified
if (!is.null(config$annotations)) {
for (annotation in config$annotations) {
plot <- plot +
annotate(
"text",
x = ifelse(is.null(annotation$x), 0, annotation$x),
y = ifelse(is.null(annotation$y), 0, annotation$y),
label = annotation$label,
hjust = ifelse(is.null(annotation$hjust), 0.5, annotation$hjust),
vjust = ifelse(is.null(annotation$vjust), 0.5, annotation$vjust),
size = ifelse(is.null(annotation$size), 3, annotation$size),
color = ifelse(is.null(annotation$color), "black", annotation$color)
)
}
}
return(plot)
}
@@ -901,9 +892,9 @@ generate_interaction_plot_configs <- function(df, type) {
plot_config$position <- "jitter"
annotations <- list(
list(x = 0.25, y = y_limits[1] + 0.1 * y_span, label = " NG ="), # Slightly above y-min
list(x = 0.25, y = y_limits[1] + 0.05 * y_span, label = " DB ="),
list(x = 0.25, y = y_limits[1], label = " SM =")
list(x = 0.25, y = y_limits[1] + 0.1 * y_span, label = " NG ="), # Slightly above y-min
list(x = 0.25, y = y_limits[1] + 0.05 * y_span, label = " DB ="),
list(x = 0.25, y = y_limits[1], label = " SM =")
)
# Loop over unique x values and add NG, DB, SM values at calculated y positions