Skip to content

mtext text size (cex) doesn't match plot

4 messages · Brian Ripley, George Locke, Peter Ehlers

#
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,
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).


example code to demonstrate the problem is below.

Thanks!

    data = c(1:10);
    par(mfrow=c(3,2));

    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);

    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);

    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);

    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);

    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);

    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);
#
On Wed, 11 May 2011, George Locke wrote:

            
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.
library(fortunes); fortune(14) applies -- see the posting guide.
#
thanks for reading the manual for me :X

2011/5/12 Prof Brian Ripley <ripley at stats.ox.ac.uk>:
#
On 2011-05-12 07:16, George Locke wrote:
For a bit more reading, you could check out ?title.
You could replace your mtext(....) calls with

   title(ylab='Y axis', cex.lab=1.5, line=4, font.lab=2)

Peter Ehlers