Search code examples
rcolorssurvival-analysisadjustment

Adding patients at risk to adjusted Kaplan Meier curves and changing the color of the curves in r


I am trying to do adjusted Kaplan Meier curves based on the variable named group. How do I add patients at risk to adjusted Kaplan Meier curves and how do I change the color of the curves?

Here is my code and simulation data obtained after reviewing this link

library(riskRegression); library(adjustedCurves);library(survival)
set.seed(42)
sim_dat <- sim_confounded_surv(n=500, max_t=1.2)
sim_dat$group <- as.factor(sim_dat$group)
sim_dat$cluster <- sample(1:5, size=nrow(sim_dat), replace=TRUE)

cox_mod <- coxph(Surv(time, event) ~ x1 + x2 + x4 + x5 + group + cluster,  data=sim_dat, x=TRUE)

predict_fun <- function(...) {
  1 - predictRisk(...)
}

# using direct adjustment 
adjsurv <- adjustedsurv(data=sim_dat,
                        variable="group",
                        ev_time="time",
                        event="event",
                        method="direct",
                        outcome_model=cox_mod,
                        predict_fun=predict_fun)

plot(adjsurv, custom_colors = c(  "red","blue") )

I read that there are many other packages in R but none reported patients at risk.

EDITS

I found a relevant link from which I could change the color of curves (added in the code above) but I wasn't able to add aat-risk patinets although they suggested using cowplot::plot_grid.

Any advice will be greatly appreciated.


Solution

  • You are in luck! I have recently pushed an update to the package that allows adding risk tables to the plot. All you need to do is to install the developmental version from github (install_github("RobinDenz1/adjustedCurves")) and you will be able to do it simply by using:

    plot(adjsurv, custom_colors=c("red", "blue"), risk_table=TRUE)
    

    Since you are using method="direct", I would recommend keeping risk_table_stratify=FALSE, because the output may otherwise be confusing (see documentation page of plot.adjustedsurv()).