Problem

I find Unicode extremely useful in my ggplots for symbols and special characters in legends. For instance, much easier than using expressions, I use the unicode for a suffix, e.g.
labs(x = "g₁", y = "g₂").
Also I usually want to save the plots as PDF, not as some pixmap format – PDFs are much smaller while offering unlimited resolution.

There are actually two separate problems that surface here.

  1. 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.
  2. The standard ggsave to PDF replaces the unicode characters with ellipses (…). That’s unacceptable.

Solutions (tested in MacOS, High Sierra & Catalina)

  1. To see the Unicode on the screen:
    In your ggplot code, add this line somewhere
    theme(text=element_text(family="Helvetica")) +
    ad lib replacing Helvetica with any font on your machine that does contain the desired Unicode characters, e.g. “Arial Unicode MS”.
  2. To produce a PDF with intact Unicode:
    Install the package Cairo, and use ggsave like so
    library(Cairo); ggsave(filename='whatever.pdf', device=cairo_pdf, …)
  3. To produce a PNG (or JPG) with intact Unicode:
    Easy: the standard ggsave renders Unicode correctly for these targets.

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…