美文网首页生信绘图
Survival生存分析封装函数-没达到预期效果mark

Survival生存分析封装函数-没达到预期效果mark

作者: 医科研 | 来源:发表于2020-02-18 17:36 被阅读0次

##########################################################################

Group_by计算在各个不同分组中的函数

最佳分割

 surv_group_plot<-function(i,group,data=mydata,width=9.4,height=5,col="nejm"){
  ## 选择最佳切割点
  res.cut <- surv_cutpoint(data, time = "OS_MONTHS", event = "OS_STATUS",
                           variables = i)
  plot(res.cut, i, palette = "npg")
  cut<-summary(res.cut)$cutpoint
  cut
  gene<-as.numeric(as.matrix(data[,i]))
  data$grouplist<-factor(ifelse(gene>cut,"high","low"))
  ## 绘图
  splots <- list()
  fit <- data %>%
    as.data.frame() %>% 
    surv_group_by(group) %>% 
    surv_fit(Surv(OS_MONTHS, OS_STATUS) ~ grouplist, data = .)
  ### 
  ggsurv.list <- ggsurvplot(fit,  
                            xlab = "Time,mo",
                            ylab="Proportion Alive",
                            pval = T,
                            conf.int = F,##置信带
                            risk.table = T,
                            legend.title = i,
                            #legend.labs = c("high","low"),##
                            palette = col)
  res<-arrange_ggsurvplots(ggsurv.list, print = F,
                           ncol = 2,nrow =1, risk.table.height = 0.25)
  ggsave(filename = paste(i,group,"group","bestsurv_.pdf",sep="_"), res,width=width,height = height)
}
### test group function
surv_group_plot("x","TNBC",col="nejm",data = mydata,width = 9.6,height = 5)

median cut的中位分组函数

##########################################################################

Group_by计算在各个不同分组中的函数

mydata<-METABRIC_data_surv
mydata$TNBC<-factor(mydata$TNBC)
surv_group_med_plot<-function(i,group,data=mydata,width=9.4,height=5,col="nejm"){
  ## 选择中位值作为切割点
  gene<-as.numeric(data[[i]])
  data$grouplist<-factor(if_else(gene>median(gene),"high","low"))
  ## 绘图
  fit <- data %>%
    as.data.frame() %>% 
    surv_group_by(group) %>% 
    surv_fit(Surv(OS_MONTHS, OS_STATUS) ~ grouplist, data = .)
  ### 
  ggsurv.list <- ggsurvplot(fit, 
                            xlab = "Time,mo",
                            ylab="Proportion Alive",
                            pval = T,
                            conf.int = F,##置信带
                            risk.table = T,
                            legend.title = i,
                            #legend.labs = c("high","low"),erro
                            palette = col)
  res<-arrange_ggsurvplots(ggsurv.list, print = F,
                           ncol = 2,nrow =1, risk.table.height = 0.25)
  ggsave(filename = paste(i,group,"metabric_group","median_surv_.pdf",sep="_"), res,width=width,height = height)
}
### test group function
mydata$ER_STATUS<-factor(mydata$ER_STATUS)
colnames(mydata)[1:20]
surv_group_med_plot("x","ER_STATUS",data = mydata,width = 10,height = 5,col="nejm")

相关文章

网友评论

    本文标题:Survival生存分析封装函数-没达到预期效果mark

    本文链接:https://www.haomeiwen.com/subject/xuxnfhtx.html