0

I have done multiple survival models using kaplan-Meir approach, each survival model was built by extracting sub set of data to different R Data table based on group column showed in the data table. I can plot each survival curve separate but I want to plot all these different models in one plot. what is the best way to do that.

 userid        lifespan_days              event        group
2                    4657                    1           A
4                    4658                    1           A
16                   1106                    1           A
50                    458                    1           A
51                   4393                    1           A
57                    305                    1           A

It would be great to do this in ggplot, By search I found following ggplot2 - plot multiple models on the same plot but I have problem of doing such scenario due to the nature of my data. For example userid is from multiple websites so userid=2 can exist under another group.

Lets say using above data.table I have created following:

a_time <- dt$lifespan_days
a_event <- dt$event
survival_model_a <- survfit(Surv(a_time, a_event) ~ 1)
plot(survival_model_a)

this will only plot one similary in the same plot I want to plot the model that I built for group b data which is in a different data.table / data.frame

Community
  • 1
  • 1
add-semi-colons
  • 18,094
  • 55
  • 145
  • 232
  • I'm note sure what exactly you're plotting, maybe you can include some code. If you don't use ggplot you can just call `lines()` instead of `plot()` for the subsequent plots. – amoebe May 06 '14 at 22:13
  • @amoebe I added a example just to make it clear. – add-semi-colons May 06 '14 at 22:21
  • If you provide your full dataset, or at least a much large sample that includes multiple groups, I'll show you a ggplot solution (unless someone else does first...). Upload your data somewhere and post a link in your question. – jlhoward May 07 '14 at 05:00

2 Answers2

2

Use lines() for subsequent plot calls like this:

b_time <- dtB$lifespan_days
b_event <- dtB$event
survival_model_b <- survfit(Surv(b_time, b_event) ~ 1)
lines(survival_model_b)

If you want to use ggplot2, there's two good answers in this question.

Community
  • 1
  • 1
amoebe
  • 4,857
  • 5
  • 37
  • 42
2

You can plot all models in one without subsetting using:

dt <- read.table(header=T, text="userid        lifespan_days              event        group
2                    4657                    1           A
4                    4658                    0           A
16                   1106                    1           B
50                    458                    1           B
51                   4393                    1           C
57                    305                    1           A")

library(survival)
a_time <- dt$lifespan_days
a_event <- dt$event
survival_model_a <- survfit(Surv(a_time, a_event) ~ dt$group)
plot(survival_model_a, col = rainbow(length(unique(dt$group))))
FFI
  • 392
  • 1
  • 10