diff --git a/R/jskm.R b/R/jskm.R index e5600dd..0c2b155 100644 --- a/R/jskm.R +++ b/R/jskm.R @@ -129,7 +129,7 @@ jskm <- function(sfit, n.risk <- n.censor <- surv <- strata <- lower <- upper <- NULL times <- seq(0, max(sfit$time), by = timeby) - if (!is.null(theme) && theme == "nejm") legendposition <- c(1, 0.5) + if (!is.null(theme) && theme == "nejm") legendposition <- legendposition if (is.null(subs)) { if (length(levels(summary(sfit)$strata)) == 0) { subs1 <- 1 @@ -190,7 +190,7 @@ jskm <- function(sfit, L <- summary(sfit)$table["0.95LCL"][[1]] U <- summary(sfit)$table["0.95UCL"][[1]] median_time <- summary(sfit)$table["median"][[1]] - ystratalabs2 <- paste0(ystratalabs, " (median : ", median_time, ", 0.95 CI : ",L, "-", U, ")") + ystratalabs2 <- paste0(ystratalabs, " (median : ", median_time, ", 95%CI : ",L, "-", U, ")") } else { # [subs1] if (is.null(ystratalabs)) @@ -200,7 +200,7 @@ jskm <- function(sfit, L <- summary(sfit)$table[,"0.95LCL"][[i]] U <- summary(sfit)$table[,"0.95UCL"][[i]] median_time <- summary(sfit)$table[,"median"][[i]] - ystratalabs2 <- c(ystratalabs2, paste0(ystratalabs[[i]], " (median : ", median_time, ", 0.95 CI : ",L, "-", U, ")")) + ystratalabs2 <- c(ystratalabs2, paste0(ystratalabs[[i]], " (median : ", median_time, ", 95%CI : ",L, "-", U, ")")) } } } @@ -377,6 +377,10 @@ jskm <- function(sfit, scale_x_continuous(xlabs, breaks = times, limits = xlims) + scale_y_continuous(ylabs, limits = ylims, labels = scale_labels) + + + + if (!is.null(theme) && theme == "jama") { p <- p + theme( panel.grid.major.x = element_blank() @@ -467,6 +471,8 @@ jskm <- function(sfit, } # Add median value + + if (med == TRUE & is.null(cut.landmark) & is.null(status.cmprsk)){ if (length(levels(summary(sfit)$strata)) == 0) { median_time <- summary(sfit)$table["median"][[1]] @@ -506,7 +512,7 @@ jskm <- function(sfit, # Add 95% CI to plot if (ci == TRUE) { - if (med == FALSE | !is.null(status.cmprsk)) { + if (med == FALSE | !is.null(status.cmprsk) | (!is.null(theme) && theme == "nejm")) { if (all(linecols2 == "black")) { p <- p + geom_ribbon(data = df, aes(ymin = lower, ymax = upper), alpha = 0.25, colour = NA) } else if (is.null(col.pal)) { @@ -705,7 +711,8 @@ jskm <- function(sfit, if (!is.null(theme) && theme == "nejm") { p2 <- p1 + coord_cartesian(ylim = nejm.infigure.ylim) + theme(axis.title.x = element_blank(), axis.title.y = element_blank(), - axis.text = element_text(size = 10 * nejm.infigure.ratiow) + axis.text = element_text(size = 10 * nejm.infigure.ratiow), + ) + guides(colour = "none", linetype = "none") p <- p + patchwork::inset_element(p2, 1 - nejm.infigure.ratiow, 1 - nejm.infigure.ratioh, 1, 1, align_to = "panel") } @@ -719,4 +726,5 @@ jskm <- function(sfit, } else { p } -} \ No newline at end of file +} + diff --git a/R/svyjskm.R b/R/svyjskm.R index 759140a..ca97a19 100644 --- a/R/svyjskm.R +++ b/R/svyjskm.R @@ -90,7 +90,7 @@ svyjskm <- function(sfit, ...) { surv <- strata <- lower <- upper <- NULL - if (!is.null(theme) && theme == "nejm") legendposition <- c(1, 0.5) + if (!is.null(theme) && theme == "nejm") legendposition <- legendposition if (is.null(timeby)) { if (inherits(sfit, "svykmlist")) { timeby <- signif(max(sapply(sfit, function(x) { @@ -264,7 +264,7 @@ svyjskm <- function(sfit, times <- seq(0, max(sfit$time), by = timeby) if (is.null(ystratalabs)) { ystratalabs <- "All" - ystratalabs2 <- paste0(ystratalabs, " (median : ", unique(df3$med), ")") + ystratalabs2 <- paste0(ystratalabs, " (median : ", unique(df$med), ")") } if (is.null(xlims)) { xlims <- c(0, max(sfit$time)) @@ -289,14 +289,14 @@ svyjskm <- function(sfit, # Final changes to data for survival plot levels(df$strata) <- ystratalabs - zeros <- if (med == T){ + zeros <- if (med == T & is.null(cut.landmark)){ data.frame("strata" = factor(ystratalabs, levels = levels(df$strata)), "time" = 0, "surv" = 1, "med" = 0.5) } else { data.frame("strata" = factor(ystratalabs, levels = levels(df$strata)), "time" = 0, "surv" = 1) } if (ci) { - if (med == T){ + if (med == T & is.null(cut.landmark)){ zeros$med <- NULL zeros$upper <- 1 zeros$lower <- 1 diff --git a/tests/testthat/test-km.R b/tests/testthat/test-km.R index 324ccf1..d40338c 100644 --- a/tests/testthat/test-km.R +++ b/tests/testthat/test-km.R @@ -7,7 +7,7 @@ library(ggplot2) test_that("Run jskm", { fit <- survfit(Surv(time, status) ~ rx, data = colon) expect_is(jskm(fit, timeby = 500, table = T, pval = T), "gg") - expect_is(jskm(fit, + expect_is(jskm(fit, med = T, table = T, pval = T, label.nrisk = "No. at risk", timeby = 365, xlabs = "Time(Day)", ylabs = "Survival", marks = F, xlims = c(0, 3500), ylims = c(0.25, 1), ystratalabs = c("Obs", "Lev", "Lev + 5FU"), ystrataname = "rx" ), "gg") @@ -37,7 +37,7 @@ test_that("Run svyjskm", { s1 <- survey::svykm(Surv(time, status > 0) ~ sex, design = dpbc, se = T) expect_is(svyjskm(s1, ci = T), "gg") expect_is(svyjskm(s1, ci = F, ylabs = "Suvrival (%)", surv.scale = "percent"), "gg") - expect_is(svyjskm(s1, table = T, pval = T, design = dpbc), "gg") + expect_is(svyjskm(s1, table = T, pval = T, design = dpbc, med = T), "gg") expect_is(svyjskm(s1, ci = T, cumhaz = T), "gg") expect_is(svyjskm(s1, ci = F, cumhaz = T), "gg") s2 <- survey::svykm(Surv(time, status > 0) ~ sex, design = dpbc, se = F)