0

I have created a pdf file containing 6 map plots, but I would like for them to be side by side in pairs rather than each being on separate pages as they are right now (At the moment all six plots are on six different pages). When complete, there should be 3 pages with pairs of side-by-side plots on each page.

As well as this, I wanted to append those plots to an existing pdf which contains some descriptive statistics.

Whilst I understand these could all be done using other software and toolkits, they aren't applicable for my task, as I am creating a function which carries out statistical analyses, creates the aforementioned plots, and then puts them into a pdf.

The following is the code I used to create the original PDF:

  pdf("Map Prints.pdf")
  print(map1)
  print(map2)
  print(map3)
  print(map4)
  print(map5)
  print(map6)

  dev.off()

Any help is much appreciated.

dayleymart
  • 23
  • 8
  • 1
    Have you looked into `knitr` or `rmarkdown`? You can combine text, `r` text output and figures and then output it into a pdf. – Walker in the City Jan 18 '18 at 18:37
  • @WalkerintheCity is dead on -- with `rmarkdown` you can have all your code in one document that carries out your statistical analyses, and puts your text, tables, and figures into a pdf for you. If for whatever reason you can't use `rmarkdown`, try `help("layout")` – duckmayr Jan 18 '18 at 18:42
  • @duckmayr Yep, that's of course the first thing that came up when I was doing my research on this. However, finding code to put within my function which uses markdown to create the pdf was extremely hard to find and I couldnt figure out how it was supposed to be done... – dayleymart Jan 18 '18 at 18:48
  • @dayleymart if you use `rstudio` you might want to try creating an `r` notebook. It was actually the first type of document that I ever used in `r` (for a class) and I think it is pretty intuitive. It can be turned into a pdf just like a `rmd` document. I don't really understand what your specific issue is though. – Walker in the City Jan 18 '18 at 19:01
  • @WalkerintheCity I understand that there are methods to creating the pdf. However, I am trying to find a method to create a pdf within the function, so that when a user calls the function, the graphs are produced in pairs (side-by-side) and saved as a pdf. – dayleymart Jan 18 '18 at 19:04
  • If you create a `ggplot` object with `facets` or pther methods of showing two plots side by side, saving it to a .pdf file would return a pdf file that you want. – Gautam Jan 18 '18 at 20:11

1 Answers1

0

Here's a quick and dirty example:

Use ggplot to create, gridExtra to arrange those and ggsave to save them to a .pdf file. All of this can be enclosed in a function call that accepts a list of plot names (or inputs to create a plot) as its argument(s).

CODE

library(ggplot2)
p1 <- ggplot(mtcars, aes(x = disp, y = mpg)) + geom_point()
p2 <- ggplot(mtcars, aes(x = hp, y = mpg)) + geom_point()
p3 <- ggplot(mtcars, aes(x = wt, y = mpg)) + geom_point()

library(gridExtra)
combined <- grid.arrange(p1, p2, p3, ncol = 2)

# To save, use ggsave() function 
ggsave("file_name.pdf", combined, heigth = 11, width = 17, dpi = 600)

You can call the function selectively on the plots you want to have on the same page and get all separate pages. There are several functions to merge pdf pages into one document e.g. tabulizer::merge_pdf or the answers posted for How to append a plot to an existing pdf file, Combine (bind) existing pdf files in R, combine multiple pdf plots into one file etc.

Gautam
  • 2,597
  • 1
  • 28
  • 51