问题描述
如何在 R 中的 Latex 表达式中使用变量?
How do I use variables in Latex expressions in R?
例如:
plot(X, Y, main=expression(R^2))
将带有漂亮的上标 2 的 R 作为主标题.
Will put R with a nice superscripted 2 as main title.
但是假设我希望它说R^2: 0.5",其中 0.5 来自 R 变量.我该怎么做?
But let's say I want it to say 'R^2: 0.5', with 0.5 coming from a R variable. How do I do that?
推荐答案
Owen 的 hack 很酷,但并不是真正的常用方法.如果您使用 bquote
,这实际上非常简单.bquote
将与 quote
执行相同的操作,不同之处在于 .()
之间的所有内容都将在指定环境(或全局,如果没有指定).
The hack of Owen is pretty cool, but not really the common way to do that. If you use bquote
, this is actually pretty easy. bquote
will do the same as quote
, with the exception that everything between .()
will be evaluated in a specified environment (or the global, if nothing is specified).
一个简单的例子:
X <- 1:10
Y <- 1:10
a <- 0.8
plot(X,Y,main=bquote(R^2 : .(a)))
给:
另请参阅 ?bquote
和 ?plotmath
了解更多示例和可能性
See also ?bquote
and ?plotmath
for more examples and possibilities
这篇关于R中绘图标签中的乳胶和变量?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!