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.
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)
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.
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:
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.
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))
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With