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.
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.
Related
I want to add a greek character to the y-axis of my barplot in R.
The problem is that I need this character to be integrated in the title. I want to write:
Diameter of aperture ("mu"m)
in the axis label.
With
ylab=expression()
I can write the greek character, with
ylab="axis title"
I can write the title with proper spaces between the words.
But I can't find a way to put all these together and write a proper label with a greek word in the axis label. I hope I was clear enough.
If you're using plotmath{grDevices}, the main help page (plotmath) contains an example of what you appear to want:
xlab = expression(paste("Phase Angle ", phi))
or for your case, I guess:
ylab = expression(paste("Diameter of aperture ( ", mu, " )"))
Does this work for you?
I think I followed your question properly. The ~ forces a space between characters in a call to expression(). Is this what you want?
plot(1:3, ylab = expression("Diameter of apeture (" * mu ~ "m)"),
, xlab = expression("Force spaces with ~" ~ mu ~ pi * sigma ~ pi)
, main = expression("This is another Greek character with space" ~ sigma))
And if you want to substitute variables in the text, use bquote. For instance, if you have a variable mu and want to show it in the title, then use the following idiom:
mu <- 2.8
plot(1:3, main=bquote(mu == .(mu)))
The part enclosed in .() will be substituted, so that the value of mu will be printed and not the greek "mu" character. Consult the R help on bquote for details.
This should be much more straight forward with latex2exp:
require(latex2exp)
plot(1, xlab = TeX('$\\mu$'))
And, in case you were dealing with an estimated quantity, plotmath{grDevices} also offers the possibility of adding a hat to your greek letter:
ylab = expression(paste("Diameter of aperture ( ", hat(mu), " )"))
The mu enclosed in hat() does the trick.
I give an updated answer to Chase's plot example (from 2011) above while working with Windows 10 with a suitable TrueType Font with the help package utf8 on R 3.62.
In my answer, I assign a value to μ. The unicode characters I just call out by their hex (??) code. The space is just a 'space' inside quotes. See my answer here:
See also:
http://www.unicode.org/Public/10.0.0/ucd/UnicodeData.txt
utf8-package {utf8}
# pi small case
utf8_print("\u03C0")
"π"
# sigma small case
utf8_print("\u03C3")
"σ"
μ <- abs(digamma(1))
μseq <- seq(μ,3*μ,μ)
dev.new()
plot(μseq, ylab = "Diameter of apeture ( μ m)",
, xlab = "Force spaces with ~ μ \u03C0 \u03C3 \u03C0 ",
, main = "This is another Greek character with space \u03C3")
#
How can I integrate this phrase into the text function:
expression(R^2,"=", round(cor(tkw14_cor$TKW, tkw14_cor$Yield),2))
I also tried
expression(R^2 == round(cor(tkw14_cor$TKW, tkw14_cor$Yield),2)))
and
expression(R^2 ~ == ~ round(cor(tkw14_cor$TKW, tkw14_cor$Yield),2)))
and all the 'paste' possibilities..
It does not work like this.
My goal is to have the text print:
R^2 (as superscript) = rounded correlation coefficient.
Thanks,
You can use bquote function.
Random example here:
plot(1)
set.seed(1)
value <- round(cor(rnorm(1000), rnorm(1000)), 2)
text(1,1.2,bquote("R"^"2" ~ " = " ~.(value)))
I am attempting to label a plot axis in r using the bquote function. The desired string includes the greek mu character and a variable.
This results in spaces in either side of the mu:
xlab = bquote("Lake NO3 (" ~ mu ~ "mol/L): " ~ .(i))
How can I get rid of the spaces next to mu?
I tried using paste and paste0 expressions, but neither of these allow both a greek character and a variable.
If you want to get rid off space on either side of mu
i <- 25
plot(1, xlab=bquote("Lake NO3 ("*mu*"mol/L): " ~.(i) ))
If you need space on the right
plot(1, xlab=bquote("Lake NO3 ("*mu~ "mol/L): " ~.(i) ))
I have a little trouble to add the equations into scatter plots using "legend"
A simple example is as follow:
plot(1:100)
# The below code can work if I add "= 0.1234" directly.
legend(locator(1), expression(paste("Linear model: ", R^2, "= 0.1234",sep="")),
text.col= "black",cex=1,bty="n")
# The below code cannot work if I add the "ps".
ps = "= 0.1234"
legend(locator(1), expression(paste("Linear model: ", R^2, ps, sep="")),
text.col= "red",cex=1,bty="n")
The real issue I have is a little complex with this example.
So how should I revise this code?
The "ps"-object is being handled as an expression, i.e. not being evaluated. To get around this use bquote and .()
legend(locator(1), legend= bquote("Linear model: "* R^2*.(ps)),
text.col= "red",cex=1,bty="n")
BTW the first version would be more compactly represented without the paste:
legend(locator(1), expression(Linear~model*":"~ R^2 == 0.1234),
text.col= "black",cex=1,bty="n")
The only thing needing to be quoted is the semi-colon.
I want to add a greek character to the y-axis of my barplot in R.
The problem is that I need this character to be integrated in the title. I want to write:
Diameter of aperture ("mu"m)
in the axis label.
With
ylab=expression()
I can write the greek character, with
ylab="axis title"
I can write the title with proper spaces between the words.
But I can't find a way to put all these together and write a proper label with a greek word in the axis label. I hope I was clear enough.
If you're using plotmath{grDevices}, the main help page (plotmath) contains an example of what you appear to want:
xlab = expression(paste("Phase Angle ", phi))
or for your case, I guess:
ylab = expression(paste("Diameter of aperture ( ", mu, " )"))
Does this work for you?
I think I followed your question properly. The ~ forces a space between characters in a call to expression(). Is this what you want?
plot(1:3, ylab = expression("Diameter of apeture (" * mu ~ "m)"),
, xlab = expression("Force spaces with ~" ~ mu ~ pi * sigma ~ pi)
, main = expression("This is another Greek character with space" ~ sigma))
And if you want to substitute variables in the text, use bquote. For instance, if you have a variable mu and want to show it in the title, then use the following idiom:
mu <- 2.8
plot(1:3, main=bquote(mu == .(mu)))
The part enclosed in .() will be substituted, so that the value of mu will be printed and not the greek "mu" character. Consult the R help on bquote for details.
This should be much more straight forward with latex2exp:
require(latex2exp)
plot(1, xlab = TeX('$\\mu$'))
And, in case you were dealing with an estimated quantity, plotmath{grDevices} also offers the possibility of adding a hat to your greek letter:
ylab = expression(paste("Diameter of aperture ( ", hat(mu), " )"))
The mu enclosed in hat() does the trick.
I give an updated answer to Chase's plot example (from 2011) above while working with Windows 10 with a suitable TrueType Font with the help package utf8 on R 3.62.
In my answer, I assign a value to μ. The unicode characters I just call out by their hex (??) code. The space is just a 'space' inside quotes. See my answer here:
See also:
http://www.unicode.org/Public/10.0.0/ucd/UnicodeData.txt
utf8-package {utf8}
# pi small case
utf8_print("\u03C0")
"π"
# sigma small case
utf8_print("\u03C3")
"σ"
μ <- abs(digamma(1))
μseq <- seq(μ,3*μ,μ)
dev.new()
plot(μseq, ylab = "Diameter of apeture ( μ m)",
, xlab = "Force spaces with ~ μ \u03C0 \u03C3 \u03C0 ",
, main = "This is another Greek character with space \u03C3")
#