根据您的系统是否具有所需的真正字体文件 [提示:安装 showtext 包并使用 View(font_files()] 您应该能够通过以下方式将可用的 unicode 字符转换为 pdf 文件
- 首先导出到一个临时文件,比如“temp.png”
- 使用 pdf() 或 cairo_pdf() 导出到 pdf 文件,比如“UnicodeToPDF.pdf”
- 结合使用 grid.arrange(来自 gridExtra)、rasterGrob(来自 grid)和 readPNG(来自 png)将 temp.png 文件插入到 UnicodeToPDF.pdf 文件中
- 删除“temp.png”文件
#--- A function to install missing packages and load them all
myfxLoadPackages = function (PACKAGES) {
lapply(PACKAGES, FUN = function(x) {
if (suppressWarnings(!require(x, character.only = TRUE))) {
install.packages(x, dependencies = TRUE, repos = "https://cran.rstudio.com/")
}
})
lapply(PACKAGES, FUN = function(x) library(x, character.only = TRUE))
}
packages = c("gridExtra","grid","png")
myfxLoadPackages(packages)
#--- The trick to get unicode characters being printed on pdf files:
#--- 1. Create a temporary file, say "temp.png"
#--- 2. Create the pdf file using pdf() or cairo_pdf(), say "UnicodeToPDF.pdf"
#--- 3. Combine the use of grid.arrange (from gridExtra), rasterGrob (from grid), and readPNG (from png) to insert the
# temp.png file into the UnicodeToPDF.pdf file
Corrvalues = data.frame(X=seq(-0.8,0.8,0.2),
Y=seq(-0.8,0.8,0.2),
PCH=-c(10122:10130)) #--- This is equivalent to using unicode characters 10122-10130 (note the use of -)
#--- Refer to http://xahlee.info/comp/unicode_index.html to see more unicode character integers
png("temp.png", width=11, height=11, units="in", res=300)
par(mar=c(4,5,3,1) + 0.1)
plot(c(-1,1), c(-1,1), bty = "n", type= "n", las = 1, cex.lab = 1.5, cex.axis = 1.25, main = NULL,
ylab=expression(paste("Correlation Coefficient (r) for ", delta ^{15},"N"," \u0028","\u2030","\u0029")),
xlab=expression(paste("Correlation Coefficient (r) for ", delta ^{13},"C"," \u0028","\u2030","\u0029")))
axis(1, at = seq(-1.0, 1.0, by = 0.1), labels = F, pos = 0, cex.axis = 0.05, tcl = 0.25)
axis(2, at = seq(-1.0, 1.0, by = 0.1), labels = F, pos = 0, cex.axis = 0.05, tcl = 0.25)
points(Corrvalues$X,Corrvalues$X,pch=Corrvalues$PCH,cex=2.75,col="#FF7F00")
dev.off()
pdf("UnicodeToPDF.pdf", width=11, height=11)
grid.arrange(
rasterGrob(
readPNG(
"temp.png",
native=F
)
)
)
dev.off()
file.remove("temp.png")
添加了以下图片以跟进 Konrad Rudolph 的评论。