简体   繁体   English

R图中的斜体希腊字母?

[英]Italic greek letters in R plot?

Is it possible to italicize greek letters in R plots? 是否可以将R图中的希腊字母斜体化? 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. plotmath文档非常清楚地指出,italic()不适用于希腊字母,但是,他们的确说带有\\ uxxxxx或\\ Uxxxxxxxx的UTF-8可以用于创建斜体希腊符号,但是我无法获得此符号上班。 Does anyone have any suggestions on how to do this with utf or some other way? 是否有人对如何使用utf或其他方式提出建议?

There may be some Tex solutions, but I have not had success in getting the basic Tex examples to work on my system. 可能有一些Tex解决方案,但是我没有成功获得基本的Tex示例在我的系统上工作。 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. 在顶部(#1)中,斜体符号看起来很奇怪。 In the second one the italic symbols look right but the ":" is italic. 在第二个中,斜体符号看起来正确,但是“:”是斜体。 Yes, the journal editor has caught that. 是的,期刊编辑已经抓住了这一点。

OS: windows 7 x64 操作系统: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. 最终制作了一个供将来使用的数据结构,并以“正确的方式”进行了一些搜索以制作Unicode字符( paste0("\\u\u0026quot;, 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")

So that's the data. 这就是数据。 The intToUtf8 function is the answer, and I implemented it within a bquote strategy: 答案是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)))) ) ) )

This is on a Mac, so it's possible this is not a general solution. 这是在Mac上,因此这可能不是通用解决方案。 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. 如果方便在输出管道中进行(La)TeX渲染,我强烈建议您使用tikzDevice程序包在图中进行任意(La)TeX排版。

Advantages 好处

  • typefaces etc. etc. can easily be made consistent between your text and figures 字体等,可以很容易地使您的文字和图形保持一致
  • access to all LaTeX typesetting machinery 使用所有 LaTeX排版设备

Disadvantages 缺点

  • if you don't already have a TeX system installed, it can be a huge pain 如果您尚未安装TeX系统,那将是一个巨大的痛苦
  • if you don't already know TeX and want to do complicated equations, you have to learn a new system 如果您还不了解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()

convert TeX -> PDF -> PNG 转换TeX-> PDF-> PNG

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

在此处输入图片说明

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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