How to save a graph as an a4 size pdf file under windows system? (R; ggplot2)

19,820

Solution 1

pdf("a4_output.pdf", paper="a4")

And if you want A4 to be the default pdf size, so that you don't need to keep on specifying it:

pdf.options(paper = "a4")

The above works for default R plotting, for ggplot you need to use the ggsave function. I'm not sure if ggplot has built-in paper sizes, but you can specify dimensions in any units with ggsave, so for A4 you can do:

ggsave(file="a4_output.pdf", width = 210, height = 297, units = "mm")

Solution 2

I ran into this, and found out that I needed the correct device (cairo_pdf) to print all text correctly. Works with grid.arrange too.

For portrait:

ggsave(filename="yourfile.pdf", 
       plot = yourplot, 
       device = cairo_pdf, 
       width = 210, 
       height = 297, 
       units = "mm")

For landscape:

ggsave(filename="yourfile.pdf", 
       plot = yourplot, 
       device = cairo_pdf, 
       width = 297, 
       height = 210, 
       units = "mm")

Or, with dplyr you could use:

ggplot(...) %>%
  ggsave(
    filename="yourfile.pdf", 
    plot = ., 
    device = cairo_pdf, 
    width = 210, 
    height = 297, 
    units = "mm")
Share:
19,820
Jada
Author by

Jada

A naturalist, like Astrology and art.

Updated on June 22, 2022

Comments

  • Jada
    Jada almost 2 years

    How to save a graph as an a4 size pdf file under windows system? In R, it's easy to save a graph as a pdf file, but how to save it as an a4 size file?