前情提要:作为三年前的马拉松授课学员,参加了每个月一次的老学员在线互动答疑,收获颇多,分享给大家:
为了让代码具有可重复性,保存图片也最好是用代码来实现,而不是用点鼠标的方式。最近有一个需求是将生存曲线和表格一起保存,尝试了经典的三段论、ggsave、图片数据类型转换、cowplot包的recordPlo函数都没能实现我的目的。恰好昨天是生信技能树的福利:每月1次的答疑时间。那就求助小洁老师来解决问题吧,小洁老师很快就找到了解决办法,实在是太厉害啦!下面是解答过程:
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"
?ggsurvplot
require("survival")
fit<- survfit(Surv(time, status) ~ sex, data = lung)
# Basic survival curves
ggsurvplot(fit, data = lung)
# 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
)
# 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"))
## 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
# Faceting risk tables:
# Generate risk table for each facet plot item
ggsurv$table + facet_grid(rx ~ adhere, scales = "free")+
theme(legend.position = "none")
# Generate risk table for each facet columns
tbl_facet <- ggsurv$table + facet_grid(.~ adhere, scales = "free")
tbl_facet + theme(legend.position = "none")
# 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)
## End(Not run)
#%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
# Example 3: CUSTOMIZED PVALUE
#%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
# Customized p-value
ggsurvplot(fit, data = lung, pval = TRUE)
ggsurvplot(fit, data = lung, pval = 0.03)
ggsurvplot(fit, data = lung, pval = "The hot p-value is: 0.031")
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"
#a$table和a$plot都是ggplot格式的图,那么是否将这两个图拼起啦?试一试:
library(patchwork)
a$plot / a$table
ggsave("sur.pdf")
#> Saving 7 x 5 in image