Question

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().

Was it helpful?

Solution

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

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top