当使用ggsurvplot()绘图时,希望通过分类变量对生存曲线进行着色,该分类变量是用于定义曲线(地层)的分类变量的超集。我已经阅读了所有的文档,并搜索了答案,但没有成功。下面提供了可重现的代码,尽管实际的ggsurvplot()函数调用是伪代码。
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)

我希望曲线的形状和含义与上面的生存图保持一致,但“鳞状细胞”和“小细胞”的颜色相同(并表示"group1"),另外两条曲线具有"group2“颜色。图例应该包含两个条目:"group1“和"group2”。
下面是一个示例代码,可以更好地解释我正在尝试做的事情(两者都不起作用)
# 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)编辑:建议使用palette函数,但以下代码会产生错误
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]但是,指定四种不同的颜色是可行的。
ggsurvplot(fit = surv, palette = c("red", "red1", "blue", "blue1"), data = veteran)https://stackoverflow.com/questions/44577613
复制相似问题