Plot tweaks for before QC

This commit is contained in:
2024-09-26 01:34:18 -04:00
parent 6256127279
commit fe9dfd9e38

View File

@@ -91,28 +91,29 @@ theme_publication <- function(base_size = 14, base_family = "sans", legend_posit
theme_foundation %+replace%
theme(
plot.title = element_text(face = "bold", size = rel(1.2), hjust = 0.5),
plot.title = element_text(face = "bold", size = rel(1.6), hjust = 0.5),
text = element_text(),
panel.background = element_rect(colour = NA),
plot.background = element_rect(colour = NA),
panel.border = element_rect(colour = NA),
axis.title = element_text(face = "bold", size = rel(1)),
panel.background = element_blank(),
plot.background = element_blank(),
panel.border = element_blank(),
axis.title = element_text(face = "bold", size = rel(1.4)),
axis.title.y = element_text(angle = 90, vjust = 2),
axis.title.x = element_text(vjust = -0.2),
axis.text = element_text(),
# axis.title.x = element_text(vjust = -0.2), # TODO this causes errors
axis.text = element_text(size = rel(1.2)),
axis.line = element_line(colour = "black"),
axis.ticks = element_line(),
# axis.ticks = element_line(),
panel.grid.major = element_line(colour = "#f0f0f0"),
panel.grid.minor = element_blank(),
legend.key = element_rect(colour = NA),
legend.position = legend_position,
legend.direction = ifelse(legend_position == "right", "vertical", "horizontal"),
legend.key.size = unit(0.5, "cm"),
# legend.key.size = unit(0.5, "cm"),
legend.spacing = unit(0, "cm"),
legend.title = element_text(face = "italic"),
legend.title = element_text(face = "italic", size = rel(1.3)),
legend.text = element_text(size = rel(1.2)),
plot.margin = unit(c(10, 5, 5, 5), "mm"),
strip.background = element_rect(colour = "#f0f0f0", fill = "#f0f0f0"),
strip.text = element_text(face = "bold")
# strip.background = element_rect(colour = "#f0f0f0", fill = "#f0f0f0"),
# strip.text = element_text(face = "bold")
)
}
@@ -384,39 +385,39 @@ generate_and_save_plots <- function(out_dir, filename, plot_configs, grid_layout
# Create the base plot
aes_mapping <- if (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 = as.factor(.data[[config$color_var]]), color = as.factor(.data[[config$color_var]]))
} else {
aes(x = .data[[config$x_var]])
}
} else if (config$plot_type == "density") {
if (!is.null(config$color_var)) {
aes(x = .data[[config$x_var]], color = .data[[config$color_var]])
aes(x = .data[[config$x_var]], color = as.factor(.data[[config$color_var]]))
} else {
aes(x = .data[[config$x_var]])
}
} else {
if (!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 = as.factor(.data[[config$color_var]]))
} else {
aes(x = .data[[config$x_var]], y = .data[[config$y_var]])
}
}
plot_base <- ggplot(df, aes_mapping)
# Use appropriate helper function based on plot type
plot <- switch(config$plot_type,
"scatter" = generate_scatter_plot(plot_base, config),
"box" = generate_box_plot(plot_base, config),
"density" = plot_base + geom_density(),
"bar" = plot_base + geom_bar(),
plot_base # default case if no type matches
)
plot <- ggplot(df, aes_mapping)
# Apply theme_publication with legend_position from config
legend_position <- if (!is.null(config$legend_position)) config$legend_position else "bottom"
plot <- plot + theme_publication(legend_position = legend_position)
# Use appropriate helper function based on plot type
plot <- switch(config$plot_type,
"scatter" = generate_scatter_plot(plot, config),
"box" = generate_box_plot(plot, config),
"density" = plot + geom_density(),
"bar" = plot + geom_bar(),
plot # default case if no type matches
)
# Add title and labels
if (!is.null(config$title)) {
plot <- plot + ggtitle(config$title)
@@ -428,6 +429,11 @@ generate_and_save_plots <- function(out_dir, filename, plot_configs, grid_layout
plot <- plot + ylab(config$y_label)
}
# Add cartesian coordinates if specified
if (!is.null(config$coord_cartesian)) {
plot <- plot + coord_cartesian(ylim = config$coord_cartesian)
}
# Apply scale_color_discrete(guide = FALSE) when color_var is NULL
if (is.null(config$color_var)) {
plot <- plot + scale_color_discrete(guide = "none")
@@ -486,14 +492,17 @@ generate_and_save_plots <- function(out_dir, filename, plot_configs, grid_layout
}
generate_scatter_plot <- function(plot, config) {
# Define the points
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 1.5
position <-
if (!is.null(config$position) && config$position == "jitter") {
position_jitter(width = 0.1, height = 0)
} else {
"identity"
}
plot <- plot + geom_point(
shape = shape,
size = size,
@@ -594,17 +603,12 @@ generate_scatter_plot <- function(plot, config) {
)
}
# Apply coord_cartesian if specified
if (!is.null(config$coord_cartesian)) {
plot <- plot + coord_cartesian(ylim = config$coord_cartesian)
}
# Set Y-axis limits if specified
if (!is.null(config$ylim_vals)) {
plot <- plot + scale_y_continuous(limits = config$ylim_vals)
}
# Add Annotations if specified
# Add annotations if specified
if (!is.null(config$annotations)) {
for (annotation in config$annotations) {
plot <- plot +
@@ -615,17 +619,12 @@ generate_scatter_plot <- function(plot, config) {
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), 4, annotation$size),
size = ifelse(is.null(annotation$size), 6, annotation$size),
color = ifelse(is.null(annotation$color), "black", annotation$color)
)
}
}
# Add Title if specified
if (!is.null(config$title)) {
plot <- plot + ggtitle(config$title)
}
return(plot)
}
@@ -640,10 +639,6 @@ generate_box_plot <- function(plot, config) {
)
}
if (!is.null(config$coord_cartesian)) {
plot <- plot + coord_cartesian(ylim = config$coord_cartesian)
}
return(plot)
}
@@ -680,7 +675,7 @@ generate_plate_analysis_plot_configs <- function(variables, stages = c("before",
plot_type = plot_type,
title = paste("Plate analysis by Drug Conc for", var, stage, "quality control"),
error_bar = error_bar,
color_var = "conc_num_factor",
color_var = "conc_num",
position = position
)
plots <- append(plots, list(config))
@@ -1126,7 +1121,7 @@ main <- function() {
plot_type = "scatter",
delta_bg_point = TRUE,
title = "Raw L vs K before quality control",
color_var = "conc_num_factor",
color_var = "conc_num",
error_bar = FALSE,
legend_position = "right"
)
@@ -1138,8 +1133,8 @@ main <- function() {
x_var = "delta_bg",
y_var = NULL,
plot_type = "density",
title = "Density plot for Delta Background by Conc All Data",
color_var = "conc_num_factor",
title = "Density plot for Delta Background by [Drug] (All Data)",
color_var = "conc_num",
x_label = "Delta Background",
y_label = "Density",
error_bar = FALSE,
@@ -1149,8 +1144,8 @@ main <- function() {
x_var = "delta_bg",
y_var = NULL,
plot_type = "bar",
title = "Bar plot for Delta Background by Conc All Data",
color_var = "conc_num_factor",
title = "Bar plot for Delta Background by [Drug] (All Data)",
color_var = "conc_num",
x_label = "Delta Background",
y_label = "Count",
error_bar = FALSE,
@@ -1165,8 +1160,8 @@ main <- function() {
plot_type = "scatter",
delta_bg_point = TRUE,
title = paste("Raw L vs K for strains above Delta Background threshold of",
df_above_tolerance$delta_bg_tolerance[[1]], "or above"),
color_var = "conc_num_factor",
round(df_above_tolerance$delta_bg_tolerance[[1]], 3), "or above"),
color_var = "conc_num",
position = "jitter",
annotations = list(
list(
@@ -1214,7 +1209,7 @@ main <- function() {
plot_type = "scatter",
delta_bg_point = TRUE,
title = "Raw L vs K for strains falling outside 2SD of the K mean at each Conc",
color_var = "conc_num_factor",
color_var = "conc_num",
position = "jitter",
legend_position = "right"
)
@@ -1228,7 +1223,7 @@ main <- function() {
plot_type = "scatter",
gene_point = TRUE,
title = "Delta Background vs K for strains falling outside 2SD of the K mean at each Conc",
color_var = "conc_num_factor",
color_var = "conc_num",
position = "jitter",
legend_position = "right"
)
@@ -1453,7 +1448,6 @@ main <- function() {
adjust = FALSE,
overlap_color = TRUE
)
generate_and_save_plots(
out_dir = out_dir,
filename = "RankPlots_na_rm",