plot() and do.call(): How to pass expressions to plot title when '...' is used otherwise?
Asked Answered
B

1

2

When run the following code, I obtain Error in as.graphicsAnnot(text) : could not find function "bold". How can I fix this?

my.qq <- function(x, main=expression(bold(italic(F)~~"Q-Q plot")),
                  margs=list(side=3, cex=par("cex.main"), font=par("font.main"),
                  adj=par("adj"), xpd=NA), ...)
{
    plot(qnorm(ppoints(n <- length(x))), sort(x), ...)
    do.call(mtext, c(list(main), margs))
}
x <- rnorm(100)
my.qq(x)
my.qq(x, main=substitute(bold(italic(F)[N(mu.,s2.)]~~"Q-Q plot"), list(mu.=0, s2.=1))) # fails

My goal is to use the list margs to pass additional arguments to mtext(). That's normally done with ..., but those arguments are already passed to plot().

Bronwyn answered 21/12, 2012 at 0:56 Comment(0)
R
3

substitute in this case returns an language object, not an expression. the expression expression is used loosely in R, however here it appears that mtext needs an object of class expression.

You can ensure this by wrapping substitute(...) in as.expression()

my.qq(x, main=as.expression(substitute(bold(italic(F)[N(mu.,s2.)]~~"Q-Q plot"), list(mu.=0, s2.=1))))

or more simply by passing an expression to substitute (as would be required in a normal call to mtext)

my.qq(x, main=substitute(expression(bold(italic(F)[N(mu.,s2.)]~~"Q-Q plot")), list(mu.=0, s2.=1)))

Both the examples above will produce enter image description here

There is a note in the help for substitute

Substituting and quoting often cause confusion when the argument is expression(...). The result is a call to the expression constructor function and needs to be evaluated with eval to give the actual expression object.

however in this case eval is not required

Rubicon answered 21/12, 2012 at 1:6 Comment(1)
Argh, I should have known that ... Thanks a lot for helping (so quickly)!Bronwyn

© 2022 - 2024 — McMap. All rights reserved.