R:layout()影响绘图区域中的边距大小

时间:2016-01-14 13:20:17

标签: r plot

我在尝试使用不同数量的面板自动生成R中的复合图时遇到问题。当我的图中有3个或更多面板时,边距明显不同(1x3图中较小),足以导致R错误地绘制标签并减损整体外观。

# plot empty plot with box around plot and figure
plot_box <- function() {
        plot(1, 1, type='n', bty='n', xaxt='n', yaxt='n', xlab='', ylab='')
        box(lwd = 6)
        box("figure", lwd=6, col='red')
}

png("box_test1.png", width=1000, height=500)
par(oma=c(0,0,0,0))
layout(t(1:2))
par(mar=c(3, 3, 3, 3))
plot_box()
par(mar=c(3, 3, 3, 3))
plot_box()
dev.off()

box_test1.png

png("box_test2.png", width=1500, height=500)
par(oma=c(0,0,0,0))
layout(t(1:3))
par(mar=c(3, 3, 3, 3))
plot_box()
par(mar=c(3, 3, 3, 3))
plot_box()
par(mar=c(3, 3, 3, 3))
plot_box()
dev.off()

box_test2.png

缩放图像以便在堆栈溢出时显示,但正如您从设备调用中看到的那样,它们的大小完全相同。

这个问题对我来说非常混乱,老实说感觉就像一个错误,但是我知道R绘图代码非常成熟,所以我有希望找到解决方案。

1 个答案:

答案 0 :(得分:3)

您使用mar设置保证金,这是以行为单位的,即, 好吧,难以妥善管理。

 ‘mar’ A numerical vector of the form ‘c(bottom, left, top, right)’
      which gives the number of lines of margin to be specified on
      the four sides of the plot.  The default is ‘c(5, 4, 4, 2) +
      0.1’.

如果您使用mai,您将获得一致的物理尺寸。

 ‘mai’ A numerical vector of the form ‘c(bottom, left, top, right)’
      which gives the margin size specified in inches.

另见?par:

 The meaning of ‘character size’ is not well-defined: this is set
 up for the device taking ‘pointsize’ into account but often not
 the actual font family in use.  Internally the corresponding pars
 (‘cra’, ‘cin’, ‘cxy’ and ‘csi’) are used only to set the
 inter-line spacing used to convert ‘mar’ and ‘oma’ to physical
 margins.  (The same inter-line spacing multiplied by ‘lheight’ is
 used for multi-line strings in ‘text’ and ‘strheight’.)