Adding greek character to axis title - 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.

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

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

In R, why is there awkward output in the legend when I am using paste() instead of c() in addition to pretty10exp()?

I'm trying to make the legend of this plot pretty, so I need there the be an actual superscript, which is why I am using the pretty10exp() function from the sfsmisc library. It works when I use the c() function.
However, I am also trying to keep the string and the scientific notation number on the same line. The legend() is broken into two lines, which I think is due to c(). I thought I could use paste(), but for some reason the output is now incorrect.
plot(1:12)
pVal <- 4
legend("topright", legend = c("P value:", sfsmisc::pretty10exp(pVal)), cex = 1.5)
legend("topright", legend = paste("P value:", sfsmisc::pretty10exp(pVal)), cex = 1.5)
pVal being an arbitrary number represented in scientific notation. The second line results in output like this: "P value: (significand) %*% 10^-4". The first line also doesn't give me what I want. How can I fix this problem?
pretty10exp returns an expression which allows it to use the ?plotmath features for making nice looking numbers. When working with expressions, you can't just paste values in like strings. You need to manipulate them with a special set of functions. One such function is substitute. You can do
plot(1:12)
pVal <- 4
legend("topright", cex = 1.5,
legend = substitute("P value: "*x, list(x=sfsmisc::pretty10exp(pVal)[[1]])) )
We use substitute() to take the value contained in the expression from pretty10exp and prefix it with the label you want. (We use * to concatenate rather than paste() since plotmath allows it)
This is what I would do:
fun <- function(text, pVal) {
y <- floor(log10(pVal))
x <- pVal / 10^y
bquote(.(text)*":" ~ .(x) %.% 10 ^ .(y))
}
plot.new()
text(0.5,0.7,fun("P value", 0.4))
text(0.5, 0.3, fun("P value", signif(1/pi, 1)))
No package is needed.

How to remove automatic space after greek lettters using the cairo library?

I am using expression() to create a string that includes greek letters. My legend label is as follows:
legend.label = paste(mu, "+", sigma, ", ", mu, ", ", mu, "+", sigma, " of
random strategy", sep ="")
This should give me something like: μ+σ, μ, μ+σ of random strategy, but instead I am getting this:
It seems as if there is an automatic space after the greek letters. I already tried using the * and ~ operator, but it didnt help. I am using the cairo package to create anti-anialised plots. However, without using the cairo package I seem to get the right result. Does anybody have an idea how to fix this? Thanks!
I don't know how you defined the "greek letters" but you can use the unicode to put them without space in your legend:
plot(0:1, 0:1, type="none", main="\U03BC+\U03C3, \U03BC, \U03BC+\U03C3 of
random strategy")
The way to do this is described in help("plotmath"):
plot(1)
legend("top", legend = expression(paste(mu + sigma,
", ",
mu,
", ",
mu + sigma,
" of random strategy")),
lty = 1, col = "red", bty = "n")

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

Resources