R情节中的斜体希腊字母?

时间:2014-09-08 20:50:27

标签: r

是否可以将R图中的希腊字母斜体化?理想情况下,以下代码会产生斜体希腊字母:

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

plotmath文档非常明确地说italic()不会为希腊字母工作,但是,他们确实说UTF-8与\ uxxxxx或\ Uxxxxxxxx可用于创建斜体希腊符号,但是我有一直无法让这个工作。有没有人有任何关于如何用utf或其他方式做这个的建议?

可能有一些Tex解决方案,但我没有成功地将基本的Tex示例用于我的系统。我向他们开放,但我可能会问一些后续问题。

以下代码几乎绘制了我想要的内容,但并不完全。

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

enter image description here

我需要斜体符号和非斜体":"。在顶部(#1)中,斜体符号看起来很奇怪。在第二个中,斜体符号看起来正确但是":"是斜体。是的,期刊编辑抓住了这一点。

操作系统:Windows 7 x64

2 个答案:

答案 0 :(得分:4)

我挣扎着这个。最后制作一个数据结构供将来使用,并在“正确的方式”上进行搜索,以制作Unicode字符(以paste0("\u", n)的各种方式失败,我认为这将是聪明的解决方法。

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

这就是数据。 intToUtf8函数就是答案,我在bquote策略中实现了它:

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

这是在Mac上,所以这可能不是一般解决方案。这就是我在设备上看到的内容:

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

enter image description here

答案 1 :(得分:0)

如果在输出管道中具有(La)TeX渲染很方便,我强烈建议您使用tikzDevice包在图形中进行任意(La)TeX排版。 / p>

优势

  • 字体等,可以很容易地使您的文字和图形保持一致
  • 使用所有 LaTeX排版设备

缺点

  • 如果您尚未安装TeX系统,那将是一个巨大的痛苦
  • 如果您还不了解TeX并想做复杂的方程式,则必须学习一个新系统
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()

转换TeX-> PDF-> PNG

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

enter image description here