I am trying to create a survival plot in R for deaths from exposure to a fungal disease over a number of weeks. I have the death week(continuous), whether they were alive (TRUE/FALSE), as well as categorical variables for diet (high/low) and sex(male/female). I have run a coxph model:
surv1 <- coxph(Surv(week_died,alive) ~ exposed + diet + sex,
data=surv)
I would like to plot a survival curve, with individual lines for exposed males on high and low diets, and the same for females on high and low diets (resulting in 4 individual survival curves on the same plot). if I use this then I only get a single curve.
plot(survfit(surv1), ylim=c(), xlab="Weeks")
I have also tried to use the ggsurv function created by Edwin Thoen (http://www.r-statistics.com/2013/07/creating-good-looking-survival-curves-the-ggsurv-function/) but keep getting an error for "invalid line type". I have tried to work out what could be causing this and think it would be this last ifelse statement - but I am not sure.
pl <- if(strata == 1) {ggsurv.s(s, CI , plot.cens, surv.col ,
cens.col, lty.est, lty.ci,
cens.shape, back.white, xlab,
ylab, main)
} else {ggsurv.m(s, CI, plot.cens, surv.col ,
cens.col, lty.est, lty.ci,
cens.shape, back.white, xlab,
ylab, main)}
Does anyone have any idea on what is causing this error/how to fix it or if I completely trying to do the wrong thing to plot these curves.
Many thanks!
survfit
on acoxph
model without any other specifications gives the survival curve for a case whose covariate predictors are the average of the population that the model was created with. From the help forsurvfit.coxph
So after you have computed
surv1
,sf
should also work as an argument toggsurv
, though I've not tested it.