• R语言
  • ggsave()或Rstudio生成的pdf中默认对"-"的处理方式,引起的一些不方便。

  • 已编辑

从ggsave()或Rstudio生成的pdf以Ctrl + c复制"−" ,以为复制的是"-" (U+002D),而实际上复制的是"−" (U+2212)。文字输入时,因为"-" (U+002D)更容易输入和常用,即减号,ggsave()或Rstudio默认的行为有些不便。

"-" (U+002D)或"−" (U+2212)在Rstudio的图形窗口可以区分开,但是保存为pdf后就无法区分两者(保存为图片后也可以区分两者)。
在pdf中显示的都是"−" (U+2212),也就是相当于ggsave()或Rstudio生成的pdf强制将"-" (U+002D)转为了"−" (U+2212)。
此外,在ggsave()或Rstudio生成的pdf中,"-" (U+002D)或"−" (U+2212)可以相互搜索。
然而,将ggsave()或Rstudio生成的pdf中"−" (U+2212)从pdf复制到文本文档或用adobe illustrator打开pdf后,就无法用"-" (U+002D)搜到"−" (U+2212)。
除了用cairo_pdf()可以完美解决上述问题,ggsave()或Rstudio还可以通过其他方式解决吗?

library(ggplot2)

df <- data.frame(x = c(-1, 0), y = c(-1, 2))

p <- ggplot(df, aes(x, y)) +
  geom_point() +
  labs(title = "Title with '−' \u2212 hyphen sign or '-' \u002D minus sign") +
  theme(title = element_text(size = 8))

ggsave("ggplot_style.pdf", p)

cairo_pdf("cairo_style.pdf")
p
dev.off()

From grDevices::pdf:

There is an exception. Character 45 ("-") is always set as minus (its value in Adobe ISOLatin1) even though it is hyphen in the other encodings. Hyphen is available as character 173 (octal 0255) in all the Latin encodings, Cyrillic and Greek. (This can be entered as "\u00ad" in a UTF-8 locale.) There are some discrepancies in accounts of glyphs 39 and 96: the supplied encodings (except CP1250 and CP1251) treat these as ‘quoteright’ and ‘quoteleft’ (rather than ‘quotesingle’/‘acute’ and ‘grave’ respectively), as they are in the Adobe documentation.

所以解决方案就是使用原生 PDF 设备时写 \u00ad,或者直接使用 Cairo 设备:

pdf("native_pdf.pdf")
plot(0:9, main = "Title with -")
dev.off()

cairo_pdf("native_cairo.pdf")
plot(0:9, main = "Title with -")
dev.off()

pdf("hyphen_pdf.pdf")
plot(0:9, main = "Title with \u00ad")
dev.off()

    nan.xiao
    感谢。原来pdf()说明里面就有写原因。网上没找到答案,在deepseek换好几个角度问,最后才给出cairo_pdf()这个正确答案。