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