0

I have survival data and I'm comparing two groups. The groups are optimal matched prior to analysis, so I need to include the pairings in the KM for the log rank test.

I completed the KM with the code below:

library(survival)
library(survminer)
library(tibble)

mydata<-tribble(
  ~id, ~ptime, ~pstatus, ~group, ~subclass,
  1, 4010, 0, 1, 1,
  2, 3, 1, 0, 1,
  3, 3672, 0, 1, 2,
  4, 980, 1, 0, 2,
  5, 3571, 0, 0, 3,
  6, 1606, 1, 1, 3,
  7, 622, 1, 0, 4,
  8, 1169, 1, 1, 4,
  9, 1589, 0, 1, 5,
  10, 816, 0, 0, 5,
  11, 551, 1, 1, 6,
  12, 1398, 0, 0, 6,
  13, 1578, 0, 1, 7,
  14, 431, 0, 0, 7,
  15, 1883, 0, 0, 8,
  16, 1549, 0, 1, 8
  )

object<-Surv(mydata$ptime, mydata$pstatus)

fit_km<-survfit(object ~ group + strata(subclass), data=mydata)

surv_pvalue(fit_km, data=mydata)

The next step is I'd like to get a KM plot. However, when I try it the usual way, I get an error - it seems like it's looking for something for every case of the data?

km_figure<-ggsurvplot(fit_km, data=mydata, 
                      pval=TRUE, 
                      risk.table=TRUE, fontsize=4, tables.height=0.15, 
                      conf.int=TRUE, 
                      ylim=c(0,1),
                      legend.labs=c("A", "B"), legend.title="Group", legend=c(0.7,0.8),
                      tables.theme=theme_cleantable(), 
                      ylab="Survival Probability", 
                      break.x.by=365.25, 
                      xscale=c("d_y"), 
                      xlab="Time Since Transplant (years)", 
                      xlim=c(0,2922),
                      palette = "lancet",
                      linetype = c("dotted", "solid"),
                      font.legend=12, pval.coord=c(0.2, 0.2), surv.scale="percent") + 
  guides(colour=guide_legend(nrow=2))

#Error in ggsurvplot_df(d, fun = fun, color = color, palette = palette,  : 
#The length of legend.labs should be 16

Is there an argument I need to be adding to the ggsurvplot to let it know that I've paired the data? Or another method I'm missing?

John Ryan
  • 343
  • 1
  • 9

0 Answers0