4

I am able to display Unicode characters on a plot within my R session, but when I try to export the figure using e.g. png(), CairoPNG(), cairo_pdf(), etc., some (although not all) of these fail to appear. For example, the following simple plot shows the correct characters for me in the R window:

plot( 1:9,
   c(280, 540, 750, 1434, 1078, 728, 662, 606, 785),
   pch=-as.hexmode(c("4E00", "4E8C", "4E09", "56DB", "4E94", "516D", "4E03", "516B", "4E5D")),
   xlab="Digit",
   ylab="pixels"
)

figure

However, when I use png() first to try to save this image to disk, the characters are replaced with boxes. The previous discussions on this I managed to find (e.g. here and here) suggested using cairo_pdf. But I get basically the same result with cairo_pdf(), CairoPNG(), cairo_ps(), etc. Changing the locale, as suggested here, also still got the same result.

figure

Some session info:

R version 3.2.2 (2015-08-14)
Platform: x86_64-w64-mingw32/x64 (64-bit)
Running under: Windows 8 x64 (build 9200)

locale:
[1] LC_COLLATE=English_United States.1252  LC_CTYPE=English_United States.1252   
[3] LC_MONETARY=English_United States.1252 LC_NUMERIC=C                          
[5] LC_TIME=English_United States.1252    

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

other attached packages:
[1] yarrr_0.1    lme4_1.1-8   Matrix_1.2-2 boot_1.3-17  Cairo_1.5-9 

loaded via a namespace (and not attached):
[1] minqa_1.2.4     MASS_7.3-43     tools_3.2.2     Rcpp_0.12.4     splines_3.2.2  
[6] nlme_3.1-121    grid_3.2.2      nloptr_1.0.4    lattice_0.20-33

Any suggestions would be greatly appreciated!

EDIT: Fixed a mistake in the code I had posted, sorry about that; you should hopefully be able to replicate the plot now.

Community
  • 1
  • 1
  • I wouldn't recommend using just Cairo for text. The docs are very explicit about the fact that it is a _toy_ text API. You probably want to use [Pango](https://www.pango.org). – oldtechaa Jun 05 '16 at 11:58
  • @oldtechaa: Thanks for the suggestion. Unfortunately I can't really figure out how to use Pango in R to export this figure. I've looked through several pages and skimmed the documentation of RGtk2 but I couldn't find a straightforward worked example. The closest thing I could find, the example [here](http://finzi.psych.upenn.edu/library/RGtk2/html/pango-Cairo-Rendering.html), doesn't run for me (I get `Error: attempt to call 'showAll' on invalid reference 'window'` at the last line). Do you know of anywhere that has a relatively simple example of what you have in mind? – Stephen Politzer-Ahles Jun 06 '16 at 08:40
  • I think it must have been a problem with the way it was done. Take a look at [my project](https://github.com/oldtechaa/SeekMIDI/blob/master/SeekMIDI.pl) (in Perl) for an example (under the `expose` sub). All it really requires is making a Pango layout from your Cairo context. Then my code sets the font size, and begins drawing similar to the way that Pango/Cairo example shows. – oldtechaa Jun 06 '16 at 14:11
  • Thanks for your suggestions (and sorry for my late reply). I had a look but, to be honest, I'm not sure what to do with this since I don't have any experience with linking R and Perl. Since the plots that I'm trying to export are based on statistical models that I'm running in R, and there are a bunch of them in a loop, I had been hoping to find a solution within R itself. But it might not be possible. Anyway, I was mainly only wanting to use Cairo for the anti-aliasing to get smooth lines; if it doesn't work, I can still just save the uglier figures from the R console, I guess. Thanks! – Stephen Politzer-Ahles Sep 12 '16 at 02:00
  • Do you have a Pango binding in R at all? That's the first step. – oldtechaa Sep 12 '16 at 12:49
  • I see that the Pango binding is a part of RGtk2. Could you show some more complete code telling how you use Cairo? And just as a note, your question is a superb example of a question with all the necessary info to solve the problem. The XY problem just had to come along and make you change tack altogether. :) – oldtechaa Sep 12 '16 at 13:16

0 Answers0