Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Stacked standard curves #212

Draft
wants to merge 18 commits into
base: dev
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ Imports:
lubridate,
R.utils,
svglite,
scales,
fs
Suggests:
knitr,
Expand Down
1 change: 1 addition & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ export(plot_layout)
export(plot_mfi_for_analyte)
export(plot_standard_curve_analyte)
export(plot_standard_curve_analyte_with_model)
export(plot_standard_curve_stacked)
export(process_plate)
export(read_luminex_data)
export(translate_sample_names_to_sample_types)
Expand Down
140 changes: 134 additions & 6 deletions R/plots-standard_curve.R
Original file line number Diff line number Diff line change
Expand Up @@ -134,11 +134,15 @@
#'
#' @param plate Plate object
#' @param model fitted `Model` object, which predictions we want to plot
#' @param data_type Data type of the value we want to plot - the same datatype as in the plate file. By default equals to `Median`
#' @param decreasing_rau_order If `TRUE` the RAU values are plotted in decreasing order, `TRUE` by default.
#' @param log_scale Which elements on the plot should be displayed in log scale. By default `"all"`. If `NULL` or `c()` no log scale is used, if `"all"` or `c("RAU", "MFI")` all elements are displayed in log scale.
#' @param data_type Data type of the value we want to plot - the same
#' datatype as in the plate file. By default equals to `Median`
#' @param decreasing_rau_order If `TRUE` the RAU values are plotted in
#' decreasing order, `TRUE` by default.
#' @param log_scale Which elements on the plot should be displayed in log scale.
#' By default `"all"`. If `NULL` or `c()` no log scale is used,
#' if `"all"` or `c("RAU", "MFI")` all elements are displayed in log scale.
#' @param plot_asymptote If `TRUE` the asymptotes are plotted, `TRUE` by default
#' @param plot_test_predictions If `TRUE` the predictions for the test samples are plotted, `TRUE` by default
#' @param plot_test_predictions If `TRUE` the predictions for the test samples are plotted, `TRUE` by default.
#' The predictions are obtained through extrapolation of the model
#' @param plot_blank_mean If `TRUE` the mean of the blank samples is plotted, `TRUE` by default
#' @param plot_rau_bounds If `TRUE` the RAU bounds are plotted, `TRUE` by default
Expand Down Expand Up @@ -232,12 +236,15 @@
#'
#' @param plate Plate object
#' @param analyte_name Name of the analyte of which standard curve we want to plot.
#' @param data_type Data type of the value we want to plot - the same types as in the plate file. By default equals to `median`
#' @param data_type Data type of the value we want to plot - the same
#' types as in the plate file. By default equals to `median`
#'
#' @return ggplot object with the plot
#'
#' @keywords internal
plot_standard_curve_thumbnail <- function(plate, analyte_name, data_type = "Median") {
plot_standard_curve_thumbnail <- function(plate,
analyte_name,
data_type = "Median") {
if (!inherits(plate, "Plate")) {
stop("plate object should be a Plate")
}
Expand Down Expand Up @@ -282,3 +289,124 @@
)
p
}

#' @title Standard curve stacked plot for levey-jennings report
#'
#' @description
#' Function generates a plot of stacked on top of each other standard curves
#' for a given analyte form a list of plates. The plot is created with the
#' levey-jennings report in mind, but it can be run by itself.
#'
#' @param list_of_plates list of Plate objects
#' @param analyte_name Name of the analyte of which standard curves we want to plot.
#' @param data_type Data type of the value we want to plot - the same
#' datatype as in the plate file. By default equals to `Median`
#' @param monochromatic If `TRUE` the color of standard curves changes
#' from white (the olders) to blue (the newest) it helps to observe drift in
#' calibration of device, otherwise more varied colors are used `TRUE` by default
#' @param decreasing_dilution_order If `TRUE` the dilution values are
#' plotted in decreasing order, `TRUE` by default
#' @param log_scale Which elements on the plot should be displayed in log scale.
#' By default `"all"`. If `NULL` or `c()` no log scale is used,
#' if `"all"` or `c("dilutions", "MFI")` all elements are displayed in log scale.
#' @param verbose If `TRUE` prints messages, `TRUE` by default
#'
#' @return ggplot object with the plot
#'
#' @export
plot_standard_curve_stacked <- function(list_of_plates,
analyte_name,
data_type = "Median",
decreasing_dilution_order = TRUE,
monochromatic = TRUE,
log_scale = c("all"),
verbose = TRUE) {

AVAILABLE_LOG_SCALE_VALUES <- c("all", "dilutions", "MFI")

Check warning on line 325 in R/plots-standard_curve.R

View check run for this annotation

Codecov / codecov/patch

R/plots-standard_curve.R#L325

Added line #L325 was not covered by tests

if (!is.null(log_scale) && !all(log_scale %in% AVAILABLE_LOG_SCALE_VALUES)) {
stop("log_scale should be a character vector containing elements from set: ", paste(AVAILABLE_LOG_SCALE_VALUES, collapse = ", ", "\nInstead passed: ", log_scale))

Check warning on line 328 in R/plots-standard_curve.R

View check run for this annotation

Codecov / codecov/patch

R/plots-standard_curve.R#L327-L328

Added lines #L327 - L328 were not covered by tests
}
if (!is.list(list_of_plates)) {
stop("list_of_plates should be a list of Plate objects, create it using `process_dir` function")

Check warning on line 331 in R/plots-standard_curve.R

View check run for this annotation

Codecov / codecov/patch

R/plots-standard_curve.R#L330-L331

Added lines #L330 - L331 were not covered by tests
}
if (length(list_of_plates) == 0) {
stop("list_of_plates should contain at least one Plate object")

Check warning on line 334 in R/plots-standard_curve.R

View check run for this annotation

Codecov / codecov/patch

R/plots-standard_curve.R#L333-L334

Added lines #L333 - L334 were not covered by tests
}
for (plate in list_of_plates) {
if (!inherits(plate, "Plate")) {
stop("list_of_plates should contain only a Plate objects, create it using `process_dir` function")

Check warning on line 338 in R/plots-standard_curve.R

View check run for this annotation

Codecov / codecov/patch

R/plots-standard_curve.R#L336-L338

Added lines #L336 - L338 were not covered by tests
}
if (!(analyte_name %in% plate$analyte_names)) {
stop(analyte_name, " not found in one of the plate on list_of_plates")

Check warning on line 341 in R/plots-standard_curve.R

View check run for this annotation

Codecov / codecov/patch

R/plots-standard_curve.R#L340-L341

Added lines #L340 - L341 were not covered by tests
}
}

plot_name <- paste0("Standard curves of: ", analyte_name)

Check warning on line 345 in R/plots-standard_curve.R

View check run for this annotation

Codecov / codecov/patch

R/plots-standard_curve.R#L345

Added line #L345 was not covered by tests

# Scale x and y if needed
x_log_scale <- "dilutions" %in% log_scale || "all" %in% log_scale
y_log_scale <- "MFI" %in% log_scale || "all" %in% log_scale
x_trans <- ifelse(x_log_scale, "log10", "identity")
x_cords_trans <- ifelse(decreasing_dilution_order, "reverse", "identity")
y_trans <- ifelse(y_log_scale, "log10", "identity")

Check warning on line 352 in R/plots-standard_curve.R

View check run for this annotation

Codecov / codecov/patch

R/plots-standard_curve.R#L348-L352

Added lines #L348 - L352 were not covered by tests

xlab <- ifelse(x_log_scale, "Dilutions (log scale)", "Dilutions")
x_ticks <- list_of_plates[[1]]$get_dilution_values("STANDARD CURVE")
x_labels <- list_of_plates[[1]]$get_dilution("STANDARD CURVE")

Check warning on line 356 in R/plots-standard_curve.R

View check run for this annotation

Codecov / codecov/patch

R/plots-standard_curve.R#L354-L356

Added lines #L354 - L356 were not covered by tests

# Add the BLANK to the plot
x_ticks <- c(x_ticks, min(x_ticks) / 2)
x_labels <- c(x_labels, "B")
ylab <- ifelse(y_log_scale, paste("MFI ", data_type, "(log scale)"), paste("MFI ", data_type))

Check warning on line 361 in R/plots-standard_curve.R

View check run for this annotation

Codecov / codecov/patch

R/plots-standard_curve.R#L359-L361

Added lines #L359 - L361 were not covered by tests

options(scipen = 30)
p <- ggplot2::ggplot()
p <- p + ggplot2::labs(title = plot_name, x = xlab, y = ylab) +
ggplot2::scale_x_continuous(
labels = x_labels,
breaks = x_ticks,
trans = x_trans
) +
ggplot2::scale_y_continuous(trans = y_trans) +
ggplot2::coord_trans(x = x_cords_trans) +
ggplot2::theme_minimal() +
ggplot2::theme(
axis.line = element_line(colour = "black"),
axis.text.x = element_text(size = 9, angle = 45, hjust = 1, vjust = 1),
axis.text.y = element_text(size = 9),
legend.position = "none",
panel.grid.minor = element_line(color = scales::alpha("grey", .5), size = 0.1) # Make the minor grid lines less visible
)

Check warning on line 380 in R/plots-standard_curve.R

View check run for this annotation

Codecov / codecov/patch

R/plots-standard_curve.R#L363-L380

Added lines #L363 - L380 were not covered by tests

number_of_colors <- length(list_of_plates)
counter <- 1
if (monochromatic) {

Check warning on line 384 in R/plots-standard_curve.R

View check run for this annotation

Codecov / codecov/patch

R/plots-standard_curve.R#L382-L384

Added lines #L382 - L384 were not covered by tests
# I don't want white and next one to be colors since on white background it's not visible
number_of_colors <- number_of_colors + 2
counter <- counter + 2 # skip white and closest one to white

Check warning on line 387 in R/plots-standard_curve.R

View check run for this annotation

Codecov / codecov/patch

R/plots-standard_curve.R#L386-L387

Added lines #L386 - L387 were not covered by tests

palette <- grDevices::colorRampPalette(c("white", "blue"))
colors <- palette(number_of_colors)

Check warning on line 390 in R/plots-standard_curve.R

View check run for this annotation

Codecov / codecov/patch

R/plots-standard_curve.R#L389-L390

Added lines #L389 - L390 were not covered by tests
} else {
colors <- scales::hue_pal()(number_of_colors)

Check warning on line 392 in R/plots-standard_curve.R

View check run for this annotation

Codecov / codecov/patch

R/plots-standard_curve.R#L392

Added line #L392 was not covered by tests
}

for (plate in list_of_plates) {
blank_mean <- mean(plate$get_data(analyte_name, "BLANK", data_type = data_type))

Check warning on line 396 in R/plots-standard_curve.R

View check run for this annotation

Codecov / codecov/patch

R/plots-standard_curve.R#L395-L396

Added lines #L395 - L396 were not covered by tests

plot_data <- data.frame(
MFI = c(plate$get_data(analyte_name, "STANDARD CURVE", data_type = data_type), blank_mean),
dilutions_value = c(plate$get_dilution_values("STANDARD CURVE"), min(plate$get_dilution_values("STANDARD CURVE")) / 2)
)

Check warning on line 401 in R/plots-standard_curve.R

View check run for this annotation

Codecov / codecov/patch

R/plots-standard_curve.R#L398-L401

Added lines #L398 - L401 were not covered by tests

# Add standard curve samples to the plot
p <- p + ggplot2::geom_point(data = plot_data, aes(x = dilutions_value, y = MFI), color = colors[counter], size = 3) +
ggplot2::geom_line(data = plot_data, aes(x = dilutions_value, y = MFI), color = "black", linewidth = 1.5) +
ggplot2::geom_line(data = plot_data, aes(x = dilutions_value, y = MFI), color = colors[counter], linewidth = 1.1)

Check warning on line 406 in R/plots-standard_curve.R

View check run for this annotation

Codecov / codecov/patch

R/plots-standard_curve.R#L404-L406

Added lines #L404 - L406 were not covered by tests

counter <- counter + 1

Check warning on line 408 in R/plots-standard_curve.R

View check run for this annotation

Codecov / codecov/patch

R/plots-standard_curve.R#L408

Added line #L408 was not covered by tests
}

p

Check warning on line 411 in R/plots-standard_curve.R

View check run for this annotation

Codecov / codecov/patch

R/plots-standard_curve.R#L411

Added line #L411 was not covered by tests
}
2 changes: 1 addition & 1 deletion man/Model.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

12 changes: 8 additions & 4 deletions man/plot_standard_curve_analyte_with_model.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

45 changes: 45 additions & 0 deletions man/plot_standard_curve_stacked.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion man/plot_standard_curve_thumbnail.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

File renamed without changes.
File renamed without changes.
Loading