2

I have 3 R plots saved as pdf files (upper_left.pdf, upper_right.pdf, lower.pdf) as vector graphic and want to make a one-page pdf file and arrange them on it as follows:

Three plots in one page

What I have tried already

I have tried reading the pdf's using magick::image_read_pdf and appending them using magick::image_append. More specifically,

library(magick)

panel.ul <- image_read_pdf("upper_left.pdf")
panel.ur <- image_read_pdf("upper_right.pdf")
panel.l <- image_read_pdf("lower.pdf")

whole <- c(panel.ul, panel.ur) %>% 
    image_append() %>% 
    c(panel.l) %>% 
    image_append(stack = TRUE)

The first issue is magick::image_read_pdf imports the plot as png (if I'm right, not vector graphic though).

magick::image_append also 'works' and gives me what I want in viewer pane (in RStudio, next to Help).

I then try to save them using export::graph2pdf(whole), but it gives me a blank page.

So, if I am to use magick, there are two issues that need to be solved:

  1. importing plots as vector graphic objects (do not know the technical term in R)
  2. Exporting the stacked plot to a vector pdf file.

How can I solve it? thanks in advance.

psyguy
  • 312
  • 3
  • 16
  • 1
    Imagemagick is a raster image processor. Any vector files will be rasterized when read. So you cannot save them again as pure vector. They will be raster files in a PDF vector shell. Imagemagick is not the right tool for doing what you want. – fmw42 Jul 13 '19 at 18:04
  • Thanks @fmw42. I see now. What would you suggest instead to do the job? – psyguy Jul 13 '19 at 18:21
  • 1
    Sorry, I do not know what to suggest, since I have never done that. – fmw42 Jul 13 '19 at 18:58

1 Answers1

2

You're basically done. You only need to add

plot(whole) # plot the external object generated in ImageMagick to R's plotting device
savePlot(type = "pdf") # saves the current plotting device to a pdf file.

You will find your plot in your workoing directory called "Rplot.pdf". savePlot has many options to customize your pdf output. Make sure to check ?savePlot.

To recreate your scheme from above youll need to temporarily save the upper panel as a separate pdf before you paste it to on top of the lower panel:

whole2 <- image_append(c(panel.ul, panel.ur))
plot(whole2)
savePlot("whole2.pdf", type = "pdf") 

If the upper and lower panel do not look proportionate you can use the heght and width parameters of savePlot to adjust the size of the first pdf.

panel.upr <-   image_read_pdf("whole2.pdf")
final <-  image_append(c(image_append(panel.upr),panel.l), stack = TRUE)
plot(final)
savePlot("final.pdf", type = "pdf") 
Grada Gukovic
  • 1,228
  • 7
  • 13
  • 1
    Thanks Gerada. There are two issues still: first, the output is not vector anymore (apparently that's either `magick`'s fault or that of R's `plot()`). The other issue is that the output has a lot of white space around it and `savePlot` (apparently) doesn't allow costume size (`graph2pdf` can do that, but it still exports as non-vector graph) P.S. the sizes are already proportionate: the two on top are 5*5 inches each, the lower panel is 10*10 (so the 3-panel will be 10*20 in principal). – psyguy Jul 13 '19 at 17:11
  • 1
    The image magick library is for processing bitmaps only. When it reads PDF, it converts it to a bitmap, sorry. I don't think it is possible to render a PDF as vector graphics on R's devices. Also, it might not be the best tool for combining PDFs. – January Jul 13 '19 at 21:52
  • I never said that I want to render PDF. Both the code in the question and my response read pdfs into ImageMagick, which than turns them into a graphic format and glues them together (still as a picture), than I plot this picture onto R's graphic device with the plot() function and create PDFs with R's function savePlot.(not with IM) – Grada Gukovic Jul 15 '19 at 08:15