绘制符号在PDF中失败

时间:2012-08-23 16:25:05

标签: r pdf plot symbols

我有以下代码来创建plot。在x - 和y-axes屏幕上显示symbolsJPEG当我以该格式保存我的情节时,但是当我保存情节时不会作为PDF

我的\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)

enter image description here enter image description here

4 个答案:

答案 0 :(得分:4)

问题是你的默认字体没有“‰”(我会说“每米尔”)作为\ 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"

答案 1 :(得分:3)

您可能需要更改编码。在我的Mac上,这让我得到了‰标志:

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

查看包grDevices的'enc'目录中的可用编码并试用它们。

答案 2 :(得分: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文件的位置保存您的文件。

答案 3 :(得分:-1)

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

  1. 首先导出到临时文件,例如“ temp.png”
  2. 使用pdf()或cairo_pdf()导出到pdf文件,说“ UnicodeToPDF.pdf”
  3. 结合使用grid.arrange(来自gridExtra),rasterGrob(来自网格)和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的评论。 enter image description here