3

From ggsurvplot doc, I can facet a ggplot object as below.

#%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
# Facet ggsurvplot() output by
# a combination of factors
#%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

# Fit (complexe) survival curves
#++++++++++++++++++++++++++++++++++++
require("survival")
fit3 <- survfit( Surv(time, status) ~ sex + rx + adhere,
                 data = colon )

# Visualize: plot survival curves by sex and facet by rx and adhere
#++++++++++++++++++++++++++++++++++++
ggsurv <- ggsurvplot(fit3, conf.int = TRUE)
ggsurv$plot +theme_bw() + facet_grid(rx ~ adhere)

enter image description here

Now if I were to use survreg to fit Weibull model, I have to use ggsurvplot_df as below. The challenge I have is the inconsistency between ggsurvplot and ggsurvplot_df despite the fact that both are built on top of ggplot.

Is there a way to facet ggsurvplot_df object as how ggsurvplot object is?

# Weibull model
wbmod <- survreg(Surv(time, status) ~ sex + rx + adhere,
                 data = colon)
summary(colon)
# Imaginary patients
newdat <- expand.grid(
  rx = levels(colon$rx),
  adhere = unique(colon$adhere),
  sex = unique(colon$sex))
newdat

# Compute survival curves
surv <- seq(.99, .01, by = -.01)
t <- predict(wbmod, type = 'quantile', p = 1-surv,
             newdata = newdat)

# How many rows and columns does t have?
dim(t)


# Use cbind() to combine the information in newdat with t
surv_wbmod_wide <- cbind(newdat, t)

# Use melt() to bring the data.frame to long format
library(reshape2)
surv_wbmod <- melt(surv_wbmod_wide, id.vars = c('rx','adhere', 'sex'), variable.name = 'surv_id', value.name = 'time')
dim(surv_wbmod)
# Use surv_wbmod$surv_id to add the correct survival probabilities surv
surv_wbmod$surv <- surv[as.numeric(surv_wbmod$surv_id)]


# Add columns upper, lower, std.err, and strata to the data.frame
surv_wbmod[, c("upper", "lower", "std.err", "strata")] <- NA

# Take a look at the structure of the object
str(surv_wbmod)


# Plot the survival curves

ggsurvplot_df(surv_wbmod, surv.geom =  geom_line, linetype = 'rx', color = 'adhere', legend.title = NULL)

enter image description here

Afiq Johari
  • 1,372
  • 1
  • 15
  • 28

1 Answers1

1

Do you mean something like this? If this is want you need then ggsurvplot_df was not an object. Therfore no faceting was possible?!

# Plot the survival curves

ggsurvplot_df <- ggsurvplot(surv_wbmod, surv.geom =  geom_line, linetype = 'rx', color = 'adhere', legend.title = NULL) 
ggsurvplot_df + theme_bw() + facet_grid(rx ~ adhere)

enter image description here

TarJae
  • 72,363
  • 6
  • 19
  • 66