简体   繁体   中英

Italic greek letters in R plot?

Is it possible to italicize greek letters in R plots? Ideally the following code would result in italic greek letters:

plot(1:10, 1:10, type="n")
text(8,4, expression(italic(gamma)~":"~italic(sigma))) #doesn't work...
text(2,2,expression(italic("this")~":"~italic("that")))

The plotmath documentation very clearly says that italic() won't work for greek letters, however, they do say that UTF-8 with \\uxxxxx or \\Uxxxxxxxx can be used to create italic greek symbols, however I have been unable to get this to work. Does anyone have any suggestions on how to do this with utf or some other way?

There may be some Tex solutions, but I have not had success in getting the basic Tex examples to work on my system. I am open to them, but I may ask some follow-up questions.

The below code almost plots what I want, but not quite.

windows(family='serif') #name for Times in default windows gr device lib
plot(1:10, 1:10, type="n")
text(5,9, expression(italic(β)~":"~italic(α))) #1
text(5,7, paste("\u03B2",":","\u03B1",sep=" "), font=3) #2
text(5,5, expression(italic(beta)~":"~italic(alpha))) #3
text(5,3, expression(italic("\u03B2")~":"~italic("\u03B1"))) #4
text(5,1.5, expression(italic("\u03B2")~plain(":")~italic("\u03B1"))) #5

在此处输入图片说明

