gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-01 23:07:55 25 4
gpt4 key购买 nike

我在尝试使用不同数量的面板在 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 绘图代码非常成熟,所以我希望有一个解决方案。

最佳答案

您使用 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’.)

关于R:layout() 影响绘图区域的边距大小,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34790682/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com