R:plot:在一张A4 pdf页面上正确拟合多个图

ada*_*888 2 pdf plot r

我有多个绘图,我想将其打印在A4 pdf页面上,以便图表填充整个页面。当我使用下面的代码时,图表上方和下方都有很多空白。如何将空白减少到大约1厘米并增加图表高度?谢谢您的帮助。

group <- "Title"
layout(matrix(c(1:12), 6, 2) )
par(mar = c(0, 4.1, 1.5, 2.1),oma = c(2, 0, 2, 0)) 
plot(1:10)
plot(1:10)
plot(1:10)
plot(1:10)
plot(1:10)
plot(1:10)
plot(1:10)
plot(1:10)
plot(1:10)
plot(1:10)
plot(1:10)
plot(1:10)

mtext(group, outer = TRUE, cex = 1.5)
mtext("text", outer = TRUE,side =1)
dev.print(pdf, file="charts12.pdf" ,onefile=T,paper='A4') 
Run Code Online (Sandbox Code Playgroud)

amw*_*l04 5

从更改dev.print为只需pdf允许您指定设备的widthheight。使用的单位是英寸,因此A4为8.27英寸X 11.69英寸。我在下面添加了一个示例。对于此示例,我不得不调整图表周围的顶部边距。

data("mtcars")
pdf("test.pdf", width = 8.27, height = 11.69)
group <- "Title"
layout(matrix(c(1:12), 6, 2) )
par(mar = c(0, 4.1,2.5, 2.1),oma = c(2, 0, 2, 0))
plot(mtcars$mpg ~ mtcars$hp)
plot(mtcars$mpg ~ mtcars$hp)
plot(mtcars$mpg ~ mtcars$hp)
plot(mtcars$mpg ~ mtcars$hp)
plot(mtcars$mpg ~ mtcars$hp)
plot(mtcars$mpg ~ mtcars$hp)
plot(mtcars$mpg ~ mtcars$hp)
plot(mtcars$mpg ~ mtcars$hp)
plot(mtcars$mpg ~ mtcars$hp)
plot(mtcars$mpg ~ mtcars$hp)
plot(mtcars$mpg ~ mtcars$hp)
plot(mtcars$mpg ~ mtcars$hp)
mtext(group, outer = TRUE, cex = 1.5)
mtext("text", outer = TRUE,side =1)
dev.off()
Run Code Online (Sandbox Code Playgroud)


CL.*_*CL. 5

dev.print“将当前设备的图形内容复制到新设备”(来自?dev.pront),这意味着输出图形的比例取决于源设备的缩放方式。调整绘图窗口的大小会影响您获得的空白量。

如果您另外指定width并且height可以将绘图拟合到 A4,则使结果独立于绘图窗口。

dev.print(pdf, file="charts12.pdf" ,onefile=T,paper='A4', width = 21/2.54, height = 29.7/2.54) 
Run Code Online (Sandbox Code Playgroud)

我除以widthheight2.54 将厘米转换为英寸。

在@amwill04 的回答中使用pdf()anddev.off()也有效,但缺点是在将图写入文件之前您看不到该图。虽然这对生产代码无关紧要,但它可能会使编写生成图形source的代码更容易,因为简单地编写代码会生成图形的预览。