I have a dataset with time, status ( 1=death, 0 = censored), treatement =1,2 .
I create my survival object km_2, I want to plot Kaplan-Meijer plot using autoplot(). I do not know what my mistake is but setting the attributes ( legendLabs for example ) do not make any change to the basic KM plot.
km_2 <- survfit(Surv(time, status)~treatment, data=prostate)
library(ggplot2)
library(ggfortify)
autoplot(km_2,
alpha=0.7, #transparency of CIs
shape= 10, #shape used to incdicaed censored obs
xlab= 'month', ylab='% survived',
title = 'KM- plot to compare tr1, tr 2',
legendLabs= c('tr1','tr2'),
pval=T,
plotTable= T
)
There are a few things to point out here. Firstly, autoplot
is a generic function, so the method used and the arguments it accepts depends on the type of object you are passing to it. In this case, you are passing a survfit
object, and you will be able to see the correct parameters to use if you type ?autoplot.survfit
into the console.
From this you will see that there is no legendLabs
or plotTable
option, and that the alpha for the confidence intervals is controlled with conf.int.alpha =
. Similarly, the censoring shape is controlled with censor.shape
.
Another issue is that there doesn't seem to be a way to change the factor labels in the legend, but in this case it is easy enough to change them in the data when you create the survfit
object.
Lastly, it's a good idea to make a reproducible example if you want prompt and useful answers. It took a while to recreate a reasonable data structure to test and demonstrate this answer.
library(survival)
library(ggplot2)
library(ggfortify)
km_2 <- survfit(Surv(time, status) ~ treatment,
data = within(prostate, treatment <- c("tr1", "tr2")[treatment]))
autoplot(km_2,
conf.int.alpha = 0.7,
censor.shape = 10,
xlab = 'month',
ylab = '% survived',
title = 'KM- plot to compare tr1, tr2'
)
As an aside, you might get a result closer to your expectation using survminer
:
library(survminer)
ggsurvplot(km_2, conf.int = TRUE, risk.table = TRUE)
Reproducible data
set.seed(1)
prostate <- data.frame(time = pmin(runif(100) * rep(c(7, 10), each = 50), 5),
treatment = rep(1:2, each = 50),
status = c(rbinom(50, 1, 0.3), rbinom(50, 1, 0.5)))
prostate$status[prostate$time > 5] <- 0