gauravsk/ranacapa

Error in `ggplot2::geom_ribbon()`:

Closed this issue · 0 comments

i am running several sequencing libraries, analysed with dada2, and built in form of phyloseq objects through a for loop. so, there is no possibility that one of them may be different from the other, at least in terms of data structures.

for two libs, however, i am having a very weird error:

Error in `ggplot2::geom_ribbon()`:
! Problem while computing aesthetics.
ℹ Error occurred in the 3rd layer.
Caused by error in `.S - .se`:
! non-numeric argument to binary operator
Run `rlang::last_trace()` to see where the error occurred.
Warning message:
In parallel::mclapply(seq_len(nr), rarefun, mc.preschedule = FALSE) :
  6 function calls resulted in an error

then i do analyse backtrace:

> rlang::last_trace(drop = FALSE)
<error/rlang_error>
Error in `ggplot2::geom_ribbon()`:
! Problem while computing aesthetics.
ℹ Error occurred in the 3rd layer.
Caused by error in `.S - .se`:
! non-numeric argument to binary operator
---
Backtrace:
     ▆
  1. ├─base::source("003_creating_phyloseq_obj_fungi.R")
  2. │ ├─base::withVisible(eval(ei, envir))
  3. │ └─base::eval(ei, envir)
  4. │   └─base::eval(ei, envir)
  5. ├─ranacapa::ggrare(...) at 003_creating_phyloseq_obj_fungi.R:232:9
  6. │ ├─base::plot(p)
  7. │ └─ggplot2:::plot.ggplot(p)
  8. │   ├─ggplot2::ggplot_build(x)
  9. │   └─ggplot2:::ggplot_build.ggplot(x)
 10. │     └─ggplot2:::by_layer(...)
 11. │       ├─rlang::try_fetch(...)
 12. │       │ ├─base::tryCatch(...)
 13. │       │ │ └─base (local) tryCatchList(expr, classes, parentenv, handlers)
 14. │       │ │   └─base (local) tryCatchOne(expr, names, parentenv, handlers[[1L]])
 15. │       │ │     └─base (local) doTryCatch(return(expr), name, parentenv, handler)
 16. │       │ └─base::withCallingHandlers(...)
 17. │       └─ggplot2 (local) f(l = layers[[i]], d = data[[i]])
 18. │         └─l$compute_aesthetics(d, plot)
 19. │           └─ggplot2 (local) compute_aesthetics(..., self = self)
 20. │             └─base::lapply(aesthetics, eval_tidy, data = data, env = env)
 21. │               └─rlang (local) FUN(X[[i]], ...)
 22. └─base::.handleSimpleError(...)
 23.   └─rlang (local) h(simpleError(msg, call))
 24.     └─handlers[[1L]](cnd)
 25.       └─cli::cli_abort(...)
 26.         └─rlang::abort(...)

i tried searching for this error and it seems like it's a problem of the data that i am passing to ggplot:

rareplot <- ggrare(subset_phylo, step=50, label="sampleID", color="fraction", parallel=T, plot=T)

final_plot <- rareplot +
		facet_wrap(~fraction) +
		theme_bw() +
		theme(legend.position="none")

it seems that i am not passing integers to the plot function but that is impossible for two reasons. phyloseq won't accept an otu table that contains something that is not an integer, and because if i do:

> sum(otu_table(subset_phylo))
[1] 65294

i get a result and not an error. so all entries are actually numerics.

what can the problem be, then?

EDIT:

i tried to run the code that i found here and it works without problems. it seems that it is not a problem of my data, i think.

FURTHER EDIT WITH SOLUTION

i think i know what the problem is. some samples, once subsetted and once all the variables i am working with are taken into account, sum up to 0 counts. to solve the problem, remove the samples that sum up to zero using something like:

subset_phylo_no_zeros <- prune_samples(as.character(colnames(otu_table(subset_phylo))[colSums(otu_table(subset_phylo))!=0]), subset_phylo)