-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path绘制森林图
61 lines (55 loc) · 2.12 KB
/
绘制森林图
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
#install.packages("survival")
#install.packages("forestplot")
rm(list = ls())
library(survival)
library(forestplot)
setwd("H:\\BaiduNetdiskDownload\\pan-cancer\\panCancer\\09.cox") #设置工作目录
rt=read.table("expTime.txt",header=T,sep="\t",check.names=F,row.names=1) #读取输入文件
rt$futime=rt$futime/365
gene=colnames(rt)[3]
#对肿瘤类型进行循环
outTab=data.frame()
for(i in levels(rt[,"CancerType"])){
rt1=rt[(rt[,"CancerType"]==i),]
cox=coxph(Surv(futime, fustat) ~ rt1[,gene], data = rt1)
coxSummary = summary(cox)
coxP=coxSummary$coefficients[,"Pr(>|z|)"]
outTab=rbind(outTab,
cbind(cancer=i,
HR=coxSummary$conf.int[,"exp(coef)"],
HR.95L=coxSummary$conf.int[,"lower .95"],
HR.95H=coxSummary$conf.int[,"upper .95"],
pvalue=coxP) )
}
write.table(outTab,file="cox.result.txt",sep="\t",row.names=F,quote=F) #输出基因和p值表格文件
############绘制森林图函数############
bioForest=function(coxFile=null,forestFile=null,forestCol=null){
#读取输入文件
rt=read.table("cox.result.txt",header=T,sep="\t",row.names=1,check.names=F)
data=(rt)
HR=data[,1:3]
hr=sprintf("%.3f",HR[,"HR"])
hrLow=sprintf("%.3f",HR[,"HR.95L"])
hrHigh=sprintf("%.3f",HR[,"HR.95H"])
pVal=data[,"pvalue"]
pVal=ifelse(pVal<0.001, "<0.001", sprintf("%.3f", pVal))
# clrs <- fpColors(box=forestCol,line="darkblue", summary="royalblue") #定义颜色
clrs <- fpColors(box="red",line="darkblue", summary="royalblue")
tabletext <-
list(c(NA, rownames(HR)),
append("pvalue", pVal),
append("Hazard ratio",paste0(hr,"(",hrLow,"-",hrHigh,")")) ) #定义图片文字
pdf(file="forest1.pdf",width = 9,height = 6,onefile = FALSE)
forestplot(tabletext,
rbind(rep(NA, 3), HR),
col=clrs,
graphwidth=unit(50, "mm"),
xlog=T,
lwd.ci=4,
boxsize=0.6,
xlab="Hazard ratio",
txt_gp=fpTxtGp(ticks=gpar(cex=1.1),xlab=gpar(cex = 1.25))
)
dev.off()
}
############绘制森林图函数############