Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

r

pdf

ggplot2

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?

like image 446
Jada Avatar asked May 28 '13 02:05

Jada


2 Answers

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")
like image 115
qwwqwwq Avatar answered Nov 14 '22 08:11

qwwqwwq


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")
like image 37
MS Berends Avatar answered Nov 14 '22 09:11

MS Berends