1

I want to generate dynamically a subscript for a text in a plot with an R expression. The text of the three points in the plot below should be $\pi_1$, $\pi_2$, and $\pi_3$.

lbs <- vector()
for(i in 1:3) lbs <- append(lbs, expression(pi[i]))

plot(1:3)
text(1:3, labels=lbs)

The Greek letter appears, but the subscript remains as i. I've tried bquote as well but without success.

I appreciate any suggestions.

2

1 Answer 1

2

Try this

lbs <- vector()
for(i in 1:3) lbs <- append(lbs, parse(text=(paste0("pi[",i,"]"))))

plot(1:3)
text(1:3, labels=lbs)

I am sure that there are better solutions but this is working. The problem with your code is that everything inside expression() is not evaluated. parse() evaluates and then returns an expression.enter image description here

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.