简体   繁体   English

autoplot() 中的属性 function 什么都不做,R

[英]attributes in autoplot() function do not do anything , R

I have a dataset with time, status ( 1=death, 0 = censored), treatement =1,2.我有一个包含时间、状态(1=死亡,0=已审查)、治疗=1,2 的数据集。
I create my survival object km_2, I want to plot Kaplan-Meijer plot using autoplot().我创建了我的生存 object km_2,我想使用 autoplot() plot Kaplan-Meijer plot。 I do not know what my mistake is but setting the attributes ( legendLabs for example ) do not make any change to the basic KM plot.我不知道我的错误是什么,但设置属性(例如 legendLabs)不会对基本 KM plot 进行任何更改。

km_2 <- survfit(Surv(time, status)~treatment, data=prostate)
library(ggplot2)
library(ggfortify)
autoplot(km_2, 
         alpha=0.7, #transparency of CIs
         shape= 10, #shape used to incdicaed censored obs
         xlab= 'month', ylab='% survived',
         title = 'KM- plot to compare tr1, tr 2',
         legendLabs= c('tr1','tr2'),
         pval=T,
         plotTable= T
         
         )

There are a few things to point out here.这里有几点需要指出。 Firstly, autoplot is a generic function, so the method used and the arguments it accepts depends on the type of object you are passing to it.首先, autoplot是一个通用的 function,因此使用的方法及其接受的 arguments 取决于您传递给它的 object 的类型。 In this case, you are passing a survfit object, and you will be able to see the correct parameters to use if you type ?autoplot.survfit into the console.在这种情况下,您传递的是survfit object,如果您在控制台中键入?autoplot.survfit ,您将能够看到要使用的正确参数。

From this you will see that there is no legendLabs or plotTable option, and that the alpha for the confidence intervals is controlled with conf.int.alpha = .从这里您将看到没有legendLabsplotTable选项,并且置信区间的 alpha 由conf.int.alpha =控制。 Similarly, the censoring shape is controlled with censor.shape .同样,审查形状由censor.shape控制。

Another issue is that there doesn't seem to be a way to change the factor labels in the legend, but in this case it is easy enough to change them in the data when you create the survfit object.另一个问题是似乎没有办法更改图例中的因子标签,但在这种情况下,在创建生存拟合survfit时很容易在数据中更改它们。

Lastly, it's a good idea to make a reproducible example if you want prompt and useful answers.最后,如果您想要快速有用的答案,最好制作一个可重现的示例。 It took a while to recreate a reasonable data structure to test and demonstrate this answer.重新创建一个合理的数据结构来测试和证明这个答案需要一段时间。

library(survival)
library(ggplot2)
library(ggfortify)

km_2 <- survfit(Surv(time, status) ~ treatment, 
                data = within(prostate, treatment <- c("tr1", "tr2")[treatment]))

autoplot(km_2, 
         conf.int.alpha = 0.7,
         censor.shape = 10,
         xlab = 'month', 
         ylab = '% survived',
         title = 'KM- plot to compare tr1, tr2'
  )

在此处输入图像描述

As an aside, you might get a result closer to your expectation using survminer :顺便说一句,您可能会使用survminer得到更接近您期望的结果:

library(survminer)
ggsurvplot(km_2, conf.int = TRUE, risk.table = TRUE)

在此处输入图像描述


Reproducible data可重现的数据

set.seed(1)

prostate <- data.frame(time = pmin(runif(100) * rep(c(7, 10), each = 50), 5),
                       treatment = rep(1:2, each = 50),
                       status = c(rbinom(50, 1, 0.3), rbinom(50, 1, 0.5)))

prostate$status[prostate$time > 5] <- 0

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM