Skip to content

expressions as axis labels

2 messages · Uwe Ligges, Paul Murrell

#
[moved to R-devel]
Thomas Lumley wrote:
That change indeed is reasonable, but I don't think it is this
particular change (at least as documented) that makes Patrick's example
doesn't work any more.
Right for text() and friends, but not for axis(), see the example below,
and maybe this is a bug (Paul?). Anyway, fixing this bug (?) won't
change much for Patrick's example.
If sub- or superscripts are a expression, space is added below
respective above that expression (if not already done before), so
complex formulas can be generated. And, at least for me, the result is
somehow expected.

I am very surprised and wondered why Patrick's example worked in
R-1.4.1.
Without having looked into the code, there must have been something
fuzzy that is broken since R-1.5.0.
Here an example on the alignments:

 plot(1:12, type="n", axes=FALSE)

 expr <- expression(F^{-''}, Cl^{-''}, NO[2]^{-''}, NO[3]^{-''}, 
    PO[4]^{3-''}, SO[4]^{2-''}, Na^{+''}, NH[4]^{+''}, K^{+''},
    Mg^{2+''}, Ca^{2+''}, H^{+''})

 axis(1, 1:12, lab=expr)  # the "problem"

 text(1:12, 3, expr) # adj=c(0.5, 0.5), the default for text()

# This looks like the x-axis annotation:
 text(1:12, 5, expr, adj=c(0.5, 0)) 

# And this is all in *one* formula, as Patrick would like to get it
somehow.
# Only a guess: Was there something like a "coerce to one formula" in
R-1.4.1?

 text(6, 9, 
    labels = expression(F^{-''} * Cl^{-''} * NO[2]^{-''} * NO[3]^{-''} * 
    PO[4]^{3-''} * SO[4]^{2-''} * Na^{+''} * NH[4]^{+''} * K^{+''} *
    Mg^{2+''} * Ca^{2+''} * H^{+''}))


I am sure, Paul will get the point in less than a second.

Uwe Ligges
-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-
r-devel mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html
Send "info", "help", or "[un]subscribe"
(in the "body", not the subject !)  To: r-devel-request@stat.math.ethz.ch
_._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._
2 days later
#
Hi

<lots of examples snipped to be replaced by my own ...>

I don't think the current code is performing incorrectly, but its
behaviour may need a bit more explanation.  Here goes ... :)

1.  Here's a demonstration that, using text(), the horizontal and
vertical adjustments of mathematical annotations are done according to
the bounding boxes of the annotations ...

    expr <- expression(F^{-''}, Cl^{-''}, NO[2]^{-''}, NO[3]^{-''}, 
        PO[4]^{3-''}, SO[4]^{2-''}, Na^{+''}, NH[4]^{+''}, K^{+''},
        Mg^{2+''}, Ca^{2+''}, H^{+''})
    plot(1:12, type="n", axes=FALSE)
    box()
    axis(2)
    abline(v=1:12, h=1:12, col="grey")
    y <- 1
    for (xadj in c(0, .5, 1))
      for (yadj in c(0, .5, 1)) {
        text(1:12, y, expr, adj=c(xadj, yadj))
        y <- y+1
      }

... this is a very "graphical" view of these annotations, which makes
some sense when drawing annotations within the data region, but less
sense in the margins.  We probably need to allow the user to choose a
more "textual" alignment based on the "baseline" of the text in the
annotation.  One day ...

2. The mtext() and axis() functions do not allow the user to control
"vertical" adjustment of text -- they determine this adjustment
themselves based on the setting of par(las).  In the example given by
Patrick, where the x-axis labels are horizontal, the tick labels are
bottom-adjusted -- adj=c(<user xadj>, 0) -- relative to the "line" in
the margin that the labels are to be drawn on.  This is comparable to
row 4 in the plot produced above ...

    axis(1, at=1:12, labels=expr)

... In the old code, the tick labels were vertically centred --
adj=c(<user xadj>, 0.5) -- comparable to row 5 in the plot.  Sorry that
my bug fix to correct this "wrong" behaviour broke your example;  I
always worry that this sort of thing will occur when I try to "fix" bits
of the graphics code :(

3.  One "recommended" way to get your example to work would be to place
"phantom" subscripts and superscripts in your labels so that they are
all the same "height".  It's not pretty, but here's an example, ...

    expr <- expression(F[phantom(0)]^{-phantom(0)},
        Cl[phantom(0)]^{-phantom(0)},
        NO[2]^{-phantom(0)}, NO[3]^{-phantom(0)}, 
        PO[4]^{3-phantom(0)}, SO[4]^{2-phantom(0)},
        Na[phantom(0)]^{+phantom(0)},
        NH[4]^{+phantom(0)}, K[phantom(0)]^{+phantom(0)},
        Mg[phantom(0)]^{2+phantom(0)}, Ca[phantom(0)]^{2+phantom(0)},
        H[phantom(0)]^{+phantom(0)})
    plot(1:12, type="n", axes=FALSE)
    box()
    axis(2)
    axis(1, at=1:12, labels=expr)

Hope that helps.

Paul
-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-
r-devel mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html
Send "info", "help", or "[un]subscribe"
(in the "body", not the subject !)  To: r-devel-request@stat.math.ethz.ch
_._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._