在 `ggsurv` 图中(或在 `plot` 中)用不同类型区分每条线

Differentiating each Line with different type in `ggsurv` plots (or in `plot`)

我正在使用 Rstudio。我正在使用 GGally 包中的 ggsurv 函数为我的数据绘制 Kaplan-Meier 曲线(用于生存分析),来自教程 here。我正在使用它而不是 plot 因为 ggsurv 会自己处理图例。

如link所示,多条曲线以颜色区分。 我想根据线型进行区分。教程似乎没有任何选项。以下是我的命令:

surv1 <- survfit(Surv(DaysOfTreatment,Survived)~AgeOnFirstContactGroup)
print(ggsurv(surv1, lty.est = 3)+ ylim(0, 1))

lty.est=3(或 2)为所有行提供相同的虚线。我想要每行不同的虚线。使用 lty=type 给出错误:object 'type' not foundlty=type 可以在 ggplot 中工作,但 ggplot 不直接处理 survfit 地块。

请告诉我如何在 ggsurv 或简单 plot 中按线型区分曲线(尽管我更喜欢 ggsurv 因为它处理图例)

来自 ggsurv 的文档

lty.est: linetype of the survival curve(s). Vector length should be either 1 or equal to the number of strata.

因此,要为每个层获得不同的线型,请将 lty.est 设置为与您绘制的线数长度相同的向量,每个值对应不同的线型。

例如,使用survival包中的肺部数据

library(GGally)
library(survival)
data(lung)
surv1 <- survfit(Surv(time,status) ~ sex, data = lung)
ggsurv(surv1, lty.est=c(1,2), surv.col = 1)

给出如下情节

您也可以将 ggplot 主题或其他 ggplot 元素添加到绘图中。例如,我们可以使用 cowplot 主题来改进外观,如下所示

library(ggplot2)
library(cowplot)
ggsurv(surv1, lty.est=c(1,2), surv.col = 1) + theme_cowplot()

如果按线型区分后需要修改图例标签,可以这样

ggsurv(surv1, lty.est=c(1,2), surv.col = 1) +
  guides(colour = FALSE) +
  scale_linetype_discrete(name   = 'Sex', breaks = c(1,2), labels = c('Male', 'Female'))