Skip to content

Commit

Permalink
fixed error in df3
Browse files Browse the repository at this point in the history
  • Loading branch information
MyungHyojong committed Oct 22, 2024
1 parent 0a59f2c commit 0ad7167
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 12 deletions.
20 changes: 14 additions & 6 deletions R/jskm.R
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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))
Expand All @@ -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, ")"))
}
}
}
Expand Down Expand Up @@ -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()
Expand Down Expand Up @@ -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]]
Expand Down Expand Up @@ -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)) {
Expand Down Expand Up @@ -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")
}
Expand All @@ -719,4 +726,5 @@ jskm <- function(sfit,
} else {
p
}
}
}

8 changes: 4 additions & 4 deletions R/svyjskm.R
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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))
Expand All @@ -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
Expand Down
4 changes: 2 additions & 2 deletions tests/testthat/test-km.R
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down Expand Up @@ -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)
Expand Down

0 comments on commit 0ad7167

Please sign in to comment.