Hello,
Here are two ways.
The first is an adaptation from your code. It uses facet_wrap_paginate,
not *_grid_*.
plotObj2 <- vector("list",2)
for(pg in 1:2) {
? plotObj2[[pg]] <- ggplot(egDat) +
??? geom_point(aes(y = obsd, x = x),
?????????????? na.rm = TRUE, shape = 20, colour = "blue") +
??? geom_line(aes(y = fit2, x = cPred)) +
??? facet_wrap_paginate(facets = ~Trt,
??????????????????????? ncol = 4, nrow = 3, page = pg) +
??? theme_bw()
}
print(plotObj2)
The second is an adaptation of SO[1]. It needs two calls to the plot
code and it's slower but gets the job done.
g <- ggplot(egDat) +
? geom_point(aes(y = obsd, x = x),
???????????? na.rm = TRUE, shape = 20, colour = "blue") +
? geom_line(aes(y = fit2, x = cPred)) +
? facet_wrap_paginate(facets = ~Trt, ncol = 4, nrow = 3, page = 1) +
? theme_bw()
n <- n_pages(g)
for(i in 1:n){
? print(g + facet_wrap_paginate(~Trt, ncol = 4, nrow = 3, page = i))
}
print(g)
Hope this helps.