主要函数Usage
Surv(time, time2, event,
type=c('right', 'left', 'interval', 'counting', 'interval2', 'mstate'),
origin=0)
ggsurvplot(
fit,
data = NULL,
fun = NULL,
color = NULL,
palette = NULL,
linetype = 1,
conf.int = FALSE,
pval = FALSE,
pval.method = FALSE,
test.for.trend = FALSE,
surv.median.line = "none",
risk.table = FALSE,
cumevents = FALSE,
cumcensor = FALSE,
tables.height = 0.25,
group.by = NULL,
facet.by = NULL,
add.all = FALSE,
combine = FALSE,
ggtheme = theme_survminer(),
tables.theme = ggtheme,
...
)
# 加载包
library("survival")
library("survminer")
setwd("/test")
class(lung)
head(lung)
table(lung$sex)
#创建生存曲线
fit<- survfit(Surv(time, status) ~ sex, data = lung)
# Fit a stratified model
data2 <- lung
data2['elder'] <- data2$age >= 60
fit2 <-survfit(Surv(time, status) ~ sex+ strata(elder), data = data2)
# Fit Cox比例风险回归模型。
coxph(Surv(time, status) ~ sex, data = lung)
coxph(Surv(time, status) ~ sex + strata(elder), data = data2)
# 创建一个数据框,其中包含来自survfit结果的摘要。
res.sum <- surv_summary(fit,data=lung)
# 保存生存分析图文件
tiff('test_km.tiff', units="in", width=5, height=4, res=600, compression = 'lzw')
# Drawing Survival Curves Using ggplot2
ggsurvplot(fit,
surv.plot.height = 1,
# tables.height = 0.01,
# tables.width = 0.01,
xlab= "Survival time (days)",
conf.int = TRUE,
risk.table = TRUE,
risk.table.col = "strata",
linetype = "strata",
surv.median.line = "hv",
ggtheme = theme_bw(),
# palette = c("#E7B800", "#2E9FDF"),
pval = TRUE)
ggsurvplot(fit, conf.int = TRUE, risk.table.col = "strata",
# Change risk table color by groups ggtheme = theme_bw(),
# Change ggplot2 theme palette = c("#E7B800", "#2E9FDF"),
fun = "event")
# fun: "event" plots cumulative events (f(y) = 1-y),
# "cumhaz" plots the cumulative hazard function (f(y) = -log(y)),
# and "pct" for survival probability in percentage.
dev.off()
# 测试生存曲线差异
surv_diff <- survdiff(Surv(time, status) ~ sex, data = lung)