add-semi-colons
add-semi-colons

Reputation: 18800

R draw multiple models in one plot

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

Upvotes: 0

Views: 2175

Answers (2)

amoebe
amoebe

Reputation: 4982

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.

Upvotes: 2

FFI
FFI

Reputation: 402

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))))

Upvotes: 2

Related Questions