From 4e01d197a6bd7c75c2ba60543e3e0674c4deaf0d Mon Sep 17 00:00:00 2001 From: Abinaya Yogasekaram <73252787+ayogasekaram@users.noreply.github.com> Date: Wed, 18 Oct 2023 01:59:55 -0400 Subject: [PATCH] update g_lineplot with cohort param. (#1078) closes #1077 --------- Co-authored-by: Davide Garolini Co-authored-by: Emily de la Rua --- R/g_lineplot.R | 12 +- man/control_lineplot_vars.Rd | 5 +- man/g_lineplot.Rd | 2 + .../_snaps/g_lineplot/g-lineplot-cohorts.svg | 296 ++++++++++++++++++ tests/testthat/test-g_lineplot.R | 16 + 5 files changed, 327 insertions(+), 4 deletions(-) create mode 100644 tests/testthat/_snaps/g_lineplot/g-lineplot-cohorts.svg diff --git a/R/g_lineplot.R b/R/g_lineplot.R index c64302d466..1941a34dd7 100644 --- a/R/g_lineplot.R +++ b/R/g_lineplot.R @@ -10,6 +10,8 @@ #' * `x` (`character`)\cr name of x-axis variable. #' * `y` (`character`)\cr name of y-axis variable. #' * `strata` (`character`)\cr name of grouping variable, i.e. treatment arm. Can be `NA` to indicate lack of groups. +#' * `cohort_id` (`character`)\cr name of the variable that identifies group belonging. Only applies if `strata` is +#' not NULL. #' * `paramcd` (`character`)\cr name of the variable for parameter's code. Used for y-axis label and plot's subtitle. #' Can be `NA` if `paramcd` is not to be added to the y-axis label or subtitle. #' * `y_unit` (`character`)\cr name of variable with units of `y`. Used for y-axis label and plot's subtitle. @@ -173,6 +175,7 @@ g_lineplot <- function(df, strata <- NULL # NULL if strata == NA or it is not in variables } else { strata <- variables[["strata"]] + cohort_id <- variables[["cohort_id"]] } checkmate::assert_flag(y_lab_add_paramcd, null.ok = TRUE) checkmate::assert_flag(subtitle_add_paramcd, null.ok = TRUE) @@ -216,7 +219,7 @@ g_lineplot <- function(df, if (!is.null(strata) && !is.null(alt_counts_df)) { strata_N <- paste0(strata, "_N") # nolint - df_N <- as.data.frame(table(alt_counts_df[[strata]], exclude = c(NA, NaN, Inf))) # nolint + df_N <- stats::aggregate(USUBJID ~ eval(parse(text = strata)), data = alt_counts_df, FUN = function(x) length(unique(x))) # nolint colnames(df_N) <- c(strata, "N") # nolint df_N[[strata_N]] <- paste0(df_N[[strata]], " (N = ", df_N$N, ")") # nolint @@ -471,6 +474,7 @@ h_format_row <- function(x, format, labels = NULL) { #' @param x (`character`)\cr x variable name. #' @param y (`character`)\cr y variable name. #' @param strata (`character` or `NA`)\cr strata variable name. +#' @param cohort_id (`character` or `NA`)\cr variable to identify subjects in cohorts. #' @param paramcd (`character` or `NA`)\cr `paramcd` variable name. #' @param y_unit (`character` or `NA`)\cr `y_unit` variable name. #' @@ -481,13 +485,15 @@ h_format_row <- function(x, format, labels = NULL) { #' control_lineplot_vars(strata = NA) #' #' @export -control_lineplot_vars <- function(x = "AVISIT", y = "AVAL", strata = "ARM", paramcd = "PARAMCD", y_unit = "AVALU") { +control_lineplot_vars <- function(x = "AVISIT", y = "AVAL", strata = "ARM", paramcd = "PARAMCD", y_unit = "AVALU", + cohort_id = "USUBJID") { checkmate::assert_string(x) checkmate::assert_string(y) checkmate::assert_string(strata, na.ok = TRUE) + checkmate::assert_string(cohort_id, na.ok = TRUE) checkmate::assert_string(paramcd, na.ok = TRUE) checkmate::assert_string(y_unit, na.ok = TRUE) - variables <- c(x = x, y = y, strata = strata, paramcd = paramcd, y_unit = y_unit) + variables <- c(x = x, y = y, strata = strata, paramcd = paramcd, y_unit = y_unit, cohort_id = cohort_id) return(variables) } diff --git a/man/control_lineplot_vars.Rd b/man/control_lineplot_vars.Rd index 7d3200963b..f1100df4dd 100644 --- a/man/control_lineplot_vars.Rd +++ b/man/control_lineplot_vars.Rd @@ -9,7 +9,8 @@ control_lineplot_vars( y = "AVAL", strata = "ARM", paramcd = "PARAMCD", - y_unit = "AVALU" + y_unit = "AVALU", + cohort_id = "USUBJID" ) } \arguments{ @@ -22,6 +23,8 @@ control_lineplot_vars( \item{paramcd}{(\code{character} or \code{NA})\cr \code{paramcd} variable name.} \item{y_unit}{(\code{character} or \code{NA})\cr \code{y_unit} variable name.} + +\item{cohort_id}{(\code{character} or \code{NA})\cr variable to identify subjects in cohorts.} } \value{ A named character vector of variable names. diff --git a/man/g_lineplot.Rd b/man/g_lineplot.Rd index 1128182477..5bdedee593 100644 --- a/man/g_lineplot.Rd +++ b/man/g_lineplot.Rd @@ -45,6 +45,8 @@ g_lineplot( \item \code{x} (\code{character})\cr name of x-axis variable. \item \code{y} (\code{character})\cr name of y-axis variable. \item \code{strata} (\code{character})\cr name of grouping variable, i.e. treatment arm. Can be \code{NA} to indicate lack of groups. +\item \code{cohort_id} (\code{character})\cr name of the variable that identifies group belonging. Only applies if \code{strata} is +not NULL. \item \code{paramcd} (\code{character})\cr name of the variable for parameter's code. Used for y-axis label and plot's subtitle. Can be \code{NA} if \code{paramcd} is not to be added to the y-axis label or subtitle. \item \code{y_unit} (\code{character})\cr name of variable with units of \code{y}. Used for y-axis label and plot's subtitle. diff --git a/tests/testthat/_snaps/g_lineplot/g-lineplot-cohorts.svg b/tests/testthat/_snaps/g_lineplot/g-lineplot-cohorts.svg new file mode 100644 index 0000000000..b3fd53ddc7 --- /dev/null +++ b/tests/testthat/_snaps/g_lineplot/g-lineplot-cohorts.svg @@ -0,0 +1,296 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +18 +19 +20 +21 +22 + + + + + + + + + + + +BASELINE +WEEK 1 DAY 8 +WEEK 2 DAY 15 +WEEK 3 DAY 22 +WEEK 4 DAY 29 +WEEK 5 DAY 36 +Lab Test ALT (U/L) + +Description of Planned Arm + + + + + + + + + + + + +A: Drug X (N = 69) +B: Placebo (N = 73) +C: Combination (N = 58) +Laboratory Test: ALT (U/L) +Plot of Mean and 80% Confidence Limits by Visit +caption + + + + + + + + + + + + + + + + + + +69 +19.2 +(18.48, 19.90) +69 +20.8 +(20.14, 21.42) +69 +19.6 +(18.90, 20.27) +69 +19.6 +(19.03, 20.23) +69 +20.3 +(19.67, 20.93) +69 +19.8 +(19.16, 20.42) + + + + + + + + + + +73 +20.3 +(19.66, 20.99) +73 +20.2 +(19.54, 20.77) +73 +20.7 +(19.97, 21.34) +73 +19.4 +(18.78, 20.02) +73 +20.4 +(19.71, 21.07) +73 +19.3 +(18.59, 19.93) + + + + + + + + + + +58 +19.2 +(18.49, 19.87) +58 +19.4 +(18.73, 19.99) +58 +20.0 +(19.43, 20.66) +58 +20.3 +(19.68, 20.95) +58 +21.0 +(20.17, 21.76) +58 +19.4 +(18.65, 20.07) + + + + + + + + + + +C: Combination + + + + + + + + + + +B: Placebo + + + + + + + + + + +A: Drug X + + +Mean 95% CI +Mean +n +Mean 95% CI +Mean +n +Mean 95% CI +Mean +n + + diff --git a/tests/testthat/test-g_lineplot.R b/tests/testthat/test-g_lineplot.R index 8316ffcb44..0c07eece42 100644 --- a/tests/testthat/test-g_lineplot.R +++ b/tests/testthat/test-g_lineplot.R @@ -25,3 +25,19 @@ testthat::test_that("g_lineplot works with custom settings and statistics table" vdiffr::expect_doppelganger(title = "g_lineplot_w_stats", fig = g_lineplot_w_stats) }) + +testthat::test_that("g_lineplot works with cohort_id specified", { + g_lineplot_cohorts <- g_lineplot( + adlb, + adsl, + strata = control_lineplot_vars(strata = "ARM", cohort_id = "USUBJID"), + mid = "median", + table = c("n", "mean", "mean_ci"), + control = control_analyze_vars(conf_level = 0.80), + title = "Plot of Mean and 80% Confidence Limits by Visit", + y_lab = "Lab Test", + subtitle = "Laboratory Test:", + caption = "caption" + ) + vdiffr::expect_doppelganger(title = "g_lineplot_cohorts", fig = g_lineplot_cohorts) +})