我有以下代码来创建plot
。在x
- 和y-axes
屏幕上显示symbols
,JPEG
当我以该格式保存我的情节时,但是当我保存情节时不会作为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)
答案 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文件
#--- 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")