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

StackOverflow https://stackoverflow.com/questions/13982856

  •  11-12-2021
  •  | 
  •  

문제

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

도움이 되었습니까?

해결책

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

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top