且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

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

更新时间:2023-11-24 12:29:46

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

dev.print "copies the graphics contents of the current device to a new device" (from ?dev.pront) which means that the proportions of the output graphic depends on how the source device has been scaled. Resizing the plot window affects the amount of whitespace you get.

如果您另外指定widthheight,您可以将绘图适合A4,使结果独立于绘图窗口.

If you additionally specify width and height you can fit the plot to A4, making the result independent of the plot window.

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

我将 widthheight 除以 2.54,将厘米转换为英寸.

I divided width and height by 2.54 to convert the centimeter to inches.

在@amwill04 的回答中使用 pdf()dev.off() 也可以使用,但缺点是您看不到 em> 写入文件之前的绘图.虽然这对于生产代码来说无关紧要,但它可能会使编写生成图形的代码更容易,因为只需 source 编写代码即可生成图形的预览.

Using pdf() and dev.off() as in the answer by @amwill04 also works but has the disadvantage that you do not see the plot before it is written to the file. While this should not matter with production code it may make writing the code that produces the graph easier because simply sourceing the code produces a preview of the graph.