0

我想从包lung中的数据集生成 Kaplan-Meier 图survival,按性别和 ECOG 分数分层。我希望曲线具有以下样式:

Men, ph.ecog = 0: solid, red
Men, ph.ecog = 1: solid, blue
Men, ph.ecog = 2: solid, green
Women, ph.ecog = 0: dashed, red
Women, ph.ecog = 1: dashed, blue
Women, ph.ecog = 2: dashed, green

我想在图下方显示风险表。在风险表图例中显示线条样式(颜色和类型)来代替长图例标签会特别好。

我根据生存曲线中多个组的不同颜色类型和线型的答案尝试了此代码:

# drop the PH.ECOG level with only 1 observation
lung$ph.ecog[lung$ph.ecog == 3] <- NA

fit <- survfit(Surv(time, status) ~ sex + ph.ecog, data = lung)

p <- ggsurvplot(fit, 
           data= lung, 
           title="Survival Probability by Sex and ECOG", 
           xlab = "Days", 
           ylab = "Survival Probability",
           conf.int = FALSE, 
           pval=TRUE, 
           pval.coord = c(0,0.25),
           risk.table = TRUE,
           risk.table.pos="out",
           risk.table.col="black",
           risk.table.y.text.col=TRUE,
           risk.table.height = 0.2,
           tables.theme = theme_cleantable() + theme(aspect.ratio = 0.2),
           tables.y.text = FALSE,
           break.time.by=180,
           legend.labs = c(
                 "Male, asymptomatic",
                 "Male, symptomatic but completely ambulatory",
                 "Male, in bed <50% of the day",
                 "Female, asymptomatic", 
                 "Female, symptomatic but completely ambulatory", 
                 "Female, in bed <50% of the day"
                  ),
           censor=TRUE,
           legend.title="",
           linetype = "strata",
           ggtheme = theme_survminer() + theme(plot.title = element_text(hjust = 0.5, face = "bold"), aspect.ratio = 1)
)

cols <-  rep(c("red", "blue", "green"), 2)
names(cols) <- paste("Sex", rep(c("Male", "Female"), each = 3), "ECOG", 0:2)

lines <-  rep(c("solid", "dashed"), each = 3)
names(lines) <- paste("Sex", rep(c("Male", "Female"), each = 3), "ECOG", 0:2)
      
p$plot <- p$plot +
  scale_linetype_manual(values = lines) +
  scale_color_manual(values = cols)

p$plot / p$table + patchwork::plot_layout(ncol = 1, heights = c(3, 1)) +
  theme(axis.text.y = element_text(color = rev(cols)))

print(p)

结果绘图区域为空,并返回警告消息:

Removed 225 row(s) containing missing values (geom_path).
Removed 63 rows containing missing values (geom_point).
Removed 225 row(s) containing missing values (geom_path).
Removed 63 rows containing missing values (geom_point).

预先感谢您的帮助。

4

0 回答 0