前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >生存分析新技能:小洁老师带你全代码实现生存曲线与风险表的完美融合

生存分析新技能:小洁老师带你全代码实现生存曲线与风险表的完美融合

作者头像
生信技能树
发布2024-07-05 14:52:30
1690
发布2024-07-05 14:52:30
举报
文章被收录于专栏:生信技能树

前情提要:作为三年前的马拉松授课学员,参加了每个月一次的老学员在线互动答疑,收获颇多,分享给大家:

  • 查找survminer是否自带保存生存图片的函数
  • 查看帮助文档中是否有file这个参数
  • 用帮助文档中的示例代码演示
  • 示例代码中并没有保存的代码,那就搜一搜:
  • 搜索的同时,尝试将图片单独保存
  • 成功保存图片,困扰我很久的问题小洁老师5分钟就解决了啦!你说小洁老师厉害不厉害?

为了让代码具有可重复性,保存图片也最好是用代码来实现,而不是用点鼠标的方式。最近有一个需求是将生存曲线和表格一起保存,尝试了经典的三段论、ggsave、图片数据类型转换、cowplot包的recordPlo函数都没能实现我的目的。恰好昨天是生信技能树的福利:每月1次的答疑时间。那就求助小洁老师来解决问题吧,小洁老师很快就找到了解决办法,实在是太厉害啦!下面是解答过程:

查找survminer是否自带保存生存图片的函数

代码语言:javascript
复制
library(survival)
library(survminer)
#> Loading required package: ggplot2
#> Loading required package: ggpubr
#> 
#> Attaching package: 'survminer'
#> The following object is masked from 'package:survival':
#> 
#>     myeloma
ls("package:survminer")
#>  [1] "%++%"                "arrange_ggsurvplots" "BMT"                
#>  [4] "BRCAOV.survInfo"     "ggadjustedcurves"    "ggcompetingrisks"   
#>  [7] "ggcoxdiagnostics"    "ggcoxfunctional"     "ggcoxzph"           
#> [10] "ggcumcensor"         "ggcumevents"         "ggflexsurvplot"     
#> [13] "ggforest"            "ggrisktable"         "ggsurvevents"       
#> [16] "ggsurvplot"          "ggsurvplot_add_all"  "ggsurvplot_combine" 
#> [19] "ggsurvplot_df"       "ggsurvplot_facet"    "ggsurvplot_group_by"
#> [22] "ggsurvplot_list"     "ggsurvtable"         "myeloma"            
#> [25] "pairwise_survdiff"   "surv_adjustedcurves" "surv_categorize"    
#> [28] "surv_cutpoint"       "surv_fit"            "surv_group_by"      
#> [31] "surv_median"         "surv_pvalue"         "surv_summary"       
#> [34] "theme_cleantable"    "theme_survminer"

查看帮助文档中是否有file这个参数

代码语言:javascript
复制
?ggsurvplot

用帮助文档中的示例代码演示

代码语言:javascript
复制
require("survival")
fit<- survfit(Surv(time, status) ~ sex, data = lung)

# Basic survival curves
ggsurvplot(fit, data = lung)
代码语言:javascript
复制

# Customized survival curves
ggsurvplot(fit, data = lung,
 surv.median.line = "hv", # Add medians survival

 # Change legends: title & labels
 legend.title = "Sex",
 legend.labs = c("Male", "Female"),
 # Add p-value and tervals
 pval = TRUE,

 conf.int = TRUE,
 # Add risk table
 risk.table = TRUE,
 tables.height = 0.2,
 tables.theme = theme_cleantable(),

 # Color palettes. Use custom color: c("#E7B800", "#2E9FDF"),
 # or brewer color (e.g.: "Dark2"), or ggsci color (e.g.: "jco")
 palette = c("#E7B800", "#2E9FDF"),
 ggtheme = theme_bw() # Change ggplot2 theme
)
代码语言:javascript
复制

# Change font size, style and color
#++++++++++++++++++++++++++++++++++++
## Not run: 
# Change font size, style and color at the same time
ggsurvplot(fit, data = lung,  main = "Survival curve",
   font.main = c(16, "bold", "darkblue"),
   font.x = c(14, "bold.italic", "red"),
   font.y = c(14, "bold.italic", "darkred"),
   font.tickslab = c(12, "plain", "darkgreen"))
