ggadjustedcurves error: Must use a vector in '[', not an object of class matrix

mindhabits picture mindhabits · Mar 20, 2019 · Viewed 16.2k times · Source

ggadjustedcurves error: Must use a vector in '[', not an object of class matrix

I call rlang::last_trace() and get this:

> 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)

Trying to plot adjusted curves using a strata variable.

Answer

C Jones picture C Jones · Mar 22, 2019

I have a very similar problem but when I generated a sample data set to post it the plots work which makes me think it is my data that is an issue. Here is the reproducible example in which both plots are generated nicely.

# 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)

When I run this code on my data the first plot works nicely but the faceted plot returns this error

> 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)

The column names used in the sample data are identical to those in my dataset and the data types are the same i.e. all columns except Survival and Censored are factors.

EDIT

I've fixed it, having decided that it was definitely a data issue I looked at the structure of the sample data and my data.

> str(x1502)
Classes ‘tbl_df’, ‘tbl’ and 'data.frame':   450 obs. of  7 variables:

> str(sampleData)
'data.frame':   1000 obs. of  7 variables:

So I used x1502 <- as.data.frame(x1502) to convert my data to a data frame and now it all works nicely. Hopefully this will work for your data mindhabits.