从整数序列生成表达式文字的列表 [英] Generate a list of expression literals from an integer sequence

查看:160
本文介绍了从整数序列生成表达式文字的列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想将一系列整数映射到一个表达式文字的序列,以便将后者用作图形中的刻度标记标签,例如

I would like to map a sequence of integers to a sequence of expression literals in order to use the latter as tick mark labels in a plot, e.g.

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

到目前为止,我已经尝试了所有的变体 bquote 替代表达式等想想,但显然我一定错过了一些东西。
另外,FAQ和相关的SO问题&答案并没有完全解决这个问题。

So far I've tried all variations of bquote, substitute, expression etc. that I could think of, but apparently I must have missed something. Also, the FAQ and related SO questions & answers didn't fully solve this for me.

如何正确执行(我想要 axis pi 作为希腊字母,并有 -2 ... 2 替代 i 在上面的例子中)?

How would I do it correctly (I want axis to render pi as the greek letter and have -2 ... 2 substituted for i in the above example)?

推荐答案

尝试这个:

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)

这篇关于从整数序列生成表达式文字的列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