Skip to content

Commit

Permalink
.connect2origin() helper modified to handle correctly strata levels, …
Browse files Browse the repository at this point in the history
…and so facet. fix for #254, #363
  • Loading branch information
kassambara committed Jan 30, 2019
1 parent a5990ea commit 3ea63df
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 23 deletions.
2 changes: 2 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@

## Bug fixes

- `ggsurvplot()` can now create correctly faceted survival curves ([@uraniborg, #254](https://github.com/kassambara/survminer/pull/254), [@BingxinS, #363](https://github.com/kassambara/survminer/pull/363))

- A typo fixed in the formula for weightened log-rank test ([@MarcinKosinski, #336](https://github.com/kassambara/survminer/pull/336).

- `surv_summary()` can now handle the output of `survfit(cox.model, newdata)` when the option `conf.type = "none"` is specified by users ([@HeidiSeibold, #335](https://github.com/kassambara/survminer/pull/335).
Expand Down
30 changes: 7 additions & 23 deletions R/ggsurvplot_df.R
Original file line number Diff line number Diff line change
Expand Up @@ -299,29 +299,13 @@ ggsurvplot_df <- function(fit, fun = NULL,

# Connect survival data to the origine
#::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
.connect2origin <- function(d, fit, data = NULL){
base <- d[1, , drop = FALSE]
base[intersect(c('time', 'n.censor', 'std.err', "n.event"), colnames(base))] <- 0
base[c('surv', 'upper', 'lower')] <- 1.0
n.strata <- length(levels(d$strata))

# Connect each group to the origin
if (n.strata > 1) {
strata <- levels(d$strata)
base <- base[rep(1, n.strata),, drop = FALSE]
row.names(base) <- 1:nrow(base)
base$strata <- strata
base$strata <- factor(strata, levels = strata)
# update variable values
if(!missing(fit)){
if(!inherits(fit, "survfit.cox")){
variables <- .get_variables(base$strata, fit, data)
for(variable in variables) base[[variable]] <- .get_variable_value(variable, base$strata, fit, data)
}
}
}
d <- rbind(base, d)
d
.connect2origin <- function(d, ...){
n.risk <- strata <- NULL
if("n.risk" %in% colnames(d)){d <- dplyr::arrange(d, dplyr::desc(n.risk))}
origin <- d %>% dplyr::distinct(strata, .keep_all = TRUE)
origin[intersect(c('time', 'n.censor', 'std.err', "n.event"), colnames(origin))] <- 0
origin[c('surv', 'upper', 'lower')] <- 1.0
dplyr::bind_rows(origin, d)
}


Expand Down

0 comments on commit 3ea63df

Please sign in to comment.