0

Currently I am looking for a solution to save 72 plots to an PDF file. Those 72 plots are created through a function and the below code of repeat.

The ID stands for a person within my dataset. This person has multiple rows of data attached to his ID. To go to the next person I use ID = ID + 1

With the below code I manage to create an PDF file but, this is a PDF file with 72 pages. I would like to have 4 plots on each row. Something in the idea of nrow = 4 like you use in grid.arrange. Preferably 4 on each row and 24 on each page.

pdf("plot1.pdf")
 repeat {
  ID = ID + 1
  print(ggplot(ID))
  if (ID == 72){
    break}}
dev.off()
Robbie Voort
  • 121
  • 6
  • Your question is not reproducible in many ways, please read https://stackoverflow.com/q/5963269, [mcve], and https://stackoverflow.com/tags/r/info (top of the [tag:r] tag page) for how to improve the question with sample data and minimal but complete working code. – r2evans Apr 19 '21 at 13:20
  • Tangent: while `repeat` is not wrong, it might be more intuitive (and perhaps safer) to do `for (x in ID) print(ggplot(x))`, as it will self-terminate when the `ID`s are exhausted, there is no risk if there are any gaps, and (to me) it is easier to read/understand and therefore maintain. – r2evans Apr 19 '21 at 13:21
  • @r2evans It was merely a question on how to get the exported plots side by side on a PDF page. I wanted to mention the repeat function so that it would be known what my method was. I could ofcourse provide a full reproducible example but I thought it was not needed as the question would be: How can I get the exported plots side by side on a PDF page (nrow = 4 in an arrange.grid -- as example) while using my method. If you still require a reproducible example please see: https://stackoverflow.com/questions/67128043/scale-the-x-and-y-axis-of-a-scatterplot-to-fill-the-whole-axis (Posted by me) – Robbie Voort Apr 19 '21 at 15:43
  • You reference `grid.arrange()` in your question... is there a particular reason why that is not an option for you here? Similar functionality with the ability to specify number of columns/rows exists in other packages like `cowplot` via `plot_grid()`. – chemdork123 Apr 19 '21 at 16:15
  • @chemdork123 As far as my knowledge goes you need to refer to an actual plot in order to create a grid, for example: `plot1 <- ggplot(df, aes(x = x, y = y)`. In a grid.arrange you would refer to plot1 etc and then use nrow = 4 to get 4 graphs side by side (if you provide 4 plots ofcourse). As I don't specifically create plots but more like generate them through a function and repeat, as different data needs to be calculated dependend on the persons ID I can't realy use grid.arrange. I know how grid.arrange() works, and therefore I refer to nrow = 4 because I want that kind of result. – Robbie Voort Apr 19 '21 at 16:46
  • If you generate them through a function, you can likely use something like `lapply()` and your function to iterate through some vector. If the output from the function inside `lapply()` will output a `ggplot()` object (like `ggplot(ID)` in your pseudocode example), then your output from `lappy()` is a list of plots. Using `cowplot::plot_grid(plotlist = your_lapply_output, nrow = 4)` - that should get you the plots in that arrangement. – chemdork123 Apr 19 '21 at 22:07
  • Regardless... we need a bit more information to help you out. Can you come up with a representative example using one of the built-in datasets? It's not clear what your code would actually be plotting. – chemdork123 Apr 19 '21 at 22:09
  • @chemdork123 Would this be enough for an example? It is an other question I asked but the code is the same: https://stackoverflow.com/questions/67128043/scale-the-x-and-y-axis-of-a-scatterplot-to-fill-the-whole-axis/67128344#67128344 – Robbie Voort Apr 20 '21 at 09:39

1 Answers1

0

I am still not sure wether this is a legit solution but at least it is fixed now!

Apparently I was looking for a solution like this:

plot.list <- list()
for(i in 1:72){
  plot.list[[length(plot.list) + 1]] <- plot_utility(i)
}

grid1 <- grid.arrange(grobs = plot.list, ncol = 4)

ggsave("plot1.pdf", 
       plot = grid1, 
       device = "pdf", 
       scale = 2,
       width = 25, 
       height = 20, 
       units = c("cm")
)
Robbie Voort
  • 121
  • 6