0

I am using a for loop to create 100 ggplots in R to be graphed onto one sheet of paper. However, I keep getting a mapping must be created by aes() error and I am not sure how to fix it.

I have tried the get function with and without environments, unclear where to go next.

for(i in 1:99){
  nam <- paste("p", i, sep = "")

  otunam <- paste("OTU", i, sep = "")

  otunam1 <- get(otunam, envir = as.environment(histotu), inherits = TRUE)

  plot <- ggplot(histotu, aes(x=otunam)) + geom_histogram(histotu, stat = "bin", binwidth = 0.01) + geom_vline(xintercept=expD[1,i], color = "red") + xlab(otunam)

  assign(nam, plot)
}

I would like to clear this error and be able to make 100 graphs using grid.arrange. I have this part to work, but not the for loop to create the objects that it calls.

Cettt
  • 11,460
  • 7
  • 35
  • 58
  • 2
    It's easier to help you if you include a simple [reproducible example](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) with sample input and desired output that can be used to test and verify possible solutions. I don't think you should be passing `histotu` to `geom_histogram` assuming that's a data.frame. You can just use `aes_string("name")` with a variable name rather than bothering with `get()` (or the new `aes(!!sym("name"))` option. And `assign()` should be avoided. Just use `map/lapply` to create a list of plots. – MrFlick May 08 '19 at 20:33
  • @Alyssa Decker: try these https://stackoverflow.com/a/52045613/ & https://stackoverflow.com/a/55194736/ – Tung May 09 '19 at 04:44

0 Answers0