Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I include a superscript to texts on a plot on R?

I need it to look like this:

R^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)))
like image 871
Meed Avatar asked Sep 23 '13 17:09

Meed


People also ask

How do I add a superscript to text in R?

To indicate a subscript, use the underscore _ character. To indicate a superscript, use a single caret character ^ . Note: this can be confusing, because the R Markdown language delimits superscripts with two carets.

How do you put a superscript in a text box?

Apply superscript or subscript formatting to text Select the character that you want to format as superscript or subscript. On the Home tab, in the Font group, pick the Font Dialog Box Launcher. On the Font tab, under Effects, select the Superscript or Subscript check box.

How will you display text as a superscript?

The <sup> tag defines superscript text. Superscript text appears half a character above the normal line, and is sometimes rendered in a smaller font. Superscript text can be used for footnotes, like WWW. Tip: Use the <sub> tag to define subscript text.

Does LaTeX do superscript?

To produce text in superscript, use a caret followed by the text you want in superscript in curly brackets. To write text as a subscript, use an underscore followed by the text in curly brackets. The symbol "&" on its own is used as part of a code in LaTeX.


2 Answers

You don't want a character vector, but an expression, hence this

expression(R^2 == 0.85)

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))))

With bquote(), anything in .( ) is evaluated and the result is included in the expression returned.

like image 195
Gavin Simpson Avatar answered Sep 21 '22 19:09

Gavin Simpson


The paste function returns a string, not an expression. I prefer to use bquote for cases like this:

text(25, 200, bquote( R^2 == .(rs), list(rs=round(rsquarelm2,2))))
like image 30
Greg Snow Avatar answered Sep 24 '22 19:09

Greg Snow