我们在既往的文章《R语言绘制复杂抽样设计数据cox回归生存曲线(Kaplan-Meier)》中介绍了怎么使用jskm包的svykm函数绘制复杂抽样设计数据cox回归生存曲线(Kaplan-Meier),但是有粉丝觉得讲得不够详细,希望讲得详细一点,今天我们继续来介绍一下。
先导入我们的R包和数据
library(jskm)
library(survey)
library(jskm)
pbc<-read.csv("E:/r/test/pbc.csv",sep=',',header=TRUE)
这是一个原发性胆道胆管炎数据,公众号回复:胆管炎数据,可以获得数据,
数据我们解释几个等下要用到的变量,age:年龄,trt:治疗方案:1D-青霉烯,2安慰剂,edema:水肿, status: 结局变量0/1/2表示审查、移植、死亡。
咱们先来一波小操作,生成一个预测值,等下好操作,不喜欢可以跳过这部分,对后面的操作没影响.
pbc$randomized <- with(pbc, !is.na(trt) & trt>0)
biasmodel <- glm(randomized~age*edema,data=pbc)
pbc$randprob <- fitted(biasmodel)
生成预测值randprob后我们就可以正式分析了,我们先生成一个调查数据
dpbc <- svydesign(id=~1, prob=~randprob, strata=~edema, data=subset(pbc,randomized))
直接使用svykm函数生成预测值,这里也要生存可信区间
s1 <- svykm(Surv(time,status>0)~sex, design=dpbc,se=T)
现在我们直接使用svyjskm函数来进行绘图
svyjskm(s1)
更改X轴,Y轴标签和标题
svyjskm(s1,xlabs = "时间",ylabs = "生存率",main = "时间与生存率关系图")
对图例进行修改
svyjskm(s1,xlabs = "时间",ylabs = "生存率",ystrataname = "性别",ystratalabs=c("男","女"))
对X轴的范围进行限制,对Y轴也是一样的
svyjskm(s1,xlabs = "时间",ylabs = "生存率",ystrataname = "性别",
ystratalabs=c("男","女"),xlims=c(0,3000))
把Y轴改成以百分比显示
svyjskm(s1,xlabs = "时间",ylabs = "生存率",ystrataname = "性别",
ystratalabs=c("男","女"),surv.scale="percent")
更改显示的时间轴点数,默认是7个点
svyjskm(s1,xlabs = "时间",ylabs = "生存率",ystrataname = "性别",
ystratalabs=c("男","女"),timeby=700)
在图中生成P值
svyjskm(s1,xlabs = "时间",ylabs = "生存率",ystrataname = "性别",
ystratalabs=c("男","女"),timeby=700,pval=T)
调整文中P值的位置和字体的大小
svyjskm(s1,xlabs = "时间",ylabs = "生存率",ystrataname = "性别",
ystratalabs=c("男","女"),timeby=700,pval=T,pval.coord=c(1000,0.4),pval.size=8)
添加Log-rank进P值后面
svyjskm(s1,xlabs = "时间",ylabs = "生存率",ystrataname = "性别",
ystratalabs=c("男","女"),timeby=700,pval=T,
pval.coord=c(1000,0.4),pval.testname=T)
调整图例位置
svyjskm(s1,xlabs = "时间",ylabs = "生存率",ystrataname = "性别",
ystratalabs=c("男","女"),timeby=700,pval=T,
pval.coord=c(1000,0.4),pval.testname=T,legendposition=c(0.6,0.8))
不显示图例
svyjskm(s1,xlabs = "时间",ylabs = "生存率",ystrataname = "性别",
ystratalabs=c("男","女"),timeby=700,pval=T,
pval.coord=c(1000,0.4),pval.testname=T,legendposition=c(0.6,0.8),legend=F)
更换色彩风格,默认是Set1
svyjskm(s1,xlabs = "时间",ylabs = "生存率",ystrataname = "性别",
ystratalabs=c("男","女"),timeby=700,pval=T,
pval.coord=c(1000,0.4),pval.testname=T,linecols="Set2")
dashed设置虚线,好像只能设置一条
svyjskm(s1,xlabs = "时间",ylabs = "生存率",ystrataname = "性别",
ystratalabs=c("男","女"),timeby=700,pval=T,
pval.coord=c(1000,0.4),pval.testname=T,linecols="Set2",dashed=T)
显示累积发生率函数
svyjskm(s1,xlabs = "时间",ylabs = "生存率",ystrataname = "性别",
ystratalabs=c("男","女"),timeby=700,pval=T,
pval.coord=c(1000,0.4),pval.testname=T,linecols="Set2",cumhaz=T)
调用ggpubr::ggarrange函数来形成表格,给了我不少启发
svyjskm(s1,xlabs = "时间",ylabs = "生存率",ystrataname = "性别",
ystratalabs=c("男","女"),timeby=700,pval=T,
pval.coord=c(1000,0.4),pval.testname=T,linecols="Set2",table=T)
修改表格下面的副标题名字
svyjskm(s1,xlabs = "时间",ylabs = "生存率",ystrataname = "性别",
ystratalabs=c("男","女"),timeby=700,pval=T,
pval.coord=c(1000,0.4),pval.testname=,
linecols="Set2",table=T,label.nrisk="生存人数")
设置Numbers at risk字体大小,默认是10
svyjskm(s1,xlabs = "时间",ylabs = "生存率",ystrataname = "性别",
ystratalabs=c("男","女"),timeby=700,pval=T,
pval.coord=c(1000,0.4),pval.testname=T,
linecols="Set2",table=T,label.nrisk="生存人数",size.label.nrisk=12)
给曲线加上百分比显示
svyjskm(s1,xlabs = "时间",ylabs = "生存率",ystrataname = "性别",
ystratalabs=c("男","女"),timeby=700,pval=T,
pval.coord=c(1000,0.4),pval.testname=T,
linecols="Set2",table=T,label.nrisk="生存人数",size.label.nrisk=12,showpercent = T)
cut.landmark是设置一个新的地标
svyjskm(s1,xlabs = "时间",ylabs = "生存率",ystrataname = "性别",
ystratalabs=c("男","女"),timeby=700,pval=T,
pval.coord=c(1000,0.4),pval.testname=T,
linecols="Set2",table=T,label.nrisk="生存人数",size.label.nrisk=12,showpercent = T,cut.landmark=2100)