代码语言:javascript
复制

## End(Not run)



#%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
# Example 2: Facet ggsurvplot() output by
# a combination of factors
#%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

# Fit (complexe) survival curves
#++++++++++++++++++++++++++++++++++++
## Not run: 
require("survival")
fit3 <- survfit( Surv(time, status) ~ sex + rx + adhere,
                data = colon )

# Visualize
#++++++++++++++++++++++++++++++++++++
ggsurv <- ggsurvplot(fit3, data = colon,
  fun = "cumhaz", conf.int = TRUE,
  risk.table = TRUE, risk.table.col="strata",
  ggtheme = theme_bw())

# Faceting survival curves
curv_facet <- ggsurv$plot + facet_grid(rx ~ adhere)
curv_facet
代码语言:javascript
复制

# Faceting risk tables:
# Generate risk table for each facet plot item
ggsurv$table + facet_grid(rx ~ adhere, scales = "free")+
 theme(legend.position = "none")
代码语言:javascript
复制

 # Generate risk table for each facet columns
tbl_facet <- ggsurv$table + facet_grid(.~ adhere, scales = "free")
tbl_facet + theme(legend.position = "none")
代码语言:javascript
复制

# Arrange faceted survival curves and risk tables
g2 <- ggplotGrob(curv_facet)
g3 <- ggplotGrob(tbl_facet)
min_ncol <- min(ncol(g2), ncol(g3))
g <- gridExtra::gtable_rbind(g2[, 1:min_ncol], g3[, 1:min_ncol], size="last")
g$widths <- grid::unit.pmax(g2$widths, g3$widths)
grid::grid.newpage()
grid::grid.draw(g)
代码语言:javascript
复制


## End(Not run)

#%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
# Example 3: CUSTOMIZED PVALUE
#%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
# Customized p-value
ggsurvplot(fit, data = lung, pval = TRUE)
代码语言:javascript
复制
ggsurvplot(fit, data = lung, pval = 0.03)
代码语言:javascript
复制
ggsurvplot(fit, data = lung, pval = "The hot p-value is: 0.031")

示例代码中并没有保存的代码,那就搜一搜:

搜索的同时,尝试将图片单独保存

代码语言:javascript
复制
require("survival")
fit<- survfit(Surv(time, status) ~ sex, data = lung)

# Customized survival curves
a = ggsurvplot(fit, data = lung,
 surv.median.line = "hv", # Add medians survival

 # Change legends: title & labels
 legend.title = "Sex",
 legend.labs = c("Male", "Female"),
 # Add p-value and tervals
 pval = TRUE,

 conf.int = TRUE,
 # Add risk table
 risk.table = TRUE,
 tables.height = 0.2,
 tables.theme = theme_cleantable(),

 # Color palettes. Use custom color: c("#E7B800", "#2E9FDF"),
 # or brewer color (e.g.: "Dark2"), or ggsci color (e.g.: "jco")
 palette = c("#E7B800", "#2E9FDF"),
 ggtheme = theme_bw() # Change ggplot2 theme
)

class(a$plot)
#> [1] "gg"     "ggplot"
class(a$table)
#> [1] "gg"     "ggplot"
代码语言:javascript
复制
#a$table和a$plot都是ggplot格式的图,那么是否将这两个图拼起啦?试一试:
代码语言:javascript
复制
library(patchwork)
a$plot / a$table
代码语言:javascript
复制
ggsave("sur.pdf")
#> Saving 7 x 5 in image

成功保存图片,困扰我很久的问题小洁老师5分钟就解决了啦!你说小洁老师厉害不厉害?

本文参与 腾讯云自媒体同步曝光计划,分享自微信公众号。
原始发表:2024-07-04,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 生信技能树 微信公众号,前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 查找survminer是否自带保存生存图片的函数
  • 查看帮助文档中是否有file这个参数
  • 用帮助文档中的示例代码演示
  • 示例代码中并没有保存的代码,那就搜一搜:
  • 搜索的同时,尝试将图片单独保存
  • 成功保存图片,困扰我很久的问题小洁老师5分钟就解决了啦!你说小洁老师厉害不厉害?
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档