R图中的Unicode字符-pdf设备

时间:2019-05-08 10:01:05

标签: r pdf plot unicode encoding

我想绘制一个图,该图的轴标题以thin space作为千位分隔符,并将该图另存为R中的pdf。例如,我将格式化这样的数字:

string <- "100\u2009000"
string
[1] "100 000"

R控制台在此处显示正确的瘦字符“ \ u2009”表示形式(尽管在网络上看起来像是一个完整的空格)然后,如果我将其绘制并发送给这样的pdf文件:

pdf("foo.pdf")
plot(1,1, axes = FALSE, xlab = "", ylab = "", col = "#FFFFFF")
text(1, 1, string)
dev.off()

我收到一堆警告:

Warning messages:
1: In text.default(1, 1, string) :
  conversion failure on '100 000' in 'mbcsToSbcs': dot substituted for <e2>
2: In text.default(1, 1, string) :
  conversion failure on '100 000' in 'mbcsToSbcs': dot substituted for <80>
3: In text.default(1, 1, string) :
  conversion failure on '100 000' in 'mbcsToSbcs': dot substituted for <89>
4: In text.default(1, 1, string) :
  font metrics unknown for Unicode character U+2009
5: In text.default(1, 1, string) :
  conversion failure on '100 000' in 'mbcsToSbcs': dot substituted for <e2>
6: In text.default(1, 1, string) :
  conversion failure on '100 000' in 'mbcsToSbcs': dot substituted for <80>
7: In text.default(1, 1, string) :
  conversion failure on '100 000' in 'mbcsToSbcs': dot substituted for <89>

在生成的pdf中,数字看起来像这样:100...000

我看到我可以给pdf设备提供编码参数,但是我没有设法使它生效。

2 个答案:

答案 0 :(得分:1)

您可以改用cairo_pdf设备:

string <- "100\u2009000"
string

cairo_pdf("foo.pdf")
plot(1,1, axes = FALSE, xlab = "", ylab = "", col = "#FFFFFF")
text(1, 1, string)
dev.off()

这对我有用,并生成了pdf。

答案 1 :(得分:1)

您还可以使用showtext渲染文本,如果使用PDF,可以保证生成矢量图形。不过,文字会转换为形状,这意味着您不能再将它们选择为文字。

library(showtext)
showtext_auto()

string <- "100\u2009000"
string

pdf("bar.pdf")
plot(1, 1, axes = FALSE, xlab = "", ylab = "", col = "#FFFFFF")
text(1, 1, string, family = "wqy-microhei")
dev.off()
相关问题