Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Superscript R squared for legend

Tags:

r

legend

I want to write a R-squared term for my legend but I do not know how. Could someone help me please? My legend syntax is:

legend(2,10, c("BW (MPE=3%, R-squared=0.77)", 
       "MY (MPE=5%, R-squared=0.80)", pch=c(2,3))

I would liek to express R-squared as R2 as we normally have in the text.

like image 844
hn.phuong Avatar asked Dec 08 '13 12:12

hn.phuong


People also ask

How to add superscripts or subscripts to a plot in R?

You can use the following basic syntax to add superscripts or subscripts to plots in R: #define expression with superscript x_expression <- expression (x^3 ~ variable ~ label) #define expression with subscript y_expression <- expression (y ~ variable ~ label) #add expressions to axis labels plot (x, y, xlab = x_expression, ylab = y_expression)

How to use superscript and subscript axis labels in ggplot2 in R?

In this article, we will see how to use Superscript and Subscript axis labels in ggplot2 in R Programming Language. First we should load ggplot2 package using library () function. To install and load the ggplot2 package, write following command to R Console. Now, let’s create a DataFrame.

How to use the star symbol after superscripts and subscripts?

Note that you need to add the star symbol (i.e. *) after a superscript or subscript, in case you want to continue with text afterwards. Have a look at the following example code:

How to add superscripts and subscripts to a text element?

It is also possible to add several superscripts and subscripts to a text element using the expression function and the symbols ^ and [] as shown in the previous examples. Note that you need to add the star symbol (i.e. *) after a superscript or subscript, in case you want to continue with text afterwards.


2 Answers

It will work if you combine bquote and as.expression:

plot(1:10)
legend(2, 10, c(as.expression(bquote("BW (MPE = 3%," ~ R^2 ~ "= 0.77)")),
                as.expression(bquote("MY (MPE = 5%," ~ R^2 ~ "= 0.80)"))), 
       pch=c(2,3))

enter image description here

like image 184
Sven Hohenstein Avatar answered Oct 11 '22 18:10

Sven Hohenstein


This is less complex than using c( as.expression ( bquote... multiple times:

plot(1:10)
legend(2, 10, expression("BW (MPE = 3%," ~ R^2 ~ "= 0.77)",
                 "MY (MPE = 5%," ~ R^2 ~ "= 0.80)"), 
        pch=c(2,3))

It is useful to understand that the expression function is really a way to make lists of expressions and that commas are therefore reserved as separators for that process. This means you cannot have a "naked" comma in something you want to be inside one of the distinct elements. The commas immediately after the %-signs are protected from parsing by the quotes. This could fully plotmath()-ified with:

plot(1:10)
legend(2, 10, expression(BW * list(MPE == 3*'%',
                                   R^2 == 0.77),
                         MY * list( MPE == 5*'%',
                                   R^2 == 0.80)
                         ), 
       pch=c(2,3))

That way the only character needing special attention is the '%'-sign because plotmath() uses that character to delimit items in the list of 'special' math tokens. See ?plotmath for the full list.

like image 9
IRTFM Avatar answered Oct 11 '22 18:10

IRTFM