Friday, January 17, 2020

Some notes on using Greek Symbols in R for variables and inserting Unicode symbols as text

From SO https://stackoverflow.com/questions/59105619/insert-specific-unicode-symbols-inside-values-of-data-frame-variable/59763650#59763650

"If you are on Windows 10 recently updated with as of April 2018 Update: Use the Windows key + '.' (e.g. hold together Windows Key plus period) in your text editor. This brings up Microsoft Emoji keyboard. Select the Greek letters variable for your script. The R Console will not accept the Greek letters as variables directly but only the from the editor script. Some of the Greek letters don't translate to English (like "µ" or "ß".) You can paste and copy them from ls() output to access. You may be able to use some math symbols as well for variable names. I can't however, get this to work with source(). That must be a text encoding problem." Click to enlarge chart.




In many true type fonts on Windows 10, you can store to Greek Symbols. Some will appear  in ls() as the english equivalent. Those like μ, Σ, β will appear as Greek symbols if the font permits this.


#not run
library(utf8)
options(digits=12)

α <- 1/137
αCODATA  <- 0.0072973525693
αQED <- 137.035999174
ε <- exp(1)
π <- pi
μ <- digamma(1)
Σ <- μ / ε
β <- beta(α,ε)
βQED <- beta(αQED,ε)
απε <- α/(π + ε)

ls()

a
aCODATA
aQED
e
p
S
ß
βQED
ape

α
αCODATA
αQED
ε
π
μ
Σ
β
βQED
απε

#pi small
utf8_print("\u03C0")
#"π"

#pi cap
utf8_print("\u03A0")
#"Π"

#sigma small
utf8_print("\u03C3")
#"σ"

#sigma cap
utf8_print("\u03A3")
#"Σ'

#XI cap
utf8_print("\u03BE")



#SO https://stackoverflow.com/questions/6044800/adding-greek-character-to-axis-title/59793390#59793390
#I give an updated answer to Chase's plot example 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: https://stackoverflow.com/questions/59105619/insert-specific-unicode-symbols-inside-values-of-data-frame-variable/59763650#59763650
#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")
#
Click to enlarge chart.



No comments:

Post a Comment