r - How to assign colors to groups in survival graphs -
i have full control on colors used display group graphs using ggsurv
. you'll find below toy example reproduce i've seen (mostly taken there):
require(data.table) # function 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 , 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 colors 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 twice give 2 different sets of color-group assignment, , don't want that. need groups displayed same color, consistency other graphs in report.
nb: have found out order in colors displayed linked survival data, can't figure out how force color assignment.
any appreciated!
found in this post ! solution 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)
Comments
Post a Comment