我正在绘制生存曲线,我修改了ggsurv函数(here)以添加我需要的几个选项 .

这是我一直在使用的代码(底部的修改功能):

Surv_all <-  survfit(Surv(Time, Status==1)~1, data = dataset, na.action = na.exclude)
Surv_ecog <- survfit(formula = Surv(Time, Status == 1) ~ Perfo, data = dataset,na.action = na.exclude)
plot_surv(Surv_all) ### Gives error
plot_surv(Surv_ecog) ### Plots fine

因此,当我绘制分层生存时,该函数效果很好,但是当我尝试绘制整体生存(Surv_all)时,我得到错误:

> plot_surv(Surv_all,pval = T)
Error in 365 * years : non-numeric argument to binary operator

years是一个数值变量,设置为默认值years = 1,所以我不明白错误的来源 . 任何帮助将非常感谢!

编辑:如果我拆分为2个函数(一个用于单个层次,一个用于多个)我不会在构建函数的方式中看到我没有看到的东西 .

提前致谢!

一些数据:

dput(dataset [1:100,])结构(列表(时间= c(0L,0L,1L,1L,2L,3L,4L,4L,5L,6L,6L,7L,7L,7L,8L,8L) ,9L,10L,10L,11L,11L,12L,12L,12L,13L,14L,14L,14L,14L,14L,14L,15L,16L,17L,17L,18L,19L,20L,20L,21L,21L ,21L,21L,21L,21L,21L,21L,22L,22L,23L,23L,24L,25L,25L,25L,26L,26L,27L,27L,28L,28L,28L,28L,29L,29L,29L ,31L,31L,32L,34L,34L,35L,35L,35L,35L,35L,35L,35L,37L,38L,38L,38L,38L,39L,40L,40L,40L,41L,41L,41L,42L ,42L,42L,42L,42L,42L,43L,44L,45L,45L),Perfo =结构(c(3L,4L,4L,5L,4L,4L,4L,5L,5L,3L,4L,3L, 4L,5L,4L,5L,4L,1L,5L,3L,4L,2L,3L,5L,5L,1L,2L,2L,3L,4L,5L,4L,4L,2L,4L,2L,1L, 1L,4L,1L,2L,2L,3L,3L,4L,5L,5L,2L,3L,2L,5L,4L,1L,3L,4L,2L,4L,3L,4L,1L,2L,3L, 4L,3L,4L,5L,2L,3L,3L,1L,3L,1L,2L,3L,3L,4L,4L,5L,4L,1L,2L,3L,4L,1L,2L,3L,4L, 3L,4L,5L,1L,2L,3L,3L,4L,5L,2L,4L,4L,5L),.标签= c(“0”,“1”,“2”,“3”,“4 “),class =”因子“),状态= c(1L,1L,1L,1L,1L,1L,1L,1L,1L,1L,1L,1L,1L,1L,1L,1L,1L,1L,1L,1L,1L, 1L,1L,1L,1L,0L,0L,1L,0L,1L,1L,1L,1L,1L,1L,1L,1L,0L,1L,0L,0L,1L,0L,1L,0L,0L, 1L,1L,1L,1L,1L,1L,1L,1L,1L,1L,1L,1L,1L,1L,0L,0L,0L,1L,1L,1L,1L,1L,1L,1L,1L, 0L,0L,0L,1L,0L,1L,0L,1L,1L,1L,1L,1L,1L,1L,1L,1L,1L,1L,1L,0L,0L,0L,1L,0L,0L, 1L,1L,1L,1L)),. Name = c(“时间”,“Perfo”,“状态”),row.names = c(1L,2L,3L,4L,5L,6L,7L,8L, 9L,10L,11L,12L,13L,14L,15L,16L,17L,18L,19L,21L,22L,23L,24L,25L,26L,27L,28L,29L,30L,31L,32L,34L,35L, 36L,37L,38L,39L,40L,41L,42L,43L,44L,45L,46L,47L,48L,49L,51L,52L,53L,54L,55L,56L,57L,58L,59L,60L,61L, 62L,63L,64L,65L,66L,67L,68L,69L,70L,71L,72L,73L,74L,75L,76L,77L,78L,79L,80L,81L,82L,83L,84L,85L,86L, 87L,88L,89L,90L,91L,92L,93L,94L,95L,96L,97L,98L,99L,101L,102L,103L,104L),class =“data.frame”)

并修改功能:

