将字符串数据包装在ggplot2的surv_miner的风险表中 [英] String wrapping text data in risk table in surv_miner for ggplot2

查看:88
本文介绍了将字符串数据包装在ggplot2的surv_miner的风险表中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

对于在RStudio中用surv_miner构造的Kaplan Meier曲线,我具有以下风险表:

由于我将您的代码(以年为基准)与具有不同时间范围(天或月)的对象一起使用,因此无法使用该限制.

I have the following risk table for a Kaplan Meier curve constructed with surv_miner in RStudio:

Kaplan Meier curve

I would like to string wrap the text in the risk table so that the number is on top, and the percentage (in parenthesis) is on another line below the number, so that they all fit in the table. I tried using the stringr package, but I don't quite know how to incorporate that into the theme. Any help would be appreciated. I've posted just my code, since it is a bit tricky to add the actual survival data. However, if the actual data is needed, I will post some dummy data. Thank you in advance.

require("survival")
require("ggplot2")
require("survminer")

fit <- survfit(Surv(Time, Survival) ~ Gender_bin, data = d)
ggsurvplot(fit,
           data = d, 
           pval = TRUE,
           legend.title = "Gender", legend.labs = c("Male", "Female"),
           xlab="Time (years)", ylab="Survival", 
           ggtheme = 
             theme_bw() + 
             theme(plot.title = element_text(hjust = 0.5, face = "bold")) +
             theme(axis.text=element_text(size=14), axis.title=element_text(size=16), axis.title.x=element_blank()) +
             theme(legend.background = element_rect(fill="gray95", size=.5), 
                   legend.text=element_text(size=14), legend.title=element_text(size=14)) +
             theme(aspect.ratio=1),
           xlim = c(0,10), 
           break.time.by = 2.5, 
           legend = c(0.9,0.8),
           risk.table = "abs_pct",
           fontsize = 3.5,
           tables.theme = theme_bw() + theme(aspect.ratio = 0.14) + 
             theme(axis.text=element_text(size=14), axis.title=element_text(size=14)) +
             theme(axis.title.y=element_blank()) +
             theme(panel.grid.major = element_blank(), panel.grid.minor = element_blank()))

surv_median(fit)$median

解决方案

Got some hacking to apparently work. Decided to use the code you provided with the dataset that is used in the example section of ggsurvplot. Searched for a section that printed the numbers and percentages in parentheses. Looked at:

? ggsurvtable   # the help page
ggsurvtable    # the code
# noticed a call to .plot_survtable(...) assumed it was not exposed for viewing
getAnywhere(.plot_survtable)
# found a section that looked like it would do the work:
else if (survtable == "risk.table") {
    pct.risk <- abs_pct.risk <- n.risk <- NULL
    llabels <- switch(risk.table.type, percentage = round(survsummary$n.risk * 
        100/survsummary$strata_size), abs_pct = paste0(survsummary$n.risk, 
        " (", survsummary$pct.risk, ")"), nrisk_cumcensor = paste0(survsummary$n.risk, 
        " (", survsummary$cum.n.censor, ")"), nrisk_cumevents = paste0(survsummary$n.risk, 
        " (", survsummary$cum.n.event, ")"), survsummary$n.risk)
    survsummary$llabels <- llabels
    mapping <- aes(x = time, y = rev(strata), label = llabels, 
        shape = rev(strata))
}

So then I copied the code and assigned an edited version with "\n" instead of " " in front of each of those "("'s since the counts were first and the pct's were second.

 .plot_survtable <- 
    # the copied and modified code went here

Then I tried to assign the correct environment:

environment(.plot_survtable) <- environment(ggsurvplot)

Tested. Failed to perform as desired so checked the environment assignment and saw that I probably needed to use assignInNamespace instead of environment<-:

 getAnywhere(.plot_survtable)
#2 differing objects matching ‘.plot_survtable’ were found
#in the following places
#  .GlobalEnv
#  namespace:survminer
#Use [] to view one of them
 ?assignInNamespace   # to check the argument list and their types
 assignInNamespace('.plot_survtable', .plot_survtable, "survminer" )

Tested again and now am claiming success!!

The limits were off because I used your code (predicated on years) with an object that had a different time scale (days or months).

这篇关于将字符串数据包装在ggplot2的surv_miner的风险表中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