将unicode字符保存到R中的.pdf中

将unicode字符保存到R中的.pdf中

欢迎您查看我对以下类似问题的回答:https://stackoverflow.com/questions/12096152/plotting-symbols-fails-in-pdf/63214207?r=SearchResults&s=2|0.0000#63214207

但这是解决你问题的办法。

代码语言:javascript运行复制#--- 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("ggplot2","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

test.plot = ggplot() +

geom_point(data = data.frame(x=1, y=1), aes(x,y), shape = "\u2191", size=3.5) +

geom_point(data = data.frame(x=2, y=2), aes(x,y), shape = "\u2020", size=3.5) +

geom_point(data = data.frame(x=1.2, y=1.2), aes(x,y), shape = -10122, size=3.5, color="#FF7F00") +

geom_point(data = data.frame(x=1.4, y=1.4), aes(x,y), shape = -129322, size=3.5, color="#FB9A99") +

geom_point(data = data.frame(x=1.7, y=1.7), aes(x,y), shape = -128515, size=5, color="#1F78B4")

ggsave("temp.png", plot = test.plot, width = 80, height = 80, units = "mm")

#--- Refer to http://xahlee.info/comp/unicode_index.html to see more unicode character integers

pdf("UnicodeToPDF.pdf")

grid.arrange(

rasterGrob(

readPNG(

"temp.png",

native=F

)

)

)

dev.off()

file.remove("temp.png")以下图片已被添加,以跟进康拉德鲁道夫的评论。

相关推荐

韋伯拍攝到最清晰的海王星行星環圖像
365游戏中心正式版

韋伯拍攝到最清晰的海王星行星環圖像

📅 09-14 👁️ 6451
小米手机闹钟关闭指南
365betapp

小米手机闹钟关闭指南

📅 10-04 👁️ 3121
您所访问的页面不存在
365游戏中心正式版

您所访问的页面不存在

📅 07-27 👁️ 5803
三星GALAXYA9
bt365账户为什么封

三星GALAXYA9

📅 10-12 👁️ 9883
移动电话机商品修理更换退货责任规定
365betapp

移动电话机商品修理更换退货责任规定

📅 07-30 👁️ 5702
《盗墓笔记》为什么有七个吴邪,齐羽和吴邪的身份解密
bt365账户为什么封

《盗墓笔记》为什么有七个吴邪,齐羽和吴邪的身份解密

📅 07-15 👁️ 8141