I need italic symbols and a non-italic ":". In the top one (#1), the italic symbols look odd. In the second one the italic symbols look right but the ":" is italic. Yes, the journal editor has caught that.

OS: windows 7 x64

I struggled with this one. Ended up making a data structure for future use and doing some searching on the "right way" to make Unicode characters (having failed with in various ways with paste0("\\u\u0026quot;, n) and what I thought would be clever workarounds.

greeks <- 
structure(list(grsym = c("α", "β", "γ", "δ", "ε", "ζ", 
"η", "θ", "ι", "κ", "λ", "μ", "ν", "ξ", "ο", "π", "ρ", 
"ς", "σ", "τ", "υ", "φ", "χ", "ψ", "ω", "Α", "Β", "Γ", 
"Δ", "Ε", "Ζ", "Η", "Θ", "Ι", "Κ", "Λ", "Μ", "Ν", "Ξ", 
"Ο", "Π", "Ρ", "Σ", "Τ", "Υ", "Φ", "Χ", "Ψ", "Ω"), 
    decUTF = c(945, 946, 947, 948, 949, 950, 951, 952, 953, 954, 
    955, 956, 957, 958, 959, 960, 961, 962, 963, 964, 965, 966, 
    967, 968, 969, 913, 914, 915, 916, 917, 918, 919, 920, 921, 
    922, 923, 924, 925, 926, 927, 928, 929, 931, 932, 933, 934, 
    935, 936, 937), hexUTF = structure(c(945L, 946L, 947L, 948L, 
    949L, 950L, 951L, 952L, 953L, 954L, 955L, 956L, 957L, 958L, 
    959L, 960L, 961L, 962L, 963L, 964L, 965L, 966L, 967L, 968L, 
    969L, 913L, 914L, 915L, 916L, 917L, 918L, 919L, 920L, 921L, 
    922L, 923L, 924L, 925L, 926L, 927L, 928L, 929L, 931L, 932L, 
    933L, 934L, 935L, 936L, 937L), class = "hexmode"), htmlSym = c("&alpha;", 
    "&beta;", "&gamma;", "&delta;", "&epsilon;", "&zeta;", "&eta;", 
    "&theta;", "&iota;", "&kappa;", "&lambda;", "&mu;", "&nu;", 
    "&xi;", "&omicron;", "&pi;", "&rho;", "&sigmaf;", "&sigma;", 
    "&tau;", "&upsilon;", "&phi;", "&chi;", "&psi;", "&omega;", 
    "&Alpha;", "&Beta;", "&Gamma;", "&Delta;", "&Epsilon;", "&Zeta;", 
    "&Eta;", "&Theta;", "&Iota;", "&Kappa;", "&Lambda;", "&Mu;", 
    "&Nu;", "&Xi;", "&Omicron;", "&Pi;", "&Rho;", "&Sigma;", 
    "&Tau;", "&Upsilon;", "&Phi;", "&Chi;", "&Psi;", "&Omega;"
    ), Description = c("GREEK SMALL LETTER ALPHA", "GREEK SMALL LETTER BETA", 
    "GREEK SMALL LETTER GAMMA", "GREEK SMALL LETTER DELTA", "GREEK SMALL LETTER EPSILON", 
    "GREEK SMALL LETTER ZETA", "GREEK SMALL LETTER ETA", "GREEK SMALL LETTER THETA", 
    "GREEK SMALL LETTER IOTA", "GREEK SMALL LETTER KAPPA", "GREEK SMALL LETTER LAMBDA", 
    "GREEK SMALL LETTER MU", "GREEK SMALL LETTER NU", "GREEK SMALL LETTER XI", 
    "GREEK SMALL LETTER OMICRON", "GREEK SMALL LETTER PI", "GREEK SMALL LETTER RHO", 
    "GREEK SMALL LETTER FINAL SIGMA", "GREEK SMALL LETTER SIGMA", 
    "GREEK SMALL LETTER TAU", "GREEK SMALL LETTER UPSILON", "GREEK SMALL LETTER PHI", 
    "GREEK SMALL LETTER CHI", "GREEK SMALL LETTER PSI", "GREEK SMALL LETTER OMEGA", 
    "GREEK CAPITAL LETTER ALPHA", "GREEK CAPITAL LETTER BETA", 
    "GREEK CAPITAL LETTER GAMMA", "GREEK CAPITAL LETTER DELTA", 
    "GREEK CAPITAL LETTER EPSILON", "GREEK CAPITAL LETTER ZETA", 
    "GREEK CAPITAL LETTER ETA", "GREEK CAPITAL LETTER THETA", 
    "GREEK CAPITAL LETTER IOTA", "GREEK CAPITAL LETTER KAPPA", 
    "GREEK CAPITAL LETTER LAMBDA", "GREEK CAPITAL LETTER MU", 
    "GREEK CAPITAL LETTER NU", "GREEK CAPITAL LETTER XI", "GREEK CAPITAL LETTER OMICRON", 
    "GREEK CAPITAL LETTER PI", "GREEK CAPITAL LETTER RHO", "GREEK CAPITAL LETTER SIGMA", 
    "GREEK CAPITAL LETTER TAU", "GREEK CAPITAL LETTER UPSILON", 
    "GREEK CAPITAL LETTER PHI", "GREEK CAPITAL LETTER CHI", "GREEK CAPITAL LETTER PSI", 
    "GREEK CAPITAL LETTER OMEGA")), .Names = c("grsym", "decUTF", 
"hexUTF", "htmlSym", "Description"), row.names = c(NA, -49L), class = "data.frame")

So that's the data. The intToUtf8 function is the answer, and I implemented it within a bquote strategy:

start_e <- expression()   # Define the class of the expression vector.
plot(5,5)
text(2+(1:5),4, sapply( greeks[1:5,3],
                        function(x) c(start_e, bquote(italic(.(intToUtf8(x)))) ) ) )

This is on a Mac, so it's possible this is not a general solution. This is what I see on my device:

png(width=480, height=200); plot(1:10, 1:10, type="n")
text(5,6, expression(italic(β)~":"~italic(α))) 
text(5,3, expression(italic("\u03B2")~plain(":")~italic("\u03B1")) );dev.off()

在此处输入图片说明

If it's convenient to have (La)TeX rendering in your output pipeline, I would strongly recommend using the tikzDevice package to get arbitrary (La)TeX typesetting in your figures.

Advantages

  • typefaces etc. etc. can easily be made consistent between your text and figures
  • access to all LaTeX typesetting machinery

Disadvantages

  • if you don't already have a TeX system installed, it can be a huge pain
  • if you don't already know TeX and want to do complicated equations, you have to learn a new system
library(tikzDevice)
tikz("plot.tex",standAlone=TRUE)
plot(1:4, 1:4, type="n")
text(1,3, "$\\gamma:\\sigma$") #doesn't work...
text(2,2,"\\textit{this}:\\textit{that}")
dev.off()

convert TeX -> PDF -> PNG

system("pdflatex plot.tex")
system("convert plot.pdf plot.png")

在此处输入图片说明

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM