几次寻求度娘无果,而且我认为这么想的绝对不仅仅只有我一个,肯定有什么大神受不了然后出手。所以选择用谷歌引擎搜英文的,看看有什么好用的包没。
长话短说(之前写的内容因为用的VPN,发布的时候简书出问题全没了~~),介绍找到的survminer
包。
安装和加载
install.packages("survminer")
if(!require(devtools)) install.packages("devtools")devtools::install_github("kassambara/survminer", build_vignettes = FALSE)
library("survminer")
画生存曲线
构建生存模型
require("survival")
fit <- survfit(Surv(time, status) ~ sex, data = lung)
画基本图:
ggsurvplot(fit, data = lung)
自定义生存曲线
ggsurvplot( fit,
data = lung,
size = 1, # change line size
palette = c("#E7B800", "#2E9FDF"),# custom color palettes
conf.int = TRUE, # Add confidence interval
pval = TRUE, # Add p-value
risk.table = TRUE, # Add risk table
risk.table.col = "strata",# Risk table color by groups
legend.labs = c("Male", "Female"), # Change legend labels
risk.table.height = 0.25, # Useful to change when you have multiple groups
ggtheme = theme_bw() # Change ggplot2 theme)
是不是更美了??
看看更详细的参数列表:
ggsurv <- ggsurvplot(
fit, # survfit object with calculated statistics.
data = lung, # data used to fit survival curves.
risk.table = TRUE, # show risk
table. pval = TRUE, # show p-value of log-rank test.
conf.int = TRUE, # show confidence intervals for
# point estimates of survival
curves. palette = c("#E7B800", "#2E9FDF"),
xlim = c(0,500), # present narrower X axis, but not affect
# survival estimates.
xlab = "Time in days", # customize X axis label.
break.time.by = 100, # break X axis in time intervals by 500.
ggtheme = theme_light(), # customize plot and risk table with a theme.
risk.table.y.text.col = T,# colour risk table text annotations.
risk.table.height = 0.25, # the height of the risk table
risk.table.y.text = FALSE,# show bars instead of names in text annotations
# in legend of risk table.
ncensor.plot = TRUE, # plot the number of censored subjects at time t
ncensor.plot.height = 0.25,
conf.int.style = "step", # customize style of confidence intervals
surv.median.line = "hv", # add the median survival pointer.
legend.labs = c("Male", "Female") # change legend labels.
)
ggsurv
还可以调整图中的字体和颜色喔~
如果这有帮助到你,写博文的时候别忘记向我的专题投稿哈~