user1701545
user1701545

Reputation: 6210

Using a parameter for expression function

I'm trying to plot a figure legend which I create with the expression function, where the argument to expression is a character which I want to plot as Greek letter symbol.

Here's an example of what I tried which obviously doesn't work:

param = "tau"
tau.vec = c(1,2,3,4)
plot(tau.vec, tau.vec)
legend("bottomright", sapply(tau.vec, function(x) expression(paste(param, "=", x, sep = " "))))

Any idea how should I do it?

Upvotes: 1

Views: 114

Answers (1)

MrFlick
MrFlick

Reputation: 206486

Try this

param = as.symbol("tau")
tau.vec = c(1,2,3,4)
plot(tau.vec, tau.vec)
legend("bottomright",as.expression(sapply(tau.vec, 
    function(x) bquote(.(param)==.(x)))))

expression() doesn't allow for escaping with variable values, bquote() does with the .() syntax.

Upvotes: 2

Related Questions