Search code examples
rsurvival-analysis

R Survival Analysis: Show total Patients (n) for Each Group


Edit: Adding in a public data set you can use.

Sorry if this is answered somewhere... I can't find examples or explanations of this for the life of me here, google or youtube.

I am trying to find a way to show total patients (n) for each group.

If using the public data source:

>data(leukemia)
>leukemia

      time  status      x
      9       1     Maintained
      13      1     Maintained
      13      0     Maintained
      .       .         .
      .       .         .
      22   43      1 Nonmaintained
      23   45      1 Nonmaintained

And a simple script to preform the analysis:

library(survival)

group <- leukemia$x

surv=Surv(leukemia$time,leukemia$status)
surv.data.group <- survfit(surv~group,type="kaplan-meier",conf.type="none")

Followed by a simple plot with a legend to show which color each group is:

plot(surv.data.group, col=c(1,2), xlab='Time (Months)', 
xmax=120,mark.time=FALSE, main="Survival Curve Example")

legend("topright", legend=unique(group), col=(1:2), lwd=0.5, bty='n')

What I want as the outcome is to show the total number (n) of patients for each group either in the legend or on the plot itself. For example, the legend might look like:

Maintained (n=11)
Nonmaintained (n=12)

Solution

  • Here is a workaround if you have two groups:

    group1 <- paste("Group 1 ", "(n=", table(data$group)[1], ")", sep="")
    group2 <- paste("Group 2 ", "(n=", table(data$group)[2], ")", sep="")
    
    group <- data$group
    legend("topright", legend= c(group1, group2), col=(1:2), lwd=1.0, bty='n')
    

    With table() you get the sum of your groups and then index with [ ]

    enter image description here