PDF输出中的Unicode字符

PDF输出中的Unicode字符

本文介绍了ggplot2 PDF输出中的Unicode字符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何在使用ggplot2创建的PDF图中使用Unicode字符作为标签,标题和类似的东西?



请考虑以下示例:

  library(ggplot2)
qplot(Sepal.Length,Petal.Length,data = iris,main =Aʙᴄᴅᴇғɢʜɪᴊᴋʟᴍɴᴏᴘǫʀsᴛᴜᴠᴡxʏᴢ)
ggsave(t.pdf)

标题使用Unicode字符(小写字母),它在输出中显示为 ... 。这个问题只发生在pdf图上;如果我用 ggsave(t.png)替换最后一行,那么输出结果与预期的一样。

我在做什么错了?我使用的R脚本是UTF-8编码。一些系统信息:

  R版本2.14.1(2011-12-22)
平台:x86_64-pc- linux-gnu(64位)

语言环境:
[1] LC_CTYPE = en_US.UTF-8 LC_NUMERIC = C
[3] LC_TIME = en_US.UTF-8 LC_COLLATE = en_US.UTF-8
[5] LC_MONETARY = en_US.UTF-8 LC_MESSAGES = en_US.UTF-8
[7] LC_PAPER = C LC_NAME = C
[9] LC_ADDRESS = C LC_TELEPHONE = C
[11] LC_MEASUREMENT = en_US.UTF-8 LC_IDENTIFICATION = C

附加基础包:
[1] stats graphics grDevices utils datasets methods base

当为这个问题寻找解决方案时,我发现了一些;虽然它与标题看起来没有直接关系,但在那里有很多关于让字体在R中做你想做的事情。



编辑

($ g $ p




$ b $ windowFonts(myCustomWindowsFontName = windowsFont(DejaVu Sans))
cairo_pdf(example.pdf,family =myCustomWindowsFontName)
qplot(Sepal.Length,Petal.Length,data = iris,main =Aʙᴄᴅᴇғɢʜɪᴊᴋʟᴍɴᴏᴘǫʀsᴛᴜᴠᴡxʏᴢ )
dev.off()

要使用基本图形命令 cairo_pdf()如上所示,首先用 windowsFonts()命令定义字体系列就足够了。当然,请确保您使用的字体实际上已经在您的系统上,并且实际上包含您需要的所有字形。



T有关DLL文件的说明下面的评论是我必须做的,以在<$ c $中获得 Cairo() CairoPDF()命令(开罗)在Windows上工作。然后:

 库(ggplot2)
库(开罗)
windowsFonts(myCustomWindowsFontName = windowsFont(DejaVu Sans))
CairoPDF(example.pdf)
par(family =myCustomWindowsFontName)
qplot(Sepal.Length,Petal.Length,data = iris,main =Aʙᴄᴅᴇғɢʜɪᴊᴋʟᴍɴᴏᴘǫʀsᴛᴜᴠᴡxʏᴢ )
dev.off()


How can I use Unicode characters for labels, titles and similar things in a PDF plot created with ggplot2?

Consider the following example:

library(ggplot2)
qplot(Sepal.Length, Petal.Length, data=iris, main="Aʙᴄᴅᴇғɢʜɪᴊᴋʟᴍɴᴏᴘǫʀsᴛᴜᴠᴡxʏᴢ")
ggsave("t.pdf")

The title of the plot uses Unicode characters (small caps), which in the output appear as .... The problem occurs only with pdf plots; if I replace the last line with ggsave("t.png"), then the output is as expected.

What am I doing wrong? The R script I have is in UTF-8 encoding. Some system information:

R version 2.14.1 (2011-12-22)
Platform: x86_64-pc-linux-gnu (64-bit)

locale:
 [1] LC_CTYPE=en_US.UTF-8       LC_NUMERIC=C
 [3] LC_TIME=en_US.UTF-8        LC_COLLATE=en_US.UTF-8
 [5] LC_MONETARY=en_US.UTF-8    LC_MESSAGES=en_US.UTF-8
 [7] LC_PAPER=C                 LC_NAME=C
 [9] LC_ADDRESS=C               LC_TELEPHONE=C
[11] LC_MEASUREMENT=en_US.UTF-8 LC_IDENTIFICATION=C

attached base packages:
[1] stats     graphics  grDevices utils     datasets  methods   base

When searching for a solution for this problem, I found some evidence that R uses a single-byte encoding for mutli-byte encodigns such as UTF-8 for PDF or postscript output. I also found suggestions to, for instance, be able to get the Euro sign working, but no general solution.

解决方案

As Ben suggested, cairo_pdf() is your friend. It also allows you to embed non-postscript fonts (i.e. TTF/OTF) in the PDF via the family argument (crucial if you don't happen to have any postscript fonts that contain the glyphs you want to use). For example:

library(ggplot2)
cairo_pdf("example.pdf", family="DejaVu Sans")
qplot(Sepal.Length, Petal.Length, data=iris, main="Aʙᴄᴅᴇғɢʜɪᴊᴋʟᴍɴᴏᴘǫʀsᴛᴜᴠᴡxʏᴢ")
dev.off()

...gives a PDF that looks like this:

See also this question; though it doesn't look directly relevant from the title, there is a lot in there about getting fonts to do what you want in R.

EDIT per request in comments, here is the windows-specific code:

library(ggplot2)
windowsFonts(myCustomWindowsFontName=windowsFont("DejaVu Sans"))
cairo_pdf("example.pdf", family="myCustomWindowsFontName")
qplot(Sepal.Length, Petal.Length, data=iris, main="Aʙᴄᴅᴇғɢʜɪᴊᴋʟᴍɴᴏᴘǫʀsᴛᴜᴠᴡxʏᴢ")
dev.off()

To use the base graphics command cairo_pdf() it should suffice to just define your font family with the windowsFonts() command first, as shown above. Of course, make sure you use a font that you actually have on your system, and that actually has all the glyphs that you need.

TThe instructions about DLL files in the comments below are what I had to do to get the Cairo() and CairoPDF() commands in library(Cairo) to work on Windows. Then:

library(ggplot2)
library(Cairo)
windowsFonts(myCustomWindowsFontName=windowsFont("DejaVu Sans"))
CairoPDF("example.pdf")
par(family="myCustomWindowsFontName")
qplot(Sepal.Length, Petal.Length, data=iris, main="Aʙᴄᴅᴇғɢʜɪᴊᴋʟᴍɴᴏᴘǫʀsᴛᴜᴠᴡxʏᴢ")
dev.off()

这篇关于ggplot2 PDF输出中的Unicode字符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-15 09:39