本文介绍了在 R 的 expression() 命令中使用 Unicode的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在 R 图中使用 expression() 以获得斜体文本.但看起来好像我不能在 ASCII 字符之外的 expression 内使用 Unicode 符号.有什么办法可以解决这个问题吗?我的目标是在我的 R 条形图中获得各种标签中的 fi 连字(以及斜体文本).

I'm using expression() in R plots in order to get italicized text. But it appears as if I cannot use Unicode symbols inside expression outside of ASCII characters. Is there some way I can work around this? My goal is to get the fi ligature in various labels in my R barplots (together with italicized text).

我在 Windows 3.0.2 版中使用 R.

I'm using R for Windows version 3.0.2.

CairoPDF(file = "Ligature1.pdf")
plot.new()
text(x =.5, y = .5, labels = "fi", family = "Times New Roman")
dev.off()
CairoPDF(file = "Ligature2.pdf")
plot.new()
text(x =.5, y = .5, labels = expression(paste(italic(m), "u", "fi", italic(m), sep = "")), family = "Times New Roman")
dev.off()

推荐答案

您要求解决方案.这就是全部.斜体部分需要表达式,但是fi"部分不需要,所以分开打印.

You asked for a work-around. That is all this is. The italic part needs expression, but the "fi" part does not, so print them separately.

plot.new()
offset1 = strwidth(expression(paste(italic(m), "u")), units="figure")
text(x =.5, y = .5, labels = expression(paste(italic(m), "u", sep="")))
text(x =.5+offset1, y = .5, labels ="fi")
offset2 = strwidth("fi ")
text(x =.5+offset1+offset2, y = .5, labels = expression(italic(m)))

但是请注意,fi"的间距有些不太正确,因此当我在屏幕上计算宽度时,我欺骗并计算了fi"的宽度(额外留了一个空格).没有额外的间距,第二个 italic(m) 与fi"重叠.

But notice that something is not quite right about the spacing of the "fi", so when I computed the width on the screen, I cheated and computed the width of "fi " (with an extra blank). Without the extra spacing, the second italic(m) overlapped the "fi".

不漂亮,但它在 Windows 下产生了想要的结果.

Not pretty, but it produces the desired result under Windows.

这篇关于在 R 的 expression() 命令中使用 Unicode的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-30 00:44