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

use*_*544 3 unicode r ggplot2

我想将特定的 unicode 字符保存到 .pdf 文件中ggsave

示例代码

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")
Run Code Online (Sandbox Code Playgroud)

但是,在保存.pdfunicode 字符时,将其转换为三个点...

尝试修复它

  1. 我试图cairo_pdfggsave-> 中使用该设备没有工作。
  2. 用这篇文章绘制了unicode字符,但不太明白......

如何在 pdf 中同时使用两个 unicode 字符?

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

Sha*_*baz 5

这似乎适用于我的 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")
Run Code Online (Sandbox Code Playgroud)

使用这里的建议:https : //stackoverflow.com/a/44548861/1827


che*_*123 5

ggsave()使用unicode 字符和 pdf有点棘手。尝试明确发布到设备。当我使用时它对我不起作用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)
Run Code Online (Sandbox Code Playgroud)

然后比较这些:

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

# using cairo_pdf() works
cairo_pdf('test_cairo.pdf')
print(p)
dev.off()
Run Code Online (Sandbox Code Playgroud)