如何在R上的图上包括文字的上标? [英] How do I include a superscript to texts on a plot on R?
问题描述
我需要它看起来像这样:
I need it to look like this:
R ^ 2 =一些值
R^2 = some values
我尝试了下面的代码,但是它不起作用,而是显示为"R(expression(^ 2))= some values":
And I've tried the code below but it wouldn't work, it came out as "R (expression (^2)) = some values" instead:
text (25, 200, paste ("R (expression (^2)) =", round (rsquarelm2, 2)))
推荐答案
您不需要字符向量,而是表达式,因此
You don't want a character vector, but an expression, hence this
expression(R^2 == 0.85)
是您所需要的.在这种情况下,您要替换另一个R运算的结果.为此,您需要substitute()
或bquote()
.我发现后者更易于使用:
is what you need. In this case, you want to substitute in the result of another R operation. For that you want substitute()
or bquote()
. I find the latter easier to work with:
rsquarelm2 <- 0.855463
plot(1:10, 1:10, type = "n")
text(5, 5, bquote(R^2 == .(round(rsquarelm2, 2))))
对于bquote()
,.( )
中的所有内容都会被求值,结果将包含在返回的表达式中.
With bquote()
, anything in .( )
is evaluated and the result is included in the expression returned.
这篇关于如何在R上的图上包括文字的上标?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!