Adding greek character to axis title

RUnicodeLabelsPlotmath

R Problem Overview


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.

R Solutions


Solution 1 - R

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?

Solution 2 - R

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

enter image description here

Solution 3 - R

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.

enter image description here

Solution 4 - R

This should be much more straight forward with latex2exp:

require(latex2exp)
plot(1, xlab = TeX('$\\mu$'))

Solution 5 - R

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.

Solution 6 - R

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

enter image description here

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionRitaView Question on Stackoverflow
Solution 1 - RNick SabbeView Answer on Stackoverflow
Solution 2 - RChaseView Answer on Stackoverflow
Solution 3 - RLaryx DeciduaView Answer on Stackoverflow
Solution 4 - ROmar WagihView Answer on Stackoverflow
Solution 5 - RemagarView Answer on Stackoverflow
Solution 6 - RrferrisxView Answer on Stackoverflow