I am experiencing an issue with the ggsurvplot function from the survminer package in R. When attempting to plot survival curves using ggsurvplot with the newdata argument, the function incorrectly interprets the number of observations as the number of levels, resulting in an error. Without the newdata argument, the function does not recognize the levels of the factor variable correctly, leading to another error.
Observed Behavior:
With newdata Argument:
set.seed(123)
d.dat.tot <- data.frame(
DONOR_SURVIVAL_TIME = rexp(100, 0.1),
DEAD_OR_ALIVE = sample(0:1, 100, replace = TRUE),
GENOTYPE.111758446 = sample(c("A/A", "G/A", "G/G"), 100, replace = TRUE)
)
# Data preparation
d.dat.tot.clean <- d.dat.tot[d.dat.tot$DEAD_OR_ALIVE < 2, ]
d.dat.tot.clean$GENOTYPE.111758446 <- as.factor(d.dat.tot.clean$GENOTYPE.111758446)
cox_model_single_111758446 <- coxph(
Surv(DONOR_SURVIVAL_TIME, DEAD_OR_ALIVE) ~ GENOTYPE.111758446,
data = d.dat.tot.clean
)
cox_plot_single_111758446 <- ggsurvplot(
survfit(cox_model_single_111758446, newdata = d.dat.tot.clean),
data = d.dat.tot.clean,
pval = TRUE,
conf.int = TRUE,
risk.table = TRUE,
legend.title = "Genotype",
legend.labs = levels(d.dat.tot.clean$GENOTYPE.111758446),
xlab = "Time to Last Follow-up, mo",
ylab = "Cumulative Survival, %",
ggtheme = theme_minimal(),
palette = "set2"
)
This results in the error:
Error in ggsurvplot_df(d, fun = fun, color = color, palette = palette, :
The length of legend.labs should be 236.
(236 is the number of cases I have.)
Without newdata Argument:
The function fails to recognize the levels of the factor variable correctly and returns:
Error in ggsurvplot_df(d, fun = fun, color = color, palette = palette, :
The length of legend.labs should be 1.
Yukiyaama is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.