Ggplot: Unicode characters in axis ticks

ggplot2

#1

There's plenty in the ggplot2 docs about inserting Greek characters in plot titles, axis titles and facet labels (using expression() in the first two cases and label_bquote() in the latter), but when it comes to customising axis ticks, the docs recommend supplying a function to labels in scale_(x|y)_continuous.

On my Mac, both of the following work great:

library(tidyverse)
df1 = data_frame(x = 1:10, y = rnorm(10))

# works
ggplot(df1) + geom_point(aes(x, y)) + scale_x_continuous(labels = function(x) paste0(x, 'σ'))

# works
ggplot(df1) + geom_point(aes(x, y)) + scale_x_continuous(labels = function(x) paste0(x, '\U03C3'))

But when I try to run this code on my organisation's Linux server, the sigmas are replaced by .. in the (PDF) plot output. Is this a platform- or setup-specific problem, or am I just not doing this the Right Way?


#2

Is it graphics-device specific?

Also (and I realise this isn't a solution, just out of curiosity), if you open the PDFs rendered on your Mac on the Linux server (if you're able to) do you get the same thing?

Do unicode chars in axis ticks work with base on the Linux server? (Again, not a suggested solution, just imagining how to isolate variables here).


#3

Ah, good point, @mara! I just checked, and they don't work on PDF on my Mac (I get ..). It does work with PNG rendering, though.

But on my Linux server, both PDFs and PNGs result in ... So it could be a bit of a multi-factor problem here.

I'm ashamed to say it's been so long since I used base plotting that I can't remember how to make a reprex with it :laughing:


#4

I am not sure this will be helpful, but for the Mac environment, when you make your PDF have you tried extrafont::embed_fonts()?

For Linux, have you tried the unicode character for sigma (U+03A3)? My go to with the ... issue is to keep searching for alternative codings :flushed:.

UPDATE:
I have a love-hate for these LaTeX-knitr-ggplot issues, and just cannot stop until I crack them. After some hacking and reading, I found this post and remembered the dev argument/option in knitr. I found something that works for keeping the sigma in the PDF on my Mac, maybe might for you?

```{r echo=FALSE, dev="cairo_pdf"}

df1 = data_frame(x = 1:10, y = rnorm(10))

# works
ggplot(df1) + geom_point(aes(x, y)) + scale_x_continuous(labels = function(x) paste0(x, 'σ'))

# works
ggplot(df1) + geom_point(aes(x, y)) + scale_x_continuous(labels = function(x) paste0(x, '\U03C3'))

Cheers, Steph


How to reference siunitx units in axis labels for ggplot2 figures?
#5

Thanks for the advice, @stephhazlitt! I've given listing the code point ('\U03C3') a go; no dice. I'll try switching to Cairo on my Mac a go now :slight_smile:

EDIT: confirmed that using cairo_pdf on Mac works! Thanks a lot!

ggsave(p, filename = 'blah-raij1.pdf', device = cairo_pdf)

If I'd bothered to check the warnings when I did the test plot, I'd have seen:

There were 50 or more warnings (use warnings() to see the first 50)
> warnings()
Warning messages:
1: In grid.Call(C_textBounds, as.graphicsAnnot(x$label),  ... : conversion failure on '2.5σ' in 'mbcsToSbcs': dot substituted for <cf>
[etc.]

Unfortunately, it doesn't work on my Linux server, although the failure happens silently (the plot saves without a warning, but the characters are still substituted). cairo_pdf() appears to be there, but it also fails silently (plot produced but substitution occurs). ?cairo_pdf says that R will report a warning if it's not compiled with Cairo support, and that isn't happening. It also says that it defaults to Nelvetica Neue (presumably it was designed for Mac first?), so I'm wondering if it's instead a missing glyph issue on Linux.

ggsave() lets you pass arguments onto device, so I tried passing one on that I have installed locally (DejaVu Sans Mono):

ggsave(p, filename = '~/blah5.pdf', device = cairo_pdf, family = 'DejaVu Sans Mono')

Unfortunately, the font renders correctly, but the substitution is still made :confused:

EDIT: hmm, this could be a locale problem on my server:

> Sys.getlocale()
[1] "C"

#6

Great that it is working on your Mac, not great about the Linux server :disappointed:. I don't use Linux, however I would guess that you are on the right path and that it is probably a character encoding issue.


#7

I'll keep tinkering! :slight_smile:


#8

Mac runs on unix, so you shouldn't get different results, if you have the same locale. I would carefully compare the locales on both systems.