R: Usage of greek letters within captions in ggtitle [duplicate] - r

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

Related

How can I subscript and superscript my labels? I can't seem to get it working for ggdensity plots

I want to subscript the '2' in 'NO2' in the main heading and the xlabel, and I want to add '(µg/m3)' (with a superscript '3') in the xlabel, but the way I did it before for histograms doesn't work. Any help would be gratefully received!
This is what I have so far:
library(ggpubr)
ggdensity(bgbind$no2,
main = "Density plot of background NO2 concentrations",
xlab = "NO2")
You can just use the symbols directly:
ggdensity(bgbind$no2,
main = "Density plot of background NO₂ concentrations",
xlab = "NO₂ (µg/m³)")
A somewhat more portable way of doing this is using unicode escape sequences. I tend to do this by looking up, say, "unicode subscript 2" in a web search engine. This will usually give you the result in the format "U+AAAA" where "AAAA" is a four digit hexadecimal number. If you do "\uAAAA" as a string in R, this will be converted to the appropriate unicode symbol. So, for example, look at what prints in the console here:
"NO\u2082 (\u03BCg/m\u00bB)"
#> [1] "NO₂ (μg/m»)"
You can use bquote().
Here is an example with ggplot2:
library(ggplot2)
bgbind <- data.frame(
no2 = 1:10,
y = 1:10
)
ggplot(bgbind, aes(no2, y)) +
geom_point() +
labs(title = bquote("Density plot of background NO"[2] ~ "concentrations"),
x = bquote(NO[2] ~ (mu*g/m^3)))
Assuming that, by main, you meant the title of the plot.
I cannot test it as I don't have your dataset and I am unable to install the package ggpubr for some reason, but in your situation, this should work:
library(ggpubr)
ggdensity(bgbind$no2,
main = bquote("Density plot of background NO"[2] ~ "concentrations"),
xlab = bquote(NO[2] ~ (mu*g/m^3)))
Inside bquote(), [] subscripts what is inside, ^ superscripts what follows, ~ adds a space, mu is turned into the symbol micro, * juxtaposes 2 elements.

Get rid of auto spacing using bquote in r

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

Superscript R squared for 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.
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.

Adding greek character to axis title

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 to show $\{ X_t \}$ in the title of a plot of R

How to show $\{ X_t \}$ of Latex in the title of a plot of R?
For example
plot(slot(x,"GRID"),slot(x,"PATH"),type="l", xlab="Time t",ylab="X",
main=paste("Simulation of \{X_t\}"))
Thanks!
Assuming you will provide meaningful arguments for the slot expressions, then I think there is a reasonable chance that this is what you want:
plot(1:10,1:10,type="l", xlab="Time t",ylab="X",
main=expression("Simulation of {"*X[t]*"}"))
This is a plotmath expression that presents "t" as a subscript of "X" and that expression is enclosed in curley braces. If I have misread your request, then note that the "*" character is a separator in the plotmath syntax and the braces are simply characters than can be deleted. (The LaTeX expressions don't make a lot of sense to those of us who just use the plotmath syntax, so describing what you want in prose or mathematical jargon would work better for any clarifications.)
To have R emulate LaTeX typesetting, see the examples and methods described in ?plotmath.
To actually have LaTeX typeset your plot text, use the tikz() graphics device provided by the tikzDevice package.
The group() plotmath function can be used to formalise DWin's answer. This solution has the advantage (IMHO) of not using strings as part of the expression. Instead we use the ~ operator to add spacing (whitespace is ignored).
plot(1:10, 1:10, type = "l", xlab = "Time t", ylab = "X",
main=expression(Simulation ~ of ~ group("{", X[t], "}")))
The bgroup() plotmath function provides scalable delimiters but is used in the same manner as the example code above.
Edit (In response to ran2's comment):
The same approaches can be used in ggplot and lattice, using the power of plotmath, just as with base graphics. E.g.
require(ggplot2)
df <- data.frame(A = 1:10, B = 1:10)
qplot(A, B, data = df,
main = expression(Simulation ~ of ~ group("{", X[t], "}")))

Resources