Tinka
Tinka

Reputation: 13

Combine multiple (>2) survival curves (null models) in same plot

I am trying to combine multiple survfit objects on the same plot, using function ggsurvplot_combine from package survminer. When I made a list of 2 survfit objects, it perfectly works. But when I combine 3 survfit objects in different ways, I receive the error:

error in levels - ( tmp value = as.character(levels)): factor level 3 is duplicated

I've read similar posts on combining survivl plots (https://cran.r-project.org/web/packages/survminer/survminer.pdf, https://github.com/kassambara/survminer/issues/195, R plotting multiple survival curves in the same plot, https://rpkgs.datanovia.com/survminer/reference/ggsurvplot_combine.html) and on this specific error, for which solutions are been provided with using 'unique'. However, I do not even understand for which factor variable this error accounts. I do not have the right to share my data or figures, so I'll try to replicate it:

  1. Data:
  1. Null models:
  1. List null models:
  1. Combine >2 null models in one plot:

ggsurvplot_combine(list_that_works, data=data, conf.int=TRUE, fun="event", combine=TRUE) This gives the plot I'm looking for, but with 2 cumulative incidence curves.

ggsurvplot_combine(list_that_fails, data=data, conf.int=TRUE, fun="event", combine=TRUE) This gives error 'error in levels - ( tmp value = as.character(levels)): factor level 3 is duplicated'.

When I try combining 3 plots with using ggsurvplot(c(KM1,KM2,KM3), data=data, conf.int=TRUE, fun="event", combine=TRUE), it gives the error:

Error: Problem with mutate() 'column 'survsummary' survsummary = purrr::map2(grouped.d$fit, grouped.d$name, .surv_summary, data=data'. x $ operator is invlid for atomic vectors.

Any help is highly appreciated! Also another way to combine surv fits is very welcome! My best bet is that it has something to do with the 'list' function that only contains of two arguments: list(PFS=, OS=)

Upvotes: 0

Views: 1839

Answers (1)

Tinka
Tinka

Reputation: 13

I fixed it! Instead of removing the post, I'll share my solution, it may be of help for others:

I made a list of the formulas instead of the null models, so:

formulas <- list(
KM1 = Surv(time1, endpoint1)~1, 
KM2 = Surv(time2, endpoint2)~1, 
KM3 = Surv(time3, endpoint3)~1)

I made a null model of the 3 formulas at once:

fit <- surv_fit(formulas, data=data)
    

Then I made a plot with this survival fit:

ggsurvplot_combine(fit, data=data)
    

Upvotes: 1

Related Questions