corrupted UTF characters in pdf plots generated by R

女生的网名这么多〃 提交于 2021-02-10 14:48:52

问题


I am trying to save plots with legends containing UTF characters for Males and Females to pdf in R. When I plot to graphics device :

plot(1)
legend('topright', bty = 'n', 
       expression(italic("legend"~"\u2640"~"\u2642")))

the legend of plot looks as expected

I can even save it to pdf by right click and "save to pdf". However, when I use inbuilt function pdf

pdf('test.pdf')
plot(1)
legend('topright', bty = 'n',
       expression(italic("legend"~"\u2640"~"\u2642")))
dev.off()

it drops a warning and shows corrupted characters instead:

cairo_pdf does not drop a warning, but it does not show the correct symbols either:

According to this post, I should specify encoding that would support my symbols, however I have no idea how to find out which it does (besides default Helvetica I tried MacRoman without success).

It is apparent that it is possible for R to generate pdf that contains these symbols (because I am able to do so by the right click). However, I would like to achieve that in automatized manner.

My R session settings:

R version 3.3.1 (2016-06-21)
Platform: x86_64-apple-darwin13.4.0 (64-bit)
Running under: OS X 10.11.5 (El Capitan)

locale:
[1] en_US.UTF-8/C/en_US.UTF-8/C/en_US.UTF-8/C

attached base packages:
[1] stats     graphics  grDevices utils     datasets  methods   base    

I also managed to reproduce same behaviour on another computer with macOS Sierra 10.12.1.


回答1:


hi this is working for me (on windows)

cairo_pdf('test.pdf' , family="DejaVu Sans")
plot(1)
legend('topright', bty = 'n',
  paste("legend",quote("\u2640")  ,quote("\u2642")  )) 
dev.off()

Unicode Characters in ggplot2 PDF Output

on mac try this

pdf('test.pdf',encoding="MacRoman")

Plotting symbols fails in PDF




回答2:


A working answer on OS X is using quartz:

quartz(type = 'pdf', file = 'test.pdf')

Source : https://stackoverflow.com/a/19610909/2962344

cairo_pdf seems to work on Linux.




回答3:


I had the same problem, the other solutions did not work for me, and finally I used png instead of pdf:

png('test.png', units="in", width=11, height=10, res = 600)
plot(1)
legend('topright', bty = 'n',
       expression(italic("legend"~"\u2640"~"\u2642")))
dev.off()


来源:https://stackoverflow.com/questions/44547350/corrupted-utf-characters-in-pdf-plots-generated-by-r

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!