How to prevent resizing fonts, plot objects, etc. In R?

I want to have several graphs in one image, and I want to have a different number of graphs depending on the image. To be precise, I first create a 1x2 chart matrix, and then a 3x2 chart matrix. I want to use the same basic settings for these two images - the same font sizes, especially since this is for paper, and the font size should be at least 6 pt for the graphic.

To achieve this, I wrote the following code for 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() 

As you can see, I set the total width to 3 inches and then calculated the total height for my image so that the aspect ratio of the graphs is correct. Font size only changes resolution by a factor. In any case, the problem is that the font size changes significantly when moving from mat.row = 1 to mat.row = 3 . Other things also change, for example, marking axes and fields, although I specifically set them in inches. Take a look:

When 3 lines are set (cropped image):

3 rows

When only 1 line is specified (cropped image):

1 row

How can I prevent this? As far as I can see, I did everything I could. It took me quite a while, so I would like to make it work instead of switching to gglplot and learning everything from scratch again. It is also small enough that I really hope that I just miss something very obvious.

+6
source share
1 answer

In ?par we can find:

In a layout with exactly two rows and columns, the base value of "cex" is reduced by 0.83 times: if there are three or more rows or columns, the reduction ratio is 0.66.

Therefore, when the mfrow values mfrow from (2, 1) to (2, 3), the cex value changes from 0.83 to 0.66. cex affects the font size and text line height.

So, you can manually specify the cex value for your charts.

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

Hope this is what you need.

Plot for mat.row = 1 (cropped): mat.row = 1 (trimmed) And the graph for mat.row = 3 (cropped): mat.row = 3 (trimmed)

+4
source

Source: https://habr.com/ru/post/1013712/


All Articles