I find Unicode extremely useful in my ggplots for symbols and special characters in legends. For instance, much easier then using expressions, I use the unicode for suffix, e.g.
labs(x = "g₁", y = "g₂").
Also I usually want to save the plots as PDF, not as some pixmap format (much smaller and best resolution).
Now there are two separate problems with this.
- Some Unicode characters, while visible in the program code, are replaced by block outlines in the plot on screen. That’s bad, but one could live with it.
- The standard
ggsaveto PDF replaces the unicode characters with ellipses (…). That’s unacceptable.
Solutions (tested in MacOS: High Sierra + Catalina)
- In your ggplot code, add this line somewhere
ad lib replacing Helvetica with any font on your machine that does contain the desired Unicode characters. Another possible font would be “Arial Unicode MS”. This helps to see the Unicode on the screen.
- To produce a PDF, install the package “Cairo”, and use ggsave like so
library(Cairo); ggsave(filename='whatever.pdf', device=cairo_pdf, …)
If you don’t want PDF but, e.g. PNG, then the dance with “device=cairo_pdf” is not necessary, in a PNG the standard
ggsaverenders the Unicode correctly.
I dug into this deeply because on one of my machines ggsave suddenly threw an error
failed to load cairo DLL.
Hmm… after lots of trying to understand & solve this (like re-installing Cairo with the wonderful RStudio, re-installing cairo in brew, …), I finally found on stack exchange this advice
brew cask install r
This re-installed R, and Cairo now also works! There still are signs and wonders :)!
What a nice task to spend time with in times of Corona…