gugl58
6/27/2018 - 11:38 AM

plot.KM

Plot a Kaplan Meier curve for a survival time, a respective survival event and a discrete riskclass

plot.KM <- function(survivaltime
					,survivalevent
					,riskclass
					,maintitle=NULL
					,xlab="Time (years)"
					,ylab="Overall Survival (proportion)"){
	inst.load.packages("survival", silent = TRUE)
	inst.load.packages("survminer", silent = TRUE) # ggsurvplot for survival curves
	
	surv.time <- Surv(time = survivaltime,event = survivalevent)
	tmp.surv.df <- data.frame("survivaltime"=surv.time
							  ,"class"=as.character(riskclass))
	confidenceinterval <- TRUE
	if(any(survfit(survivaltime ~ class, data=tmp.surv.df)$n == 1)) # I have an error and I JUST ASSUME!! that this is the problem.
		confidenceinterval <- FALSE
	tmp <- ggsurvplot(fit = survfit(survivaltime ~ class, data=tmp.surv.df)
					  ,data = tmp.surv.df
					  ,conf.int = confidenceinterval
					  ,risk.table = TRUE
					  ,title=maintitle
					  ,risk.table.pos = "in"
					  ,xlab=xlab
					  ,ylab=ylab
					  ,legend.labs=levels(tmp.surv.df$class)
					  ,pval = TRUE) 
	return(tmp)
}