I am trying to create survival plots using the survminer package in R with data split by genetic mutations and include a summary line representing all individuals across facets. The survival analysis and plotting work fine when not using add.all in ggsurvplot for multiple groups (split by sex and mutation type). However, when attempting to add a combined survival curve across facets using add.all = TRUE, it doesn't show as expected. Below is a minimal reproducible example using the lung dataset:
example:
library(survival) ; library(survminer)
set.seed(102030)
lung <- lung %>% mutate( mut = sample(c("mutA","mutB"), nrow(lung), replace = T, prob = c(0.5,0.5))) # create 2 groups for demo
# Basic survival curves
fit1 <- survfit(Surv(time, status) ~ 1 , data = lung) # general overall survival
ggsurvplot(fit1 , data = lung)
# Basic survival curves with 2 groups for sex
fit2 <- survfit(Surv(time, status) ~ sex , data = lung)
ggsurvplot(fit2 , data = lung)
# Survival curves with 2 variables and 2 groups per variable (problematic part)
fit3 <- survfit(Surv(time, status) ~ sex + mut, data = lung)
ggsurvplot(fit3 , data = lung, facet.by = "mut") # example one facet by mut. Works as intended!
ggsurvplot(fit3 , data = lung,
facet.by = "mut",
add.all = T) # supposedly should have added a third groups (i.e. a line that represents the number of individuals in each facet)
# I also tried this approach, but this is not faceting the plot either.
ggsurvplot_combine(
list(fit1, fit3),
list(lung, lung),
facet.by = "mut",
pval = TRUE,
risk.table = TRUE,
conf.int = F,
palette = "jco"
)
I expect the add.all = TRUE parameter to add a summary curve across the facets, but this does not occur. How can I correct this issue or is there a limitation in the function?
It might be that I need to use ggsurvplot_group_by
or ggsurvplot_facet
differently.
Since facetting (or grouping) and adding the total group (add.all=TRUE) isn't implemented in survminer (yet), you could plot the curves separately, and then use arrange_ggsurvplots
to combine them.
pA <- ggsurvplot(fit3, title="Mutant A",
add.all=TRUE,
data = subset(lung, subset=mut=="mutA")); pA
pB <- ggsurvplot(fit3, add.all=TRUE, title="Mutant B",
data = subset(lung, subset=mut=="mutB"))
pAB <- arrange_ggsurvplots(list(pA, pB))
ggsave("pAB.png", pAB, width=10, height=5)