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

Update default g_km y limits #1021

Merged
merged 8 commits into from
Aug 2, 2023
Merged
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
3 changes: 2 additions & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,8 @@ Suggests:
nestcolor (>= 0.1.1),
rmarkdown,
stringr,
testthat (>= 3.0)
testthat (>= 3.0),
vdiffr (>= 1.0.0)
VignetteBuilder:
knitr
RdMacros:
Expand Down
16 changes: 14 additions & 2 deletions R/kaplan_meier_plot.R
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
#' @param xlab (`string`)\cr label of x-axis.
#' @param ylab (`string`)\cr label of y-axis.
#' @param ylim (`vector` of `numeric`)\cr vector of length 2 containing lower and upper limits for the y-axis.
#' If `NULL` (default), the minimum and maximum y-values displayed are used as limits.
#' @param title (`string`)\cr title for plot.
#' @param footnotes (`string`)\cr footnotes for plot.
#' @param col (`character`)\cr lines colors. Length of a vector should be equal
Expand Down Expand Up @@ -182,7 +183,7 @@ g_km <- function(df,
xlab = "Days",
yval = c("Survival", "Failure"),
ylab = paste(yval, "Probability"),
ylim = c(0, 1),
ylim = NULL,
title = NULL,
footnotes = NULL,
draw = TRUE,
Expand Down Expand Up @@ -639,7 +640,7 @@ h_ggkm <- function(data,
censor_show,
xlab,
ylab,
ylim = c(0, 1),
ylim = NULL,
title,
footnotes = NULL,
max_time = NULL,
Expand All @@ -652,6 +653,17 @@ h_ggkm <- function(data,
ggtheme = nestcolor::theme_nest()) {
checkmate::assert_numeric(lty, null.ok = TRUE)
checkmate::assert_character(col, null.ok = TRUE)

if (is.null(ylim)) {
if (!is.null(max_time)) {
y_lwr <- min(data[data$time < max_time, ][["estimate"]])
y_upr <- max(data[data$time < max_time, ][["estimate"]])
} else {
y_lwr <- min(data[["estimate"]])
y_upr <- max(data[["estimate"]])
}
ylim <- c(y_lwr, y_upr)
}
checkmate::assert_numeric(ylim, finite = TRUE, any.missing = FALSE, len = 2, sorted = TRUE)

# change estimates of survival to estimates of failure (1 - survival)
Expand Down
5 changes: 3 additions & 2 deletions man/g_km.Rd

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

5 changes: 3 additions & 2 deletions man/h_ggkm.Rd

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

160 changes: 160 additions & 0 deletions tests/testthat/_snaps/g_km/g-km-crop-ylim.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
199 changes: 199 additions & 0 deletions tests/testthat/_snaps/g_km/g-km-custom-ylim.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
28 changes: 28 additions & 0 deletions tests/testthat/test-g_km.R
Original file line number Diff line number Diff line change
Expand Up @@ -52,3 +52,31 @@ testthat::test_that("g_km plot with < = > in group labels works", {

testthat::expect_true(grid::is.grob(grob_tmp))
})

testthat::test_that("g_km ylim parameter works as expected", {
set.seed(123)

df <- tern_ex_adtte %>%
dplyr::filter(PARAMCD == "OS") %>%
dplyr::mutate(is_event = CNSR == 0)

variables <- list(tte = "AVAL", is_event = "is_event", arm = "ARMCD")

g_km_crop_ylim <- g_km(
df = df,
variables = variables,
annot_surv_med = FALSE,
annot_at_risk = FALSE,
max_time = 1000
)
vdiffr::expect_doppelganger(title = "g_km_crop_ylim", fig = g_km_crop_ylim)

g_km_custom_ylim <- g_km(
df = df,
variables = variables,
annot_surv_med = FALSE,
annot_at_risk = FALSE,
ylim = c(0.25, 0.75)
)
vdiffr::expect_doppelganger(title = "g_km_custom_ylim", fig = g_km_custom_ylim)
})