How to include superscript in texts on a graph on R?

I need it to look like this:

R ^ 2 = some values

And I tried the code below, but it didn’t work, it turned out as "R (expression (^ 2)) = multiple values" instead:

text (25, 200, paste ("R (expression (^2)) =", round (rsquarelm2, 2))) 
+6
source share
3 answers

You do not need a character vector, but the expression, therefore, is

 expression(R^2 == 0.85) 

- that's what you need. In this case, you want to replace the result of another R-operation. For this 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() everything in .( ) bquote() evaluated, and the result is included in the returned expression.

+7
source

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

 text(25, 200, bquote( R^2 == .(rs), list(rs=round(rsquarelm2,2)))) 
+4
source

How to enable formatting and mathematical values ​​on the graphs FAQ 7.13 .

For example, if ahat is an estimate of the parameter a you are interested in, use

title(substitute(hat(a) == ahat, list(ahat = ahat)))

(note that this is '==' , not '=' ). Sometimes bquote() gives a more compact form, e.g. title(bquote(hat(a) = .(ahat)))

where the subexpressions enclosed in '.()' are replaced by their values.

demo(plotmath) also useful.


In this case you can use

 title(substitute(R^2 = rsq, list(rsq = format(rsquarelm2, digits = 2)))) 

or

 title(bquote(R^2 == .(format(rsquarelm2, digits = 2)))) 

( format is more suitable here than round , because you want to control how this value is displayed, rather than creating an approximation of the value itself.)

+1
source

Source: https://habr.com/ru/post/954440/


All Articles