ggadjustedcurves错误:必须在'['中使用向量,而不是类矩阵的对象
我打电话给rlang::last_trace()
并得到:
> rlang::last_error()
<error>
message: Must use a vector in `[`, not an object of class matrix.
class: `rlang_error`
backtrace:
1. survminer::ggadjustedcurves(...)
2. survminer:::ggadjustedcurves.average(data, fit, variable, size = size)
4. base::sort.default(unique(data[, variable]))
8. base::order(x, na.last = na.last, decreasing = decreasing)
9. base::lapply(z, function(x) if (is.object(x)) as.vector(xtfrm(x)) else x)
10. base:::FUN(X[[i]], ...)
13. base::xtfrm.default(x)
15. base::rank(x, ties.method = "min", na.last = "keep")
17. tibble:::`[.tbl_df`(x, !nas)
18. tibble:::check_names_df(i, x)
尝试使用地层变量绘制调整后的曲线。
答案 0 :(得分:1)
我有一个非常相似的问题,但是当我生成一个示例数据集发布时,绘图工作使我认为是我的数据是一个问题。这是两个图都能很好地生成的可重现示例。
# generate reproducible data set
set.seed(3)
sampleData <- data.frame(Has_an_A_allele = sample(c("Yes", "No"), 1000, replace = TRUE),
Survival = rexp(1000, 0.5),
Censored = as.numeric(sample(c("1", "0"), 1000, replace = TRUE)),
Disease = sample(c("A", "B"), 1000, replace = TRUE),
Gender = sample(c("Male", "Female"), 1000, replace = TRUE),
Stage = sample(c("Early", "Advanced"), 1000, replace = TRUE),
Age = sample(c("Under 60", "Over 60"), 1000, replace = TRUE))
Summary(sampleData)
# create survival fit
fit<-survfit(Surv(Survival, Censored) ~ Has_an_A_allele, data = sampleData)
# create survival plot with p value
ggsurvplot(fit5, sampleData, xlim = c(0, 10), break.time.by = 2, pval = TRUE)
# create faceted survival plot with p value
ggsurvplot(fit5, sampleData, xlim = c(0, 10), break.time.by = 2, facet.by = "Disease", pval = TRUE)
当我在数据上运行此代码时,第一个图可以很好地工作,但多面图返回此错误
> ggsurvplot(fit4, x1502, xlim = c(0, 10), break.time.by = 2, facet.by = "Gender", pval = TRUE)
Error: Must use a vector in `[`, not an object of class matrix.
Call `rlang::last_error()` to see a backtrace
> rlang::last_error()
<error>
message: Must use a vector in `[`, not an object of class matrix.
class: `rlang_error`
backtrace:
1. survminer::ggsurvplot(...)
4. survminer::surv_group_by(data, grouping.vars = facet.by)
5. survminer:::.levels(data[, grouping.vars])
6. base::as.factor(x)
7. base::factor(x)
8. base::order(y)
9. base::lapply(z, function(x) if (is.object(x)) as.vector(xtfrm(x)) else x)
10. base:::FUN(X[[i]], ...)
13. base::xtfrm.default(x)
15. base::rank(x, ties.method = "min", na.last = "keep")
17. tibble:::`[.tbl_df`(x, !nas)
18. tibble:::check_names_df(i, x)
样本数据中使用的列名称与我的数据集中的列名称相同,并且数据类型相同,即,除“生存率”和“被审查”外的所有列都是因素。
编辑
我已修复它,并确定这绝对是一个数据问题,因此我查看了示例数据和数据的结构。
> str(x1502)
Classes ‘tbl_df’, ‘tbl’ and 'data.frame': 450 obs. of 7 variables:
> str(sampleData)
'data.frame': 1000 obs. of 7 variables:
因此,我使用x1502 <- as.data.frame(x1502)
将数据转换为数据框,现在一切正常。希望这对您的数据思维习惯有用。