简体   繁体   English

`levels<-`(`*tmp*`, value = as.character(levels)) 中的错误:因子级别 [3] 重复

[英]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我正在尝试使用自动绘图 plot 刻面生存曲线,但协变量和刻面的组合会在因子内重复水平

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.我尝试了 survminer,但 plot 看起来很可怕,协变量占据了 plot 区域的大部分。

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).我认为您应该再次查看ggsurvplot ,因为autoplot.survfit似乎不喜欢拥有多个自变量(无论您是否考虑)。

The ggsurvplot function returns a ggplot object, so you don't need to settle for the default options. ggsurvplot function 返回一个 ggplot object,因此您无需满足于默认选项。 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

在此处输入图像描述

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

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