Cleanup plot inconsistincies

This commit is contained in:
2024-09-18 14:26:41 -04:00
parent b35551c3e2
commit 728449efb4

View File

@@ -466,20 +466,21 @@ generate_and_save_plots <- function(out_dir, file_name, plot_configs, grid_layou
} }
generate_scatter_plot <- function(plot, config) { generate_scatter_plot <- function(plot, config) {
# Determine Shape, Size, and Position for geom_point
shape <- if (!is.null(config$shape)) config$shape else 3 shape <- if (!is.null(config$shape)) config$shape else 3
size <- if (!is.null(config$size)) config$size else 0.1 size <- if (!is.null(config$size)) config$size else 0.1
position <- if (!is.null(config$position) && config$position == "jitter") "jitter" else "identity" position <-
if (!is.null(config$position) && config$position == "jitter") {
# Add geom_point with determined parameters position_jitter(width = 0.1, height = 0)
} else {
"identity"
}
plot <- plot + geom_point( plot <- plot + geom_point(
shape = shape, shape = shape,
size = size, size = size,
position = position position = position
) )
if (!is.null(config$cyan_points)) { if (!is.null(config$cyan_points) && config$cyan_points) {
plot <- plot + geom_point( plot <- plot + geom_point(
aes(x = .data[[config$x_var]], y = .data[[config$y_var]]), aes(x = .data[[config$x_var]], y = .data[[config$y_var]]),
color = "cyan", color = "cyan",
@@ -514,19 +515,19 @@ generate_scatter_plot <- function(plot, config) {
"rect", "rect",
xmin = -Inf, xmax = Inf, xmin = -Inf, xmax = Inf,
ymin = config$sd_band, ymax = Inf, ymin = config$sd_band, ymax = Inf,
fill = "#542788", fill = ifelse(!is.null(config$fill_positive), config$fill_positive, "#542788"),
alpha = 0.3 alpha = ifelse(!is.null(config$alpha_positive), config$alpha_positive, 0.3)
) + ) +
annotate( annotate(
"rect", "rect",
xmin = -Inf, xmax = Inf, xmin = -Inf, xmax = Inf,
ymin = -config$sd_band, ymax = -Inf, ymin = -config$sd_band, ymax = -Inf,
fill = "orange", fill = ifelse(!is.null(config$fill_negative), config$fill_negative, "orange"),
alpha = 0.3 alpha = ifelse(!is.null(config$alpha_negative), config$alpha_negative, 0.3)
) + ) +
geom_hline( geom_hline(
yintercept = c(-config$sd_band, config$sd_band), yintercept = c(-config$sd_band, config$sd_band),
color = "gray" color = ifelse(!is.null(config$hl_color), config$hl_color, "gray")
) )
} }
@@ -590,7 +591,10 @@ generate_scatter_plot <- function(plot, config) {
x = annotation$x, x = annotation$x,
y = annotation$y, y = annotation$y,
label = annotation$label, label = annotation$label,
na.rm = TRUE 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), 4, annotation$size),
color = ifelse(is.null(annotation$color), "black", annotation$color)
) )
} }
} }
@@ -768,7 +772,7 @@ generate_rank_plot_configs <- function(df_filtered, variables, is_lm = FALSE) {
# SD-based plots for L and K # SD-based plots for L and K
for (variable in c("L", "K")) { for (variable in c("L", "K")) {
if (is_lm) { if (is_lm) {
rank_var <- paste0("Rank_lm_", variable) rank_var <- paste0("Rank_lm_", variable)
zscore_var <- paste0("Z_lm_", variable) zscore_var <- paste0("Z_lm_", variable)
@@ -778,9 +782,9 @@ generate_rank_plot_configs <- function(df_filtered, variables, is_lm = FALSE) {
zscore_var <- paste0("Avg_Zscore_", variable) zscore_var <- paste0("Avg_Zscore_", variable)
y_label <- paste("Avg Z score", variable) y_label <- paste("Avg Z score", variable)
} }
for (sd_band in sd_bands) { for (sd_band in sd_bands) {
num_enhancers <- sum(df_filtered[[zscore_var]] >= sd_band, na.rm = TRUE) num_enhancers <- sum(df_filtered[[zscore_var]] >= sd_band, na.rm = TRUE)
num_suppressors <- sum(df_filtered[[zscore_var]] <= -sd_band, na.rm = TRUE) num_suppressors <- sum(df_filtered[[zscore_var]] <= -sd_band, na.rm = TRUE)
@@ -792,16 +796,24 @@ generate_rank_plot_configs <- function(df_filtered, variables, is_lm = FALSE) {
plot_type = "scatter", plot_type = "scatter",
title = paste(y_label, "vs. Rank for", variable, "above", sd_band, "SD"), title = paste(y_label, "vs. Rank for", variable, "above", sd_band, "SD"),
sd_band = sd_band, sd_band = sd_band,
fill_positive = "#542788",
fill_negative = "orange",
alpha_positive = 0.3,
alpha_negative = 0.3,
annotations = list( annotations = list(
list( list(
x = median(df_filtered[[rank_var]], na.rm = TRUE), x = median(df_filtered[[rank_var]], na.rm = TRUE),
y = 10, y = 10,
label = paste("Deletion Enhancers =", num_enhancers) label = paste("Deletion Enhancers =", num_enhancers),
hjust = 0.5,
vjust = 1
), ),
list( list(
x = median(df_filtered[[rank_var]], na.rm = TRUE), x = median(df_filtered[[rank_var]], na.rm = TRUE),
y = -10, y = -10,
label = paste("Deletion Suppressors =", num_suppressors) label = paste("Deletion Suppressors =", num_suppressors),
hjust = 0.5,
vjust = 0
) )
), ),
shape = 3, shape = 3,
@@ -819,6 +831,10 @@ generate_rank_plot_configs <- function(df_filtered, variables, is_lm = FALSE) {
plot_type = "scatter", plot_type = "scatter",
title = paste(y_label, "vs. Rank for", variable, "above", sd_band, "SD No Annotations"), title = paste(y_label, "vs. Rank for", variable, "above", sd_band, "SD No Annotations"),
sd_band = sd_band, sd_band = sd_band,
fill_positive = "#542788",
fill_negative = "orange",
alpha_positive = 0.3,
alpha_negative = 0.3,
annotations = NULL, annotations = NULL,
shape = 3, shape = 3,
size = 0.1, size = 0.1,
@@ -868,9 +884,18 @@ generate_rank_plot_configs <- function(df_filtered, variables, is_lm = FALSE) {
title = title, title = title,
annotations = list( annotations = list(
list( list(
x = 0, x = median(df_filtered[[rank_var]], na.rm = TRUE),
y = 0, y = 10,
label = paste("R-squared =", round(lm_summary$r.squared, 2)) label = paste("Deletion Enhancers =", num_enhancers),
hjust = 0.5,
vjust = 1
),
list(
x = median(df_filtered[[rank_var]], na.rm = TRUE),
y = -10,
label = paste("Deletion Suppressors =", num_suppressors),
hjust = 0.5,
vjust = 0
) )
), ),
shape = 3, shape = 3,
@@ -916,7 +941,15 @@ generate_correlation_plot_configs <- function(df) {
x_label = paste("z-score", gsub("Z_lm_", "", rel$x)), x_label = paste("z-score", gsub("Z_lm_", "", rel$x)),
y_label = paste("z-score", gsub("Z_lm_", "", rel$y)), y_label = paste("z-score", gsub("Z_lm_", "", rel$y)),
annotations = list( annotations = list(
list(x = 0, y = 0, label = paste("R-squared =", round(lm_summary$r.squared, 3))) list(
x = Inf,
y = Inf,
label = paste("R-squared =", round(lm_summary$r.squared, 3)),
hjust = 1.1,
vjust = 2,
size = 4,
color = "black"
)
), ),
add_smooth = TRUE, # Add regression line add_smooth = TRUE, # Add regression line
lm_line = list(intercept = coef(lm_model)[1], slope = coef(lm_model)[2]), lm_line = list(intercept = coef(lm_model)[1], slope = coef(lm_model)[2]),
@@ -1380,15 +1413,14 @@ main <- function() {
) )
# Re-rank # Re-rank
zscores_interactions_filtered <- process_data( zscores_interactions_filtered_ranked <- process_data(
df = zscores_interactions_filtered, df = zscores_interactions_filtered,
variables = interaction_vars, variables = interaction_vars,
filter_na = TRUE, # TODO what I'm currently having issues with
rank = TRUE rank = TRUE
) )
rank_plot_filtered_configs <- generate_rank_plot_configs( rank_plot_filtered_configs <- generate_rank_plot_configs(
df = zscores_interactions_filtered, df = zscores_interactions_filtered_ranked,
variables = interaction_vars, variables = interaction_vars,
is_lm = FALSE is_lm = FALSE
) )
@@ -1402,7 +1434,7 @@ main <- function() {
message("Generating filtered ranked linear model plots") message("Generating filtered ranked linear model plots")
rank_plot_lm_filtered_configs <- generate_rank_plot_configs( rank_plot_lm_filtered_configs <- generate_rank_plot_configs(
df = zscores_interactions_filtered, df = zscores_interactions_filtered_ranked,
variables = interaction_vars, variables = interaction_vars,
is_lm = TRUE is_lm = TRUE
) )