在调整后的生存曲线ggadjustedcurves(survminer,ggplot2)中自定义线型 [英] Customising line type in adjusted survival curves ggadjustedcurves (survminer, ggplot2)

查看:242
本文介绍了在调整后的生存曲线ggadjustedcurves(survminer,ggplot2)中自定义线型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试绘制一条调整后的生存曲线,但是努力地按组更改线型.我可以使用典型的ggplot2语言自定义绘图的其他方面,但是我遇到了线型变化的问题.

I'm trying to plot an adjusted survival curve, but struggling with changing the line types by group. I'm able to customise other aspects of the plot using typical ggplot2 language, but I've hit a wall with changing line type.

示例:

library(survival)
library(survminer)

fit2 <- coxph( Surv(stop, event) ~ size + strata(rx), data = bladder )

ggadjustedcurves(fit2,
                 variable = "rx", 
                 data = bladder,
                 method = "average",
                 palette = c("#E69F00", "#56B4E9"),
                 size = 1.3,
                 legend = "right",
                 legend.title = expression(bold("Legend title")),
                 xlab = "Time",
                 font.legend = 12) +
  theme(legend.text.align = 0.5)

我尝试添加:

geom_line( aes( linetype = c(1, 2) )
add.params = list(linetype = c(1, 2))

还有

linetype = c(1, 2)

,但似乎没有任何效果.

but nothing seems to work.

推荐答案

首先,您需要看一下代码.

First you need to look at the code.

ggadjustedcurves

看来, ggadjustedcurves 将其所有参数传递给依赖于方法"参数(在本例中为平均值")的辅助函数,因此现在来看一下(隐藏的)函数:

It appears that ggadjustedcurves passes all it arguments on to helper functions that depend on the "method" argument, in this case "average", so now look at that (hidden) function:

 getAnywhere( ggadjustedcurves.average )

请注意,除了主函数"中定义的少数几个参数外,没有任何规定可以接受其他参数,即除了大小以外,不使用R的省略号机制或其他可能的aes参数的规范.(它也不使用 geom_line .)因此,您需要同时更改主功能和辅助功能的 ,以接受"linetype"参数.在这里,我展示了如何修改辅助函数(尽管也需要对 ggadjustedcurves 函数进行此操作,如果需要使它完全通用,还可以对其他辅助函数进行此操作):

And note that there is no provision to accept additional arguments beyond the few that are defined in the "master function", i.e. no use of R's ellipsis mechanism or specifications of other possible aes-arguments besides size. (It's also not using geom_line.) So you need to change both the master function and the helper function to accept a "linetype" argument. Here I show how to modify the helper function (although this needs to be done to the ggadjustedcurves function as well and maybe the rest of the helper functions if you want this to be completely general):

assignInNamespace('ggadjustedcurves.average',  

  function (data, fit, variable, size = 1, ..., linetype=linetype) 
    {
    time <- surv <- NULL
    lev <- sort(unique(data[, variable]))
    pred <- survexp(as.formula(paste("~", variable)), data = data, 
                    ratetable = fit)
    curve <- data.frame(time = rep(c(0, pred$time), length(lev)), 
                        variable = factor(rep(lev, each = 1 + length(pred$time))), 
                        surv = c(rbind(1, pred$surv)))
    ggplot(curve, aes(x = time, y = surv, color = variable)) + 
        geom_step(size = size, ..., linetype=linetype)  # not geom_line
    }, 
   pos="package:survminer")

如果您对"geom_segment线型"进行SO搜索,则会发现 geom_segment (这是 geon_step 所使用的)的构造方式不容易给它短向量以修改步长函数结果的连续"长度.请参见使用线型和组美学的ggplot错误.这意味着如果需要不同的线型,则需要使用 for-loop lapply 来构建单独的阶梯曲线".

If you do an SO search on "geom_segment linetype" you find that geom_segment (which is what geon_step uses) is not constructed in a manner that makes it easy to give it short vectors to modify "contiguous" lengths of step function results. See ggplot error using linetype and group aesthetics . This means you would need to use a for-loop or lapply to build separate "step-curves" if you need different line types.

这篇关于在调整后的生存曲线ggadjustedcurves(survminer,ggplot2)中自定义线型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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