From fc9bfbef952496cd04f1d2185b2dfe0e9f462edd Mon Sep 17 00:00:00 2001 From: Abinaya Yogasekaram <73252787+ayogasekaram@users.noreply.github.com> Date: Fri, 26 Apr 2024 10:21:33 -0400 Subject: [PATCH] update g_lineplot code to handle special characters (#1230) update g_lineplot code to handle special characters in group_var. add test for maintaining factor levels. related to #1212 --------- Signed-off-by: Abinaya Yogasekaram <73252787+ayogasekaram@users.noreply.github.com> Co-authored-by: github-actions <41898282+github-actions[bot]@users.noreply.github.com> --- NEWS.md | 2 + R/g_lineplot.R | 20 +- .../g_lineplot/g_lineplot_factor_levels.svg | 295 ++++++++++++++++++ tests/testthat/test-g_lineplot.R | 18 ++ 4 files changed, 328 insertions(+), 7 deletions(-) create mode 100644 tests/testthat/_snaps/g_lineplot/g_lineplot_factor_levels.svg diff --git a/NEWS.md b/NEWS.md index 731cc7afd6..db0bfdb1db 100644 --- a/NEWS.md +++ b/NEWS.md @@ -1,6 +1,8 @@ # tern 0.9.4.9002 ### Enhancements +* Added `facet_var` to `g_lineplot` to allow plot faceting by a factor variable. +* Updated `g_lineplot` legend to follow factor levels set by users. * Added examples and tests for `label_all` parameter to `extract_survival_biomarkers` and `extract_survival_subgroups`. ### Miscellaneous diff --git a/R/g_lineplot.R b/R/g_lineplot.R index b2770aee8b..df2b80e169 100644 --- a/R/g_lineplot.R +++ b/R/g_lineplot.R @@ -244,11 +244,15 @@ g_lineplot <- function(df, df_N[[strata_N]] <- paste0(df_N[[group_var]], " (N = ", df_N$N, ")") # nolint # keep strata factor levels - matches <- sapply(unique(df_N[[group_var]]), - function(x) unique(df_N[[paste0(group_var, "_N")]]) #nolint - [grepl(paste0("^", x), unique(df_N[[paste0(group_var, "_N")]]))]) - df_N[[paste0(group_var, "_N")]] <- factor(df_N[[group_var]]) #nolint - levels(df_N[[paste0(group_var, "_N")]]) <- unlist(matches) #nolint + matches <- sapply(unique(df_N[[group_var]]), function(x) { + regex_pattern <- gsub("([][(){}^$.|*+?\\\\])", "\\\\\\1", x) + unique(df_N[[paste0(group_var, "_N")]])[grepl( + paste0("^", regex_pattern), + unique(df_N[[paste0(group_var, "_N")]]) + )] + }) + df_N[[paste0(group_var, "_N")]] <- factor(df_N[[group_var]]) # nolint + levels(df_N[[paste0(group_var, "_N")]]) <- unlist(matches) # nolint # strata_N should not be in colnames(df_stats) checkmate::assert_disjunct(strata_N, colnames(df_stats)) @@ -551,7 +555,9 @@ control_lineplot_vars <- function(x = "AVISIT", checkmate::assert_string(paramcd, na.ok = TRUE, null.ok = TRUE) checkmate::assert_string(y_unit, na.ok = TRUE, null.ok = TRUE) - variables <- c(x = x, y = y, group_var = group_var, paramcd = paramcd, - y_unit = y_unit, subject_var = subject_var, facet_var = facet_var) + variables <- c( + x = x, y = y, group_var = group_var, paramcd = paramcd, + y_unit = y_unit, subject_var = subject_var, facet_var = facet_var + ) return(variables) } diff --git a/tests/testthat/_snaps/g_lineplot/g_lineplot_factor_levels.svg b/tests/testthat/_snaps/g_lineplot/g_lineplot_factor_levels.svg new file mode 100644 index 0000000000..2aae1a2d80 --- /dev/null +++ b/tests/testthat/_snaps/g_lineplot/g_lineplot_factor_levels.svg @@ -0,0 +1,295 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +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) + + + + + + + + + + +C: Combination +A: Drug X +B: Placebo +Laboratory Test: ALT (U/L) +Plot of Mean and 80% Confidence Limits by Visit +caption + + + + + + + + + + + + + + + + + + +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) + + + + + + + + + + +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) + + + + + + + + + + +B: Placebo + + + + + + + + + + +A: Drug X + + + + + + + + + + +C: Combination + + +Mean 80% CI +Mean +n +Mean 80% CI +Mean +n +Mean 80% CI +Mean +n + + diff --git a/tests/testthat/test-g_lineplot.R b/tests/testthat/test-g_lineplot.R index 8acd4fcde4..96608268f0 100644 --- a/tests/testthat/test-g_lineplot.R +++ b/tests/testthat/test-g_lineplot.R @@ -50,6 +50,24 @@ testthat::test_that("g_lineplot works with cohort_id specified", { expect_snapshot_ggplot(title = "g_lineplot_cohorts", fig = g_lineplot_cohorts, width = 10, height = 8) }) +testthat::test_that("g_lineplot maintains factor levels in legend", { + adlb$ARM <- factor(adlb$ARM, levels = c("C: Combination", "A: Drug X", "B: Placebo")) + testthat::expect_silent(g_lineplot_factor_levels <- withr::with_options( + opts_partial_match_old, + g_lineplot( + adlb, + 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" + ) + )) + + expect_snapshot_ggplot(title = "g_lineplot_factor_levels", fig = g_lineplot_factor_levels, width = 10, height = 8) +}) testthat::test_that("g_lineplot does not produce a warning if group_var has >6 levels", { set.seed(1) adlb$FACTOR7 <- as.factor(sample(1:7, nrow(adlb), replace = TRUE))