16

When using the pdf() function in R for saving a plot in an external file, we can specify width and/or height to adjust the size of the plot. However, there are situations when we obtain multiple plot (say using par(mfrow=c(2,4))). In this situation, it's kind of difficult to determine what is the best width and height for the PDF file in order to have all plots displayed properly. Is there a way to let R automatically "fit the plots" in the PDF file? I searched the arguments in pdf() and tried some, but the results are not satisfactory. Thank you very much!

alittleboy
  • 10,616
  • 23
  • 67
  • 107
  • 2
    This seems impossibly vague. How is one to determine the "best" width and hheight? – IRTFM Oct 15 '12 at 05:06
  • @DWin: sorry for the confusion. what I mean "best" is that all parts of the plots are visible to readers, and there won't be any "cut-off" due to paper size limits (like A4). – alittleboy Oct 15 '12 at 05:22
  • As far as I know, there is no way to change the sizes of individual pages in the pdf device. Most people produce multiple different sized pdfs and use Ghostscript to stick the separate pages together. – sebastian-c Oct 15 '12 at 07:44
  • @sebastian-c: oh...that's not a good news...anyway thanks for your comments! – alittleboy Oct 15 '12 at 21:45

1 Answers1

2

How about something using ggplot?

require(ggplot2)

# Bogus data
x <- rnorm(10000)
y <- as.factor(round(rnorm(10000, mean=10, sd=2), 0))
df <- data.frame(vals=x, factors=y)

myplot <- ggplot(data=df, aes(x=vals)) +
  geom_density() +
  facet_wrap(~ factors)

ggsave(filename="~/foo.pdf", plot=myplot, width=8, height=10, units="in")

EDIT: Should you need to print over multiple pages, see this question.

Community
  • 1
  • 1
Peter
  • 4,219
  • 4
  • 28
  • 40