Add configurable plot filtering
This commit is contained in:
@@ -545,6 +545,13 @@ generate_and_save_plots <- function(out_dir, filename, plot_configs, page_width
|
|||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
# Filter NAs if specified
|
||||||
|
if (!is.null(config$na_rm) && config$na_rm) {
|
||||||
|
df <- df %>%
|
||||||
|
filter(!is.na(.data[[config$x_var]])) %>%
|
||||||
|
filter(!is.na(.data[[config$y_var]]))
|
||||||
|
}
|
||||||
|
|
||||||
# Set up aes mapping based on plot type
|
# Set up aes mapping based on plot type
|
||||||
aes_mapping <- if (config$plot_type == "bar") {
|
aes_mapping <- if (config$plot_type == "bar") {
|
||||||
if (!is.null(config$color_var)) {
|
if (!is.null(config$color_var)) {
|
||||||
@@ -1092,7 +1099,7 @@ generate_interaction_plot_configs <- function(df_summary, df_interactions, type)
|
|||||||
))
|
))
|
||||||
}
|
}
|
||||||
|
|
||||||
generate_rank_plot_configs <- function(df, is_lm = FALSE, adjust = FALSE, overlap_color = FALSE) {
|
generate_rank_plot_configs <- function(df, is_lm = FALSE, adjust = FALSE, na_rm = FALSE, overlap_color = FALSE) {
|
||||||
sd_bands <- c(1, 2, 3)
|
sd_bands <- c(1, 2, 3)
|
||||||
plot_configs <- list()
|
plot_configs <- list()
|
||||||
|
|
||||||
@@ -1109,7 +1116,7 @@ generate_rank_plot_configs <- function(df, is_lm = FALSE, adjust = FALSE, overla
|
|||||||
}
|
}
|
||||||
|
|
||||||
# Helper function to create a rank plot configuration
|
# Helper function to create a rank plot configuration
|
||||||
create_plot_config <- function(variable, rank_var, zscore_var, y_label, sd_band, with_annotations = TRUE) {
|
create_plot_config <- function(variable, rank_var, zscore_var, y_label, sd_band, na_rm, with_annotations = TRUE) {
|
||||||
num_enhancers <- sum(df[[zscore_var]] >= sd_band, na.rm = TRUE)
|
num_enhancers <- sum(df[[zscore_var]] >= sd_band, na.rm = TRUE)
|
||||||
num_suppressors <- sum(df[[zscore_var]] <= -sd_band, na.rm = TRUE)
|
num_suppressors <- sum(df[[zscore_var]] <= -sd_band, na.rm = TRUE)
|
||||||
|
|
||||||
@@ -1119,6 +1126,7 @@ generate_rank_plot_configs <- function(df, is_lm = FALSE, adjust = FALSE, overla
|
|||||||
x_var = rank_var,
|
x_var = rank_var,
|
||||||
y_var = zscore_var,
|
y_var = zscore_var,
|
||||||
x_label = "Rank",
|
x_label = "Rank",
|
||||||
|
y_label = y_label,
|
||||||
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,
|
||||||
@@ -1128,8 +1136,7 @@ generate_rank_plot_configs <- function(df, is_lm = FALSE, adjust = FALSE, overla
|
|||||||
alpha_negative = 0.3,
|
alpha_negative = 0.3,
|
||||||
shape = 3,
|
shape = 3,
|
||||||
size = 0.1,
|
size = 0.1,
|
||||||
y_label = y_label,
|
na_rm = na_rm,
|
||||||
x_label = "Rank",
|
|
||||||
legend_position = "none"
|
legend_position = "none"
|
||||||
)
|
)
|
||||||
|
|
||||||
@@ -1161,10 +1168,12 @@ generate_rank_plot_configs <- function(df, is_lm = FALSE, adjust = FALSE, overla
|
|||||||
# Loop through SD bands
|
# Loop through SD bands
|
||||||
for (sd_band in sd_bands) {
|
for (sd_band in sd_bands) {
|
||||||
# Create plot with annotations
|
# Create plot with annotations
|
||||||
plot_configs[[length(plot_configs) + 1]] <- create_plot_config(variable, rank_var, zscore_var, y_label, sd_band, with_annotations = TRUE)
|
plot_configs[[length(plot_configs) + 1]] <-
|
||||||
|
create_plot_config(variable, rank_var, zscore_var, y_label, sd_band, na_rm, with_annotations = TRUE)
|
||||||
|
|
||||||
# Create plot without annotations
|
# Create plot without annotations
|
||||||
plot_configs[[length(plot_configs) + 1]] <- create_plot_config(variable, rank_var, zscore_var, y_label, sd_band, with_annotations = FALSE)
|
plot_configs[[length(plot_configs) + 1]] <-
|
||||||
|
create_plot_config(variable, rank_var, zscore_var, y_label, sd_band, na_rm, with_annotations = FALSE)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -1650,21 +1659,23 @@ main <- function() {
|
|||||||
generate_and_save_plots(out_dir, "rank_plots_lm", rank_lm_plot_configs,
|
generate_and_save_plots(out_dir, "rank_plots_lm", rank_lm_plot_configs,
|
||||||
page_width = 18, page_height = 12)
|
page_width = 18, page_height = 12)
|
||||||
|
|
||||||
message("Generating filtered ranked plots")
|
message("Generating overlapped ranked plots")
|
||||||
rank_plot_filtered_configs <- generate_rank_plot_configs(
|
rank_plot_filtered_configs <- generate_rank_plot_configs(
|
||||||
df_interactions,
|
df_interactions,
|
||||||
is_lm = FALSE,
|
is_lm = FALSE,
|
||||||
adjust = FALSE,
|
adjust = FALSE,
|
||||||
|
na_rm = TRUE,
|
||||||
overlap_color = TRUE
|
overlap_color = TRUE
|
||||||
)
|
)
|
||||||
generate_and_save_plots(out_dir, "rank_plots_na_rm", rank_plot_filtered_configs,
|
generate_and_save_plots(out_dir, "rank_plots_na_rm", rank_plot_filtered_configs,
|
||||||
page_width = 18, page_height = 12)
|
page_width = 18, page_height = 12)
|
||||||
|
|
||||||
message("Generating filtered ranked linear model plots")
|
message("Generating overlapped ranked linear model plots")
|
||||||
rank_plot_lm_filtered_configs <- generate_rank_plot_configs(
|
rank_plot_lm_filtered_configs <- generate_rank_plot_configs(
|
||||||
df_interactions,
|
df_interactions,
|
||||||
is_lm = TRUE,
|
is_lm = TRUE,
|
||||||
adjust = FALSE,
|
adjust = FALSE,
|
||||||
|
na_rm = TRUE,
|
||||||
overlap_color = TRUE
|
overlap_color = TRUE
|
||||||
)
|
)
|
||||||
generate_and_save_plots(out_dir, "rank_plots_lm_na_rm", rank_plot_lm_filtered_configs,
|
generate_and_save_plots(out_dir, "rank_plots_lm_na_rm", rank_plot_lm_filtered_configs,
|
||||||
@@ -1672,7 +1683,7 @@ main <- function() {
|
|||||||
|
|
||||||
message("Generating correlation curve parameter pair plots")
|
message("Generating correlation curve parameter pair plots")
|
||||||
correlation_plot_configs <- generate_correlation_plot_configs(
|
correlation_plot_configs <- generate_correlation_plot_configs(
|
||||||
df_interactions_joined
|
df_interactions
|
||||||
)
|
)
|
||||||
generate_and_save_plots(out_dir, "correlation_cpps", correlation_plot_configs,
|
generate_and_save_plots(out_dir, "correlation_cpps", correlation_plot_configs,
|
||||||
page_width = 10, page_height = 7)
|
page_width = 10, page_height = 7)
|
||||||
|
|||||||
Reference in New Issue
Block a user