kassambara / survminer

Survival Analysis and Visualization
https://rpkgs.datanovia.com/survminer/
509 stars 162 forks source link

How to plot Cox model which has risk.table group by grouped variable with ggsurvplot()? #478

Open youyuxiansen opened 4 years ago

youyuxiansen commented 4 years ago

Hi @kassambara . From the description in #67 image

I'm using coxph for adjusting by age and wt.loss.I want to draw the graph which has risk.table of each level of sex.

Here is an example code of my question.

library("survival")
# Fit cox ph
#%%%%%%%%%%%%%
res.cox <- coxph(Surv(time, status) ~ age + sex + wt.loss, data =  lung)
res.cox

# Survival curves by sex after adjusting by age and wt.loss
#%%%%%%%%%%%%%%%%%%%%%%%%
# we construct a new data frame with two rows, 
# one for each value of sex; the other covariates are fixed to their average values 
# Create the new data  
new_df <- with(lung,
               data.frame(sex = c(1, 2), 
                          age = rep(mean(age, na.rm = TRUE), 2),
                          wt.loss = rep(mean(wt.loss, na.rm = TRUE), 2)
               )
)
new_df

# Survival curves with new data
#%%%%%%%%%%%%%%%%%%%%%%%%%%%
library(survminer)
fit <- survfit(res.cox, newdata = new_df)
ggsurvplot(fit, conf.int = TRUE, palette = "Dark2", data = lung,
           censor = FALSE, surv.median.line = "hv", risk.table = TRUE)
TBradley27 commented 1 year ago

Hello, I am also having this problem at the moment