简体   繁体   中英

Error in `levels<-`(`*tmp*`, value = as.character(levels)) : factor level [3] is duplicated

I'm trying to plot faceted survival curves with autoplot but the combination of covariates and facetting them duplicates levels within the factors

library(survival)
library(ggfortify)
fit <- survfit( Surv(time, status) ~ inst + sex,
                 data = lung )

autoplot(fit, facets = TRUE)


Error in `levels<-`(`*tmp*`, value = as.character(levels)) : 
  factor level [3] is duplicated

Has anyone successfully plotted faceted survival curves with autoplot? I tried survminer but the plot looks horrific with the covariates taking up most of the plot area.

I think you should look again at ggsurvplot , since autoplot.survfit doesn't seem to like having more than one independent factor variable (whether you facet or not).

The ggsurvplot function returns a ggplot object, so you don't need to settle for the default options. You can add scales and styling as you see fit. To take your example, we could do:

library(survival)
library(ggfortify)
library(survminer)

fit <- survfit( Surv(time, status) ~ inst + sex,
                 data = lung )

p <- ggsurvplot(fit, facet.by = "inst", conf.int = TRUE) + 
  theme(strip.background = element_blank(),
        axis.line.x = element_line())

p$facet <- facet_wrap(.~inst, ncol = 3, nrow = 6, scales = "free")

p

在此处输入图像描述

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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