plot_surv <- function(s, CI = F, plot.cens = F, surv.col = 'gg.def',
                    cens.col = 'red', lty.est = 1, lty.ci = 2,
                    cens.shape = 4,  xlab = 'Days', years = 1,
                    ylab = 'Survival', main = "",pval=T){
library(ggplot2)
#library(plotly)
#years<-as.numeric(years)

strata <- ifelse(is.null(s$strata) ==T, 1, length(s$strata))
stopifnot(length(surv.col) == 1 | length(surv.col) == strata)
stopifnot(length(lty.est) == 1 | length(lty.est) == strata)

ggsurv.s <- function(s, CI = F, plot.cens = F, surv.col = 'gg.def',
                   cens.col = 'red', lty.est = 1, lty.ci = 2,
                   cens.shape = 4,  xlab = 'Days', years = 1,
                   ylab = 'Survival', main = "", pval=T){

#years<-as.numeric(years)

dat <- data.frame(time = c(0, s$time),
                  surv = c(1, s$surv),
                  up = c(1, s$upper),
                  low = c(1, s$lower),
                  cens = c(0, s$n.censor))
dat.cens <- subset(dat, cens != 0)

col <- ifelse(surv.col == 'gg.def', 'black', surv.col)

pl <- ggplot(dat, aes(x = time, y = surv)) +
  xlab(xlab) + ylab(ylab) + ggtitle(main) +
  geom_step(col = col, lty = lty.est) + theme_bw()

pl <- if(years== 1){
  pl + scale_x_continuous(limits = c(0,365),breaks = seq(0,365,30))
} else {pl + scale_x_continuous(limits = c(0,365*years),breaks = seq(0,365*years,30*years))}

pl <- if(CI == T) {
  pl + geom_step(aes(y = up), color = col, lty = lty.ci) +
    geom_step(aes(y = low), color = col, lty = lty.ci)
} else (pl)

pl <- if(plot.cens == T & length(dat.cens) > 0){
  pl + geom_point(data = dat.cens, aes(y = surv), shape = cens.shape,
                  col = cens.col)
} else if (plot.cens == T & length(dat.cens) == 0){
  stop ('There are no censored observations')
} else(pl)
}


ggsurv.m <- function(s, CI = F, plot.cens = T, surv.col = 'gg.def',
                   cens.col = 'red', lty.est = 1, lty.ci = 2,
                   cens.shape = 4,  xlab = 'Days',
                   ylab = 'Survival', main = "",pval=T) {

# years<-as.numeric(years)

 n <- s$strata


groups <- factor(unlist(strsplit(names(s$strata), '='))[seq(2, 2*strata, by = 2)])
gr.name <-  unlist(strsplit(names(s$strata), '='))[1]
gr.df <- vector('list', strata)
ind <- vector('list', strata)
n.ind <- c(0,n); n.ind <- cumsum(n.ind)
for(i in 1:strata) ind[[i]] <- (n.ind[i]+1):n.ind[i+1]

for(i in 1:strata){
  gr.df[[i]] <- data.frame(
    time = c(0, s$time[ ind[[i]] ]),
    surv = c(1, s$surv[ ind[[i]] ]),
    up = c(1, s$upper[ ind[[i]] ]),
    low = c(1, s$lower[ ind[[i]] ]),
    cens = c(0, s$n.censor[ ind[[i]] ]),
    group = rep(groups[i], n[i] + 1))
}

dat <- do.call(rbind, gr.df)
dat.cens <- subset(dat, cens != 0)

pl <- ggplot(dat, aes(x = time, y = surv, group = group)) +
  xlab(xlab) + ylab(ylab) + ggtitle(main) + theme_bw() +
  geom_step(aes(col = group, lty = group))+
  theme(legend.title=element_blank()) #+ 
#scale_y_continuous(sec.axis = sec_axis(~., breaks = seq(0,1,0.1),name = ""))

col <- if(length(surv.col == 1)){
  scale_colour_manual(name = gr.name, values = rep(surv.col, strata))
} else{
  scale_colour_manual(name = gr.name, values = surv.col)
}
pl <- if (pval==T) {
  pl + annotate("label",label=paste0("Log-Rank p-value: ",p.val),x=40,y=min(s$surv)-0.1)
} else {pl}

pl <- if(surv.col[1] != 'gg.def'){
  pl + col
} else {pl + scale_colour_discrete(name = gr.name)}

line <- if(length(lty.est) == 1){
  scale_linetype_manual(name = gr.name, values = rep(lty.est, strata))
} else {scale_linetype_manual(name = gr.name, values = lty.est)}

pl <- pl + line

pl <- if(CI == T) {
  if(length(surv.col) > 1 && length(lty.est) > 1){
    stop('Either surv.col or lty.est should be of length 1 in order
         to plot 95% CI with multiple strata')
  }else if((length(surv.col) > 1 | surv.col == 'gg.def')[1]){
    pl + geom_step(aes(y = up, color = group), lty = lty.ci) +
      geom_step(aes(y = low, color = group), lty = lty.ci)
  } else{pl +  geom_step(aes(y = up, lty = group), col = surv.col) +
      geom_step(aes(y = low,lty = group), col = surv.col)}
} else {pl}

pl <- if(years== 1){
  pl + scale_x_continuous(limits = c(0,365),breaks = seq(0,365,30))
} else {pl + scale_x_continuous(limits = c(0,365*years),breaks = seq(0,365*years,30*years))}

pl <- if(plot.cens == T & length(dat.cens) > 0){
  pl + geom_point(data = dat.cens, aes(y = surv), shape = cens.shape,
                  col = cens.col)
} else if (plot.cens == T & length(dat.cens) == 0){
  stop ('There are no censored observations')
} else(pl)

}

pl <- if(strata == 1) {ggsurv.s(s, CI , plot.cens, surv.col ,
                              cens.col, lty.est, lty.ci,
                              cens.shape, xlab,
                              ylab, main,years)
} else {ggsurv.m(s, CI, plot.cens, surv.col ,
               cens.col, lty.est, lty.ci,
               cens.shape, xlab,
               ylab, main,pval,years)}
pl
}