0

I have a loop to plot some scatterplots with ggplot, but I am getting them as individual plots (per unique participant ID). I would like to have multiple graphs per page, like a grid. I probably don't want all 50 participant plots on one page, but some reasonable arrangement would be good. Very new to R so explicit code or breakdowns appreciated. My code and thanks:

for(i in unique(data$id)){
  gi <- ggplot(aes(Date, Time), data = subset(data, id == i)) +
    geom_point() +facet_grid(~id)
  ggsave(filename = sprintf('%s.png', i), plot = gi)

}

per comments so far, I've tried following the gridExtra format but get an error message. New code:

gi <- list()
for(i in unique(data$id)){
  gi <- ggplot(aes(Date, Time), data = subset(data, id == i)) +
    geom_point() +facet_grid(~id)
  ggsave(filename = sprintf('%s.png', i), plot = gi)

}
do.call(grid.arrange,gi)

error message: Error in $<-.data.frame(*tmp*, wrapvp, value = list(x = 0.5, y = 0.5, : replacement has 17 rows, data has 23

Bananas
  • 3
  • 5
  • have you seen these similar posts; [1](https://stackoverflow.com/questions/9315611/grid-of-multiple-ggplot2-plots-which-have-been-made-in-a-for-loop), [2](https://www.r-bloggers.com/gridextra-%E2%80%93-multiple-plots-from-ggplot2/) – mnm Mar 30 '18 at 03:42
  • 1
    Possible duplicate of [Grid of multiple ggplot2 plots which have been made in a for loop](https://stackoverflow.com/questions/9315611/grid-of-multiple-ggplot2-plots-which-have-been-made-in-a-for-loop) – Maurits Evers Mar 30 '18 at 04:18
  • Hi @Ashish, I didn't see those posts (thank you!) but I'm still having trouble. I've edited my post above with the new code following gridExtra and the error message – Bananas Mar 30 '18 at 04:19
  • @Bananas how about providing a sample of your dataset. Hint, use `dput()`. See [here](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) on how to create a reproducible example. Also, see this [post](https://stackoverflow.com/questions/29814912/error-replacement-has-x-rows-data-has-y) for resolving the error message. One more piece of advice, please show some effort in solution seeking behavior. Its quite possible that someone else had the same/similar problem earlier. – mnm Mar 30 '18 at 05:40
  • @Ashish thanks for guidance with this. With regard to solution seeking behavior--I've spent an embarrassing number of hours trying to figure this out on my own by scouring the internet, including similar posts on Stackoverflow. Maybe Stackoverflow is not so great for novices. I am a double sinner in being new to stats and R and trying to learn both very quickly for a project. – Bananas Mar 30 '18 at 17:05
  • See the answer on this post: https://stackoverflow.com/a/45185980/18143306 – Melanie Baker Mar 22 '22 at 11:34

1 Answers1

0

Are you trying to save them as individual plots (what ggsave does), or add each plot to a list (what I'm guessing by gi <- list()), or both?

Each time you run through this loop, you're creating a plot, and then assigning it to the variable gi. If you typed gi or print(gi) into the console, you'd get just the last plot you made in that loop, right? If you're trying to make each plot an item in a list, you need to append each plot to the list.

So instead of gi <- ggplot(...), do something like gi <- append(gi, ggplot(...)).

I'm also confused by you both facetting and using a for loop. It seems like you could drop the loop and just use facet_wrap(~ id).

camille
  • 16,432
  • 18
  • 38
  • 60