将希腊字符添加到轴标题

68

我想在R中的条形图的y轴上添加一个希腊字符。
问题是我需要将此字符集成到标题中。我想要写:

Diameter of aperture ("mu"m)

在坐标轴的标签中。

使用

ylab=expression()

我可以写希腊字符,只需使用

ylab="axis title"

我可以用适当的空格写出标题。

但是我找不到一种方法将所有这些组合在一起,并编写一个带有希腊单词的轴标签。我希望我表述清楚了。

6个回答

67
如果你正在使用plotmath{grDevices},主帮助页面(plotmath)中包含了一个你似乎要的例子:
xlab = expression(paste("Phase Angle ", phi))

或者针对您的情况,我猜测:

ylab = expression(paste("Diameter of aperture ( ", mu, " )"))

这对你有用吗?


44

我认为我正确地理解了你的问题。在调用expression()时,~会强制在字符之间添加空格。这是你想要的吗?

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

在此输入图片描述


17

如果您想在文本中替换变量,请使用bquote。例如,如果您有一个变量mu并希望在标题中显示它,则可以使用以下习语:

mu <- 2.8
plot(1:3, main=bquote(mu == .(mu)))

被包含在 .() 中的部分将会被替换,这样就会打印出 mu 的值而不是希腊字母"mu"。请查看 R 中 bquote 帮助文档获取更多细节。

输入图片说明


这太完美了!正是我所寻找的。 - mikey

9
这应该会更加直观易懂,使用latex2exp即可实现:
require(latex2exp)
plot(1, xlab = TeX('$\\mu$'))

3
如果你习惯于使用Latex排版,那么这比expression更加易用。谢谢。 - ClimateUnboxed

4

如果您处理的是估计量,plotmath{grDevices}还提供了在希腊字母上添加帽子的可能性:

ylab = expression(paste("Diameter of aperture ( ", hat(mu), " )"))
hat() 中括号内的 mu 是关键。

0

我在使用Windows 10和适当的TrueType字体以及R 3.62上的utf8包时,对Chase在2011年提供的绘图示例进行了更新。在我的答案中,我为μ分配了一个值。我只是通过它们的十六进制(??)代码来调用Unicode字符。空格只是引号内的“空格”。请查看我的答案这里

另请参阅: 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


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接