From 29469cef8092182c2258aca63279c5e8a06c3905 Mon Sep 17 00:00:00 2001 From: Mateusz Nizwantowski Date: Thu, 17 Oct 2024 16:58:01 +0200 Subject: [PATCH] Plot standard curve thumbnail (#183) * reformat docs * new function * better visuals and switch in template * add margin * format code, indentation etc * improve visuals * listeners are working in other direction * add synchronisation functionality and slightly refactor code * format code to stay consistent --- R/plots-standard_curve.R | 70 ++++++++++++++++++++++-- inst/templates/plate_report_template.Rmd | 47 ++++++++++++---- man/plot_standard_curve_thumbnail.Rd | 24 ++++++++ tests/testthat/test-helpers.R | 2 +- 4 files changed, 125 insertions(+), 18 deletions(-) create mode 100644 man/plot_standard_curve_thumbnail.Rd diff --git a/R/plots-standard_curve.R b/R/plots-standard_curve.R index ac440a40..e31f4875 100644 --- a/R/plots-standard_curve.R +++ b/R/plots-standard_curve.R @@ -4,7 +4,6 @@ #' Plot standard curve samples of a plate of a given analyte. #' #' @param plate A 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 datatype as in the plate file. By default equals to `Net MFI` #' @param decreasing_rau_order If `TRUE` the RAU values are plotted in decreasing order, `TRUE` by default @@ -130,6 +129,9 @@ plot_standard_curve_analyte <- function(plate, #' Plot standard curve of a certain analyte with fitted model #' +#' @description +#' Function plots the values of standard curve samples and the fitted model. +#' #' @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` @@ -145,10 +147,7 @@ plot_standard_curve_analyte <- function(plate, #' @param ... Additional arguments passed to the `predict` function #' #' @return a ggplot object with the plot -#' -#' @description -#' Function plots the values of standard curve samples and the fitted model. -#' +# #' #' @import ggplot2 #' @@ -222,3 +221,64 @@ plot_standard_curve_analyte_with_model <- function(plate, ) return(p) } + + +#' @title Standard curve thumbnail for report +#' +#' @description +#' Function generates a thumbnail of the standard curve for a given analyte. +#' The thumbnail is used in the plate report. It doesn't have any additional +#' parameters, because it is used only internally. +#' +#' @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` +#' +#' @return ggplot object with the plot +#' +#' @keywords internal +plot_standard_curve_thumbnail <- function(plate, analyte_name, data_type = "Median") { + if (!inherits(plate, "Plate")) { + stop("plate object should be a Plate") + } + if (!(analyte_name %in% plate$analyte_names)) { + stop(analyte_name, " not found in the plate object") + } + plot_data <- data.frame( + MFI = plate$get_data(analyte_name, "STANDARD CURVE", data_type = data_type), + plate = plate$plate_name, + RAU = dilution_to_rau(plate$get_dilution_values("STANDARD CURVE")) + ) + blank_mean <- mean(plate$get_data(analyte_name, "BLANK", data_type = data_type)) + x_ticks <- c(plot_data$RAU, max(plot_data$RAU) + 1) + x_labels <- c(sprintf("%0.2f", plot_data$RAU), "") + + + + p <- ggplot2::ggplot(plot_data, aes(x = .data$RAU, y = .data$MFI)) + + ggplot2::geom_point(aes(color = "Standard curve samples"), size = 9) + + ggplot2::geom_hline( + aes(yintercept = blank_mean, color = "Blank mean"), + linetype = "solid", linewidth = 1.8 + ) + + ggplot2::labs(title = analyte_name, x = "", y = "") + + ggplot2::scale_x_continuous( + breaks = x_ticks, labels = x_labels, + trans = "log10" + ) + + #ggplot2::scale_y_continuous(trans = "log10") + + ggplot2::theme_minimal() + + ggplot2::theme( + axis.line = element_line(colour = "black", size = 2), + axis.text.x = element_text(size = 0), + axis.text.y = element_text(size = 0), + legend.position = "none", + plot.title = element_text(hjust = 0.5, size = 50), + panel.grid.minor = element_blank(), + ) + + ggplot2::coord_trans(x = "reverse") + + ggplot2::scale_color_manual( + values = c("Standard curve samples" = "blue", "Blank mean" = "red", "Min-max RAU bounds" = "gray") + ) + p +} diff --git a/inst/templates/plate_report_template.Rmd b/inst/templates/plate_report_template.Rmd index 641bb5e6..388df945 100644 --- a/inst/templates/plate_report_template.Rmd +++ b/inst/templates/plate_report_template.Rmd @@ -68,6 +68,7 @@ plot_layout(params$plate) display: grid; grid-template-columns: repeat(5, 1fr); gap: 5px; /* Space between the plots */ + margin-top: 20px; } .plot-container { @@ -89,31 +90,33 @@ plot_layout(params$plate) @@ -144,7 +166,8 @@ for (i in seq_along(params$plate$analyte_names)) { '') } # Close the grid diff --git a/man/plot_standard_curve_thumbnail.Rd b/man/plot_standard_curve_thumbnail.Rd new file mode 100644 index 00000000..b1ecb42c --- /dev/null +++ b/man/plot_standard_curve_thumbnail.Rd @@ -0,0 +1,24 @@ +% Generated by roxygen2: do not edit by hand +% Please edit documentation in R/plots-standard_curve.R +\name{plot_standard_curve_thumbnail} +\alias{plot_standard_curve_thumbnail} +\title{Standard curve thumbnail for report} +\usage{ +plot_standard_curve_thumbnail(plate, analyte_name, data_type = "Median") +} +\arguments{ +\item{plate}{Plate object} + +\item{analyte_name}{Name of the analyte of which standard curve we want to plot.} + +\item{data_type}{Data type of the value we want to plot - the same types as in the plate file. By default equals to \code{median}} +} +\value{ +ggplot object with the plot +} +\description{ +Function generates a thumbnail of the standard curve for a given analyte. +The thumbnail is used in the plate report. It doesn't have any additional +parameters, because it is used only internally. +} +\keyword{internal} diff --git a/tests/testthat/test-helpers.R b/tests/testthat/test-helpers.R index 048ed1c6..423b2c67 100644 --- a/tests/testthat/test-helpers.R +++ b/tests/testthat/test-helpers.R @@ -39,7 +39,7 @@ test_that("Test is.scalar", { test_that("Test verbose cat", { expect_output(verbose_cat("a", "b"), "ab") - expect_null(verbose_cat("a", "b", verbose = F)) + expect_null(verbose_cat("a", "b", verbose = FALSE)) }) test_that("Test clamp function", {