Search code examples
rsurvival-analysis

Plot two curves only (Not all) in survfit output in R


I have a set of survival data with a covariate (SubType) in 5 groups. I managed to plot the 5 groups using:

km.type <- survfit(SurvObj ~ SubType, data = data, conf.type = "log-log")
plot(km.type, mark.time=FALSE)

I would like to display only 2 groups instead of all 5 groups. Is there any way to do this?


Solution

  • You can subset the data before, during, or after fitting the survival model

    library('survival')
    
    summary(aml)
    # time            status                   x     
    # Min.   :  5.00   Min.   :0.0000   Maintained   :11  
    # 1st Qu.: 12.50   1st Qu.:1.0000   Nonmaintained:12  
    # Median : 23.00   Median :1.0000                     
    # Mean   : 29.48   Mean   :0.7826                     
    # 3rd Qu.: 33.50   3rd Qu.:1.0000                     
    # Max.   :161.00   Max.   :1.0000                    
    
    ## before
    dd <- aml[aml$x == 'Maintained', ]
    fit <- survfit(Surv(time, status) ~ x, data = dd)
    plot(fit, conf.int = FALSE)
    
    ## during
    fit <- survfit(Surv(time, status) ~ x, data = aml, subset = x == 'Maintained')
    ## or
    fit <- survfit(Surv(time, status) ~ x, data = aml[aml$x == 'Maintained', ])
    plot(fit, conf.int = FALSE)
    
    ## after
    fit <- survfit(Surv(time, status) ~ x, data = aml)
    plot(fit, col = c('black','transparent'), conf.int = FALSE)
    

    where col is a vector of colors for each of the groups in order.

    All are identical:

    enter image description here