问题
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:
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:
- importing plots as vector graphic objects (do not know the technical term in R)
- Exporting the stacked plot to a vector pdf file.
How can I solve it? thanks in advance.
回答1:
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")
来源:https://stackoverflow.com/questions/57020571/reading-pdf-plots-arranging-them-on-a-grid-save-in-one-page-pdf-using-r