9

When supplying a plotmath expression to a plot title I thought that paste worked just like paste, but now I am not sure. Is it a special argument to plotmath functions? In plotmath paste() behaves like paste0() and paste0 is quoted directly, whilst the sep argument of paste() is ignored, but not quoted in the expression. How is paste() interpreted? See the four examples below:

#  Data to plot
x <- seq( 55 , 70 , length = 2001 )
probs <- dexp( x , rate = 5 / 60  )
s <- sample( x , 10000 , prob = probs , repl = TRUE )


#  Some plots
par(mfrow = c(2,2) )
#1)  paste() behaves like paste0() here
hist( s , breaks = 30 , 
    main = expression( paste( "1: Sampling of" , Phi , "Distribution" ) ) ,
    xlab = "No spaces in title using paste()" , axes = F )

#2)  add spaces in the expression where they are needed
hist( s , breaks = 30 , 
    main = expression( paste( "2: Sampling of "  , Phi , " Distribution" ) ) ,
    xlab = "Single spaces in titles: paste() behaves like paste0()" , axes = F )

#3)  Don't put spaces, use a sep argument - ignored
hist( s , breaks = 30 , 
    main = expression( paste( "3: Sampling of"  , Phi , "Distribution" , sep = " " ) ) ,
    xlab = "No spaces using paste(), `sep` argument is ignored but not quoted in expression" , axes = F )

#4)  paste0 is not recognised
hist( s , breaks = 30 , 
    main = expression( paste0( "4: Sampling of " , Phi , " Distribution" ) ) ,
    xlab = "paste0() not recognised" , axes = F )

enter image description here

Simon O'Hanlon
  • 58,647
  • 14
  • 142
  • 184
  • 1
    you may need `eval(expression(paste...))` --- at least that's what I did at the console level to return a char string. – Carl Witthoft Oct 24 '13 at 11:40
  • @CarlWitthoft that worked with say my sample code for plot 1? I find that R instead searches for the object `Phi` (Phi is a plotmath feature and should be interpreted as the symbol). – Simon O'Hanlon Oct 24 '13 at 12:07
  • Hmph. I seem to remember a tutorial on how to do exactly what you want, but have no clue where it was. – Carl Witthoft Oct 24 '13 at 14:02
  • 1
    @CarlWitthoft: Your comment betrays a fundamental misunderstanding of plotmath expressions. If you want a character string to be printed to a plot device with `text` you just enclose it in quotes. If you want to concatenate expressions or strings with displayed spaces use `~`. The example using `paste` and an interposed " " in the ?plotmath page doesn't really need that function, and could be more compactly written as: `text(8, 3, expression(frac(1, sigma*sqrt(2*pi))~~e^{ frac( -(x-mu)^2, 2*sigma^2)}), cex = 1.2)` – IRTFM Oct 24 '13 at 19:35
  • @DWin, well, I *did* say I'd completely forgotten -and that included the `?plotmath` page :-) . But thanks for reminding me to rtfm before commenting; I deserved that. – Carl Witthoft Oct 24 '13 at 19:41

1 Answers1

6

Indeed paste in plotmath is not the classic paste. See ?plotmath:

paste(x, y, z) juxtapose x, y, and z

paste in the context of plotmath doesn't have a sep argument.

And in the source code plotmath.c you can see that paste is redefined:

/*----------------------------------------------------------------------
*
* Code for Concatenate Expressions
*
*/

static int ConcatenateAtom(SEXP expr)
{
    return NameAtom(expr) && NameMatch(expr, "paste");
}

static BBOX RenderConcatenate(SEXP expr, int draw, mathContext *mc,
                         pGEcontext gc, pGEDevDesc dd)
{
    BBOX bbox = NullBBox();
    int i, n;

    expr = CDR(expr);
    n = length(expr);

    for (i = 0; i < n; i++) {
        bbox = CombineBBoxes(bbox, RenderElement(CAR(expr), draw, mc, gc, dd));
        if (i != n - 1)
         bbox = RenderItalicCorr(bbox, draw, mc, gc, dd);
        expr = CDR(expr);
    }
    return bbox;
}

It is dispatched later in the file:

static BBOX RenderFormula(SEXP expr, int draw, mathContext *mc,
                         pGEcontext gc, pGEDevDesc dd)
{
    SEXP head = CAR(expr);

    ....
    else if (ConcatenateAtom(head))
        return RenderConcatenate(expr, draw, mc, gc, dd);
    ....

(That being said I know nothing about C so I may be wrong on that one)

plannapus
  • 18,529
  • 4
  • 72
  • 94