[R] mtext text size (cex) doesn't match plot

Prof Brian Ripley ripley at stats.ox.ac.uk
Thu May 12 07:29:03 CEST 2011


On Wed, 11 May 2011, George Locke wrote:

> Hi,
>
> I am using mtext instead of the ylab argument in some plots because i
> want to move it away from the numbers in the axis.  However, the text
> in the X axis,
>
> for example:
>    par(mar=c(5, 5.5, 4, 2));
>    plot(data, main="plot name", xlab= 'X axis', ylab="",
>         font=2, cex.lab=1.5, font.lab=2, cex.main=1.8);
>    mtext('Y axis', side=2, cex=1.5, line=4, font=2);
>
> This works fine, but if I then set
>
>    par(mfrow=c(3,2));
>
> the text produced by mtext becomes much larger than the text "X axis"
> produced by plot, despite their having identical cex specifications.
> In this case, the words "Y axis" become much larger than "plot name".
> Note that without par(mfrow) the size of "X axis" and "Y axis" match
> iff their cex(.lab) arguments match.
>
> How can I make mtext produce text that exactly matches the xlab?  In
> my limited experience fiddling around with this problem, the size of
> the mtext does not depend on par(mfrow), whereas the size of the xlab
> does, so if there were a formula that relates the actual size of text,

Please do read the help!  ?mtext says

      cex: character expansion factor.  ‘NULL’ and ‘NA’ are equivalent
           to ‘1.0’.  This is an absolute measure, not scaled by
           ‘par("cex")’ or by setting ‘par("mfrow")’ or ‘par("mfcol")’.

so no 'limited experience fiddling around with this problem' was 
needed.  And see ?par:

      ‘cex’ A numerical value giving the amount by which plotting text
           and symbols should be magnified relative to the default.
           This starts as ‘1’ when a device is opened, and is reset when
           the layout is changed, e.g. by setting ‘mfrow’.

      ‘mfcol, mfrow’ A vector of the form ‘c(nr, nc)’.  Subsequent
           figures will be drawn in an ‘nr’-by-‘nc’ array on the device
           by _columns_ (‘mfcol’), or _rows_ (‘mfrow’), respectively.

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

> cex argument, and par(mfrow), then I could use that to attenuate the
> cex argument of mtext.  Any solution will do, so long as it maintains
> the relative sizes of the plot and the three text fields (main, x axis
> label, y axis label).

library(fortunes); fortune(14) applies -- see the posting guide.

-- 
Brian D. Ripley,                  ripley at stats.ox.ac.uk
Professor of Applied Statistics,  http://www.stats.ox.ac.uk/~ripley/
University of Oxford,             Tel:  +44 1865 272861 (self)
1 South Parks Road,                     +44 1865 272866 (PA)
Oxford OX1 3TG, UK                Fax:  +44 1865 272595


More information about the R-help mailing list