ggplot2 PDF 输出中的 Unicode 字符 [英] Unicode Characters in ggplot2 PDF Output
问题描述
如何在使用 ggplot2 创建的 PDF 图中使用 Unicode 字符作为标签、标题和类似内容?
考虑以下示例:
库(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 方法基础
在寻找这个问题的解决方案时,我发现了一些;虽然它看起来与标题没有直接关系,但里面有很多关于让字体在 R 中做你想做的事情.
EDIT 每个评论中的请求,这里是特定于 Windows 的代码:
库(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()
要使用基本图形命令 cairo_pdf()
,只需首先使用 windowsFonts()
命令定义您的字体系列就足够了,如上所示.当然,请确保您使用的是系统上实际拥有的字体,并且该字体实际上具有您需要的所有字形.
以下注释中有关 DLL 文件的说明是我在 library(Cairo) 中获取
在 Windows 上工作.然后:Cairo()
和 CairoPDF()
命令所需的)
库(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 字符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!