简体   繁体   English

在 R 中将 unicode 字符保存为 .pdf

[英]Save unicode characters to .pdf in R

I would like to save specific unicode characters to a pdf file with ggsave .我想使用ggsave将特定的 unicode 字符保存到 pdf 文件中。

Example code示例代码

library(ggplot2)

ggplot() +
  geom_point(data = data.frame(x=1, y=1), aes(x,y), shape = "\u2191") +
  geom_point(data = data.frame(x=2, y=2), aes(x,y), shape = "\u2020")

ggsave("test.pdf", plot = last_plot()), width = 40, height = 40, units = "mm")

However, when saving the .pdf the unicode characters are transformed to three dots...但是,保存.pdf ,unicode 字符会转换为三个点...

Attempts to fix it尝试修复它

  1. I tried to use the cairo_pdf device in ggsave -> didn't work.我试图用cairo_pdf设备ggsave - >没有工作。
  2. Used this post to plot the unicode characters, but didn't quite understand it...用这篇文章绘制了unicode字符,但不太明白......

Question问题

How do I use both unicode characters in a pdf?如何在 pdf 中同时使用两个 unicode 字符?

> sessionInfo()
R version 3.6.2
Platform: x86_64-apple-darwin15.6.0 (64-bit)
Running under: macOS Catalina 10.15.5

This seems to work on my mac:这似乎适用于我的 Mac:

library(tidyverse)

quartz(type = 'pdf', file = 'test.pdf')

ggplot() +
    geom_point(data = data.frame(x=1, y=1), aes(x,y), shape = "\u2191") +
    geom_point(data = data.frame(x=2, y=2), aes(x,y), shape = "\u2020")

Using the suggestion from here: https://stackoverflow.com/a/44548861/1827使用这里的建议: https : //stackoverflow.com/a/44548861/1827

It's a bit touchy using ggsave() with unicode characters and pdfs.使用带有 unicode 字符和 pdf 的ggsave()有点棘手。 Try to explicitly post to the device.尝试明确发布到设备。 It does not work for me when I use pdf() , but using cairo_pdf() worked.当我使用pdf()时它对我不起作用,但使用cairo_pdf()工作。

p <- ggplot() +
  geom_point(data = data.frame(x=1, y=1), aes(x,y), shape = "\u2191", size=4) +
  geom_point(data = data.frame(x=2, y=2), aes(x,y), shape = "\u2020", size=4)

Then compare these:然后比较这些:

# using pdf() gives me warnings and does not work
pdf('test.pdf')
print(p)
dev.off()

# using cairo_pdf() works
pdf('test_cairo.pdf')
print(p)
dev.off()

You are welcome to check my answer to a similar question here: https://stackoverflow.com/questions/12096152/plotting-symbols-fails-in-pdf/63214207?r=SearchResults&s=2|0.0000#63214207欢迎您在这里查看我对类似问题的回答: https : //stackoverflow.com/questions/12096152/plotting-symbols-fails-in-pdf/63214207?r=SearchResults&s=2|0.0000#63214207

But here is the solution for your problem.但这是您问题的解决方案。

#--- 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")

The following image has been added to follow up on Konrad Rudolph's comments.添加了以下图像以跟进 Konrad Rudolph 的评论。 在此处输入图片说明

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM