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