1

I would like to have full control on the colors used to display group graphs using ggsurv. You'll find below a toy example to reproduce what I've seen (mostly taken from there):

require(data.table)

# Function to create synthetic survival data
simulWeib <- function(N, lambda, rho, beta, rateC)
{
  # covariate --> N Bernoulli trials
  x <- sample(x=c(0, 1), size=N, replace=TRUE, prob=c(0.5, 0.5))

  # Weibull latent event times
  v <- runif(n=N)
  Tlat <- (- log(v) / (lambda * exp(x * beta)))^(1 / rho)

  # censoring times
  C <- rexp(n=N, rate=rateC)

  # follow-up times and event indicators
  time <- pmin(Tlat, C)
  status <- as.numeric(Tlat <= C)

  # data set
  data.frame(id=1:N,
             time=time,
             status=status,
             x=x)
}

set.seed(1234)
nbGroups <- 7
dat <- list()
for(k in 1:nbGroups)
{
  dat.onegp <- simulWeib(N=10, lambda=0.01, rho=1, beta=-0.6, rateC=0.001)
  # fit <- coxph(Surv(time, status) ~ x, data=dat.onegp)
  dat.onegp <- mutate(dat.onegp, Group = paste0("G",k))
  dat[[k]] <- dat.onegp
}

dat.df <- rbindlist(dat)
dat.df.survCurv <- survfit( Surv(dat.df$time, dat.df$status) ~ dat.df$Group )
# Vector with colors to be used
cols = colorRampPalette(brewer.pal(9, "Set1"))(nbGroups)

ggsurv(dat.df.survCurv, size.est = 1 ) +
  guides(linetype = FALSE) +
  scale_colour_manual(name = "Exp. groups",  breaks = sort(dat.df$Group), values = cols)

Running this twice will give two different sets of color-group assignment, and I don't want that. I need groups to always be displayed with the same color, for consistency with other graphs in a report.

NB: I have found out that the order in which colors are displayed is linked with the survival data, but I can't figure out how to force color assignment.

Any help appreciated!

Community
  • 1
  • 1
xvrtzn
  • 65
  • 3

1 Answers1

0

Found it in this post ! The solution is to use limits instead of breaks

ggsurv(dat.df.survCurv, size.est = 1 ) +
  guides(linetype = FALSE) +
  scale_colour_manual(name = "Exp. groups",  limits = sort(dat.df$Group), values = cols)
Community
  • 1
  • 1
xvrtzn
  • 65
  • 3