Malcolm Perry
2016-Jan-14 13:42 UTC
[R] R: layout() affects margin size in subfigures [unexpected behaviour]
The absolute margin size of figures in R seems to be affected by the layout of the plot, which i think is surprising (not sure if it qualifies as a bug). The following plots have different margins sizes, with the 1x3 plot margins being smaller (thus giving a larger plot area). This is causing havoc with a package I am writing to automatically generate composite figures, since labels are positioned differently depending on the number of panels. 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("margin_test_1.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() png("margin_test_2.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() I have also posted this question to StackOverflow, and it has images of the graphical output which illustrate the problem better: http://stackoverflow.com/questions/34790682/r-layout-affects-margin-size-in-plot-regions Thanks, Malcolm PS I was unsure if this question belonged to help or devel - I will repost on devel if it is likely to get better answers. [[alternative HTML version deleted]]
Sarah Goslee
2016-Jan-14 16:48 UTC
[R] R: layout() affects margin size in subfigures [unexpected behaviour]
You're setting margin using mar, which is in terms of lines, which is, well, difficult to manage properly. ?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?. If you use mai instead, you will get a consistent physical size. ?mai? A numerical vector of the form ?c(bottom, left, top, right)? which gives the margin size specified in inches. See also this bit of ?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?.) Sarah On Thu, Jan 14, 2016 at 8:42 AM, Malcolm Perry <mgperry32 at gmail.com> wrote:> The absolute margin size of figures in R seems to be affected by the layout > of the plot, which i think is surprising (not sure if it qualifies as a > bug). The following plots have different margins sizes, with the 1x3 plot > margins being smaller (thus giving a larger plot area). This is causing > havoc with a package I am writing to automatically generate composite > figures, since labels are positioned differently depending on the number of > panels. > > 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("margin_test_1.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() > > png("margin_test_2.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() > > I have also posted this question to StackOverflow, and it has images of the > graphical output which illustrate the problem better: > http://stackoverflow.com/questions/34790682/r-layout-affects-margin-size-in-plot-regions > > Thanks, > > Malcolm > > PS I was unsure if this question belonged to help or devel - I will repost > on devel if it is likely to get better answers. >-- Sarah Goslee http://www.numberwright.com