Include Text Control Characters in Plotmath Expressions

Include text control characters in plotmath expressions

As you have figured plotmath does not support newlines within, but you can use mtext with bquote, to write each line. For example I create a list of lines :

Lines <- list(bquote(paste( "Plot of " , phi , " of: "  , .(lab))),
bquote(paste("Functional form: " , .(form)))

mtext(do.call(expression, Lines),side=3,line=1:0)

Sample Image

Is paste a special argument in plotmath expressions?

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)

Concatenate strings and expressions in a plot's title

You want to read ?plotmath to see how to do this sort of thing. Here is an example:

plot(1:10, main = expression(ARL[1] ~ "curve for" ~ S^2))

The [.] is subscript, whilst ^ gives superscript. The ~ spaces out the parts of the expression as if there were literal spaces.

Edit: normally I would have done:

plot(1:10, main = expression(ARL[1] ~ curve ~ for ~ S^2))

but that throws an error because for is being interpreted as the start of a for() loop call.

plotmath: how to use a function definition in a label?

You can use this solution:

lambda <- function(t) 1/t

sb <- as.character(as.expression(body('lambda')))

plot(0~0, xlab=bquote(lambda(t)==.(sb)))

Using % as subscript in plotmath

plot.new()
text(0.5, 0.5, expression(paste(P[Con_30*"%"*Fat], "=0.611")), cex = 1.3)

Adding Text with Expressions to R plot

I think you could use the answer from this question. For your case, something like:

plot(1:598,xaxt='n',yaxt='n', type="l")

my_text <- list( bquote( "RMSE=Root Mean Squared Error" ) ,
bquote( "MAD=Mean Absolute Deviance" ) ,
bquote( paste( "Average RMSE=5.78" , m^3/h) ) ,
bquote( paste( "Average MAD=4.47", m^3/h ) ) )

mtext(side=1,do.call(expression, my_text), line=-1:-4, adj=0)

The line argument puts every bquote on a separate line (-1 to -4). You might need some tweaking of adj and padj to get the position right -- see ?mtext.

For the bonus question: I prefer to use regular plotting functions, as I find it more easy to control those fully, e.g. beyond the default colours and spacing. With a bit of tweaking, I also prefer the plain look of base graphics to the ggplot looks. I use regular plotting functions for all of my scientific publications, never had a problem.

Displaying a greater than or equal sign

An alternative to using expressions is Unicode characters, in this case Unicode Character 'GREATER-THAN OR EQUAL TO' (U+2265). Copying @mnel's example

.d <- data.frame(a = letters[1:6], y = 1:6)

ggplot(.d, aes(x=a,y=y)) + geom_point() +
scale_x_discrete(labels = c(letters[1:5], "\u2265 80"))

Unicode is a good alternative if you have trouble remembering the complicated expression syntax or if you need linebreaks, which expressions don't allow. As a downside, whether specific Unicode characters work at all depends on your graphics device and font of choice.

How to automate graph axis labeling with plotmath expressions?

Try parse instead of expression:

ylab(parse(text = df[i, ]$ylabel))

Expression and new line in plot labels

You can introduce a line break inside an expression:

bquote(atop("first line",
"second line" ~ x ^ 2))

(I’m using bquote rather than expression here – both work in this case.)

Execute demo(plotmath) for more information and look at the documentation for atop.

boxplot apparently has some trouble interpreting expressions in its title. A simple fix is to plot the title separately:

boxplot(data, main = '')
title(bquote(atop("first line", "second line" ~ x ^ 2)))


Related Topics



Leave a reply



Submit