R pdf set margin
Asked Answered
A

2

9

I am trying to set the figure margin for a pdf generated with R. Using mar, I can reduce the plot margins. However the resulted plot is still a square in the middle of the page instead of using all the space. How can I change that?

pdf(file = "test.pdf", paper="a4r")
par(mar=c(0,0,0,0)+0.1)
plot(1:10,1:10)
dev.off()

Thank you!

enter image description here

Asper answered 15/11, 2016 at 6:4 Comment(4)
Use the width and height arguments to pdf, e.g., pdf(file = "test.pdf", width=8, height=5).Amok
You might still want to set the margins as well. If you want the top and right margins to be minimal and the x and y axis titles to be right at the edge of the plot, something like par(mar=c(4,4,0.5,0.5)) would do that.Amok
thanks, that works. Weird that there is no other alternative than to avoid using paper="a4r"...Asper
You can still use paper="a4r", but then your plot will have a margin unless you use width=11.69, height=8.27 (or larger measurements with the same aspect ratio).Amok
Q
3

Within pdf(), change the width and height arguments (https://stat.ethz.ch/R-manual/R-devel/library/grDevices/html/pdf.html). The default values are 7 and 7, which is likely why there is extra space on your page.

Queensland answered 29/1, 2017 at 5:43 Comment(0)
M
0

The eaf package is useful for this issue.

Example:

pdf("plot.pdf")
par(family = "Times")
plot(df)
dev.off()
eaf::pdf_crop("plot.pdf")
Mutable answered 4/4 at 1:34 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.