9

我有以下代码来创建一个plot. 在x-屏幕上出现了,y-axes当我以该格式保存我的情节时,但当我将情节另存为.symbolsJPEGPDF

是否有替代符号\u2030可以打印在我的 PDF 中或我的问题的其他解决方案?请参阅以下正确(JPEG 格式)和不正确(PDF)图的示例。

plot(c(-1,1), c(-1,1), bty = "n", type= "n", las = 1, cex.lab = 1.5, cex.axis = 1.25, main = NULL, 
ylab=expression(paste("Correlation Coefficient (r) for ", delta ^{15},"N"," \u0028","\u2030","\u0029")), 
xlab=expression(paste("Correlation Coefficient (r) for ", delta ^{13},"C"," \u0028","\u2030","\u0029")))
axis(1, at = seq(-1.0, 1.0, by = 0.1), labels = F, pos = 0, cex.axis = 0.05, tcl = 0.25)
axis(2, at = seq(-1.0, 1.0, by = 0.1), labels = F, pos = 0, cex.axis = 0.05, tcl = 0.25)

在此处输入图像描述 在此处输入图像描述

4

4 回答 4

4

问题是您的默认字体没有“‰”(我将其称为“per mil”)作为使用 \u0028 生成的字形。您需要更改为具有该字形的字体:

?pdfFonts

这就是我在没有问题的情况下得到的设置(至少据我所知)。

> str(pdfFonts("sans"))
List of 1
 $ sans:List of 3
  ..$ family  : chr "Helvetica"
  ..$ metrics : chr [1:5] "Helvetica.afm" "Helvetica-Bold.afm" "Helvetica-Oblique.afm" "Helvetica-BoldOblique.afm" ...
  ..$ encoding: chr "default"
  ..- attr(*, "class")= chr "Type1Font"
于 2012-08-23T17:42:09.993 回答
3

您可能必须更改编码。在我的 Mac 上,这让我得到了 ‰ 符号:

pdf('test.pdf',encoding="MacRoman")
plot.new()
text(0,labels="\u2030")
dev.off()

包的“enc”目录中grDevices查找可用的编码并尝试一下。

于 2012-08-23T17:37:36.560 回答
1

我在使用 MAC 以 PDF 格式保存时遇到了同样的问题。我正在使用 ggsave 和 MacRoman 进行储蓄。

ggsave("Name_of_your_file.pdf", #choose your own name
   encoding="MacRoman",
   width = 20, #size that you want
   height = 20, #size that you want
   units = "cm")

它会将您的文件保存在您保存 R 文件的位置。

于 2020-08-02T09:01:49.430 回答
-1

根据您的系统是否具有所需的真正字体文件 [提示:安装 showtext 包并使用 View(font_files()] 您应该能够通过以下方式将可用的 unicode 字符转换为 pdf 文件

  1. 首先导出到一个临时文件,比如“temp.png”
  2. 使用 pdf() 或 cairo_pdf() 导出到 pdf 文件,比如“UnicodeToPDF.pdf”
  3. 结合使用 grid.arrange(来自 gridExtra)、rasterGrob(来自 grid)和 readPNG(来自 png)将 temp.png 文件插入到 UnicodeToPDF.pdf 文件中
  4. 删除“temp.png”文件
#--- 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("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
Corrvalues = data.frame(X=seq(-0.8,0.8,0.2),
                        Y=seq(-0.8,0.8,0.2),
                        PCH=-c(10122:10130)) #--- This is equivalent to using unicode characters 10122-10130 (note the use of -)
#--- Refer to http://xahlee.info/comp/unicode_index.html to see more unicode character integers

png("temp.png", width=11, height=11, units="in", res=300)
par(mar=c(4,5,3,1) + 0.1)
plot(c(-1,1), c(-1,1), bty = "n", type= "n", las = 1, cex.lab = 1.5, cex.axis = 1.25, main = NULL, 
     ylab=expression(paste("Correlation Coefficient (r) for ", delta ^{15},"N"," \u0028","\u2030","\u0029")), 
     xlab=expression(paste("Correlation Coefficient (r) for ", delta ^{13},"C"," \u0028","\u2030","\u0029")))
axis(1, at = seq(-1.0, 1.0, by = 0.1), labels = F, pos = 0, cex.axis = 0.05, tcl = 0.25)
axis(2, at = seq(-1.0, 1.0, by = 0.1), labels = F, pos = 0, cex.axis = 0.05, tcl = 0.25)
points(Corrvalues$X,Corrvalues$X,pch=Corrvalues$PCH,cex=2.75,col="#FF7F00")
dev.off()

pdf("UnicodeToPDF.pdf", width=11, height=11)
grid.arrange(
  rasterGrob(
    readPNG(
      "temp.png",
      native=F
    )
  )
)
dev.off()

file.remove("temp.png")

添加了以下图片以跟进 Konrad Rudolph 的评论。 在此处输入图像描述

于 2020-09-16T21:14:38.703 回答