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

ulmd4ohb  于 2023-05-20  发布在  其他
关注(0)|答案(3)|浏览(132)

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

示例代码

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

然而,当保存.pdf时,unicode字符被转换为三个点...

尝试修复

1.我尝试在ggsave中使用cairo_pdf设备->不起作用。
1.我用这个post来绘制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
pftdvrlh

pftdvrlh1#

这在我的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")

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

jobtbby3

jobtbby32#

在unicode字符和pdf中使用ggsave()有点棘手。尝试明确发布到设备。当我使用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)

然后比较这些:

# 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()
u3r8eeie

u3r8eeie3#

欢迎您在这里查看我对类似问题的回答:https://stackoverflow.com/questions/12096152/plotting-symbols-fails-in-pdf/63214207?r=搜索结果&s=2| 0.0000#63214207
但这是你的问题的解决方案。

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

以下图片已被添加到后续康拉德鲁道夫的评论。

相关问题