简体   繁体   中英

automatically adjust R plot size in a PDF output

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!

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 .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM