When plotting with ggsurvplot()
would like to color the survival curves by a categorical variable that is a superset of the categorical variables used to define the curves (the strata). I have read all the documentation and searched for the answer without success. Reproducible code is provided below, although the actual ggsurvplot()
function call is pseudo-code.
library(survival)
library(survminer)
veteran <- veteran
veteran$group <- with(veteran,
ifelse(
celltype == "squamous" | celltype == "smallcell",
"group1", "group2"
)
)
# code used to generate the accompanying plot
surv <- survfit(Surv(time, status) ~ celltype, data = veteran)
ggsurvplot(fit = surv, data = veteran)
I would like the shape and meaning of the curves to remain as in the survival graph above but have the color for "squamous" and "smallcell" to be the same (and represent "group1") and for the other two curves to have the "group2" color. The legend should contain two entries: "group1" and "group2".
Below is an example code that may better explain what I am attempting to do (neither works)
# pseudo-code, version1: without the grouping data in the survfit object
surv <- survfit(Surv(time, status) ~ celltype, data = veteran)
ggsurvplot(fit = surv, color = veteran$group,
legend.labs = levels(factor(veteran$group)), data = veteran)
# pseudo-code, version2: with the grouping data in the survfit
surv <- survfit(Surv(time, status) ~ celltype + group, data = veteran)
ggsurvplot(fit = surv, color = group,
legend.labs = levels(factor(veteran$group)), data = veteran)
EDIT: Use of the palette
function has been suggested but the following code produces and error
ggsurvplot(fit = surv, palette = c("red", "red", "blue", "blue"), data = veteran)
#Error in names(scurve_cols) <- legend.labs :
#'names' attribute [4] must be the same length as the vector [2]
However, specifying four distinct colors works.
ggsurvplot(fit = surv, palette = c("red", "red1", "blue", "blue1"), data = veteran)
Answering this for completeness. Solution provided by user Henrik in the comments. Possibly only works with the newest version of survminer [0.4.0].
library(survival)
library(survminer)
veteran <- veteran
#coding an external 'superset' variable is unnecessary
surv <- survfit(Surv(time, status) ~ celltype, data = veteran)
ggsurvplot(fit = surv, palette = c("red", "red", "blue", "blue"), data = veteran)