runicodeplotgraphicstypesetting

Typesetting combining Unicode in R plots


The unicode combining overline character, U+0305 is useful for indicating mathematical terms such as for the mean of a random variable X. (Some prefer the appearance of the macron, U+0304 (X̄), but a quick Wikipedia suggests this is imprecise.)

I understand how to enter such a character in R, and

cat("X\u0305")

correctly produces . However, when I attempt to place this character in a plot(1:10,10:1,main="X\u0305")

the overline is dramatically offset, as shown here. The same issue is present in ggplot, as ggplot(data.frame(x=1:10,y=10:1)) + geom_point(aes(x=x,y=y,color="X\u0305"))

produces similar output.

I am running Mac OS X El Capitan, and I encounter this problem in both the Terminal and in RStudio. The similarly tagged question, "Unicode characters in ggplot legend" did not solve my problem.


Solution

  • Another option would be to use expression(bar(X)):

    ggplot(data.frame(x=1:10,y=10:1)) + 
       geom_point(aes(x=x,y=y,color="Xbar")) +   
       scale_colour_discrete(labels=expression(bar(X)))
    
    plot(1:10,10:1,main=expression(bar(X)))
    

    ?plotmath has more info on the various options for mathematical text using expressions in R.