Create a list of expression literals from an integer sequence

I would like to match a sequence of integers with a sequence of expression literals so that I can use them as label labels in a graphic, for example

lbls <- lapply(-2:2, function(i) expression(i * pi))
plot(...)
axis(1, at=seq(-2,2)*pi, labels=lbls)

So far, I've tried all the options bquote, substitute, expressionand so on, that I could think of, but apparently, I must have missed something. In addition, answers to frequently asked questions and related questions and answers did not fully resolve this for me.

How will I do it correctly (I want it to axisdisplay pias a Greek letter and have -2... 2replaced with iin the above example)?

+4
source share
2 answers

:

lbls <- do.call("expression", lapply(-2:2, function(i) substitute(X * pi, list(X = i))))
plot(-10:10, -10:10, xaxt="n")
axis(1, at=seq(-2,2)*pi, labels=lbls)

enter image description here

+4

:

lbls <- parse(text = paste(seq(-2, 2), "pi", sep = "*"))
+2

All Articles