如何防止在R中调整字体,绘图对象等的大小?

Poo*_*ick 6 plot r

我想在同一图像中有多个图,我想根据图像得到不同数量的图.确切地说,我首先创建一个1x2的图表矩阵,然后创建一个3x2的图表矩阵.我想对这两个图像使用相同的基本设置 - 特别是相同的字体大小,因为这是针对纸张的,并且字体大小必须至少为6磅.

为了实现这一点,我为R编写了以下代码:

filename = "test.png"
font.pt = 6    # font size in pts (1/72 inches)
total.w = 3    # total width in inches
plot.ar = 4/3  # aspect ratio for single plot
mat.col = 2    # number of columns
mat.row = 1    # number of rows
dpi = 300

plot.mar = c(3, 3, 1, 2) + 0.1
plot.mgp = c(2, 1, 0)
plot.w = total.w / mat.col - 0.2 * plot.mar[2] - 0.2 * plot.mar[4]
plot.h = plot.w / plot.ar
total.h = (plot.h + 0.2 * plot.mar[1] + 0.2 * plot.mar[3]) * mat.row

png(filename, width = total.w, height = total.h, res = dpi * 12 / font.pt, units = "in")

par(mfrow = c(mat.row, mat.col), mai = 0.2 * plot.mar, mgp = plot.mgp)

plot(1, 1, axes = T, typ = 'p', pch = 20, xlab = "Y Test", ylab = "X Test")

dev.off()
Run Code Online (Sandbox Code Playgroud)

如您所见,我将总宽度设置为3英寸,然后计算图像的总高度,以便绘图的宽高比正确.字体大小仅通过因子更改分辨率.总之,现在的问题是,当我走的字体大小显著的变化mat.row = 1mat.row = 3.其他的东西也会改变,例如轴和边距的标记,即使我特别设置了以英寸为单位的那些.看一看:

设置3行时(裁剪图像):

3排

仅设置1行时(裁剪图像):

1排

我怎么能阻止这个?据我所见,我尽我所能.这花了我很长一段时间,所以我想让它工作,而不是转而gglplot学习一切从头开始.它也足够小,我真的希望我只是遗漏了一些非常明显的东西.

Ist*_*rel 5

?par我们可以找到:

在正好有两行和两列的布局中,“cex”的基值减少了 0.83 倍:如果有三个或更多行或列,则减少系数为 0.66。

因此,当您将mfrow值从 (2, 1)更改为 (2, 3) 时,cex值会从 0.83 更改为 0.66。cex影响字体大小和文本行高。

因此,您可以手动cex为绘图指定值。

par(mfrow = c(mat.row, mat.col), mai = 0.2 * plot.mar, mgp = plot.mgp, cex = 1)
Run Code Online (Sandbox Code Playgroud)

希望,这是你需要的。

绘图mat.row = 1(裁剪): mat.row = 1(裁剪) 并绘制mat.row = 3(裁剪): mat.row = 3(裁剪)