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):
When only 1 line is specified (cropped image):
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.