How can I get unicode characters to appear in the facet labels (or anywhere, really) in ggplot charts?
There are quite a few related posts floating around, but none have done the trick for me. (I address why none are duplicates at the end)
So, given this code:
library(ggplot2)
facets <- c('✓', '✗')
facets2 <- c('\u2713', '\u2717')
facets3 <- c('check', 'x')
set.seed(123)
my_df <- data.frame(x = runif(40), y = runif(40),
z = rep(facets, each=20),
stringsAsFactors = F)
ggplot(my_df, aes(x, y, color=z)) + geom_point() +
facet_wrap(~z) +
theme(legend.position = 'none')
...I get this plot (note the missing facet labels):
I get the same result when I use facets2
for the labels (i.e. specifying escaped char codes instead of having literals), but of course when I use facets3
everything appears as it should.
My sessionInfo()
I'm using R Studio 1.0.136 and my sessionInfo()
is
R version 3.3.1 (2016-06-21)
Platform: x86_64-apple-darwin13.4.0 (64-bit)
Running under: OS X 10.11.6 (El Capitan)
locale:
[1] en_US.UTF-8/en_US.UTF-8/en_US.UTF-8/C/en_US.UTF-8/en_US.UTF-8
attached base packages:
[1] stats graphics grDevices utils datasets methods base
other attached packages:
[1] ggplot2_2.2.1
loaded via a namespace (and not attached):
[1] labeling_0.3 colorspace_1.2-6 scales_0.4.1 assertthat_0.1 lazyeval_0.2.0
[6] plyr_1.8.4 tools_3.3.1 gtable_0.2.0 tibble_1.2 Rcpp_0.12.11.2
[11] grid_3.3.1 digest_0.6.12 munsell_0.4.3
Other posts about this
1) These have no answers (Unicode characters in ggplot labels, utf-8 in ggplot axis labels, this is basically the same: Use a half filled squares on ggplot2 facet_wrap labels, How can I get a unicode symbol into factor levels for a ggplot?)
2) This is about greek letters rather than any unicode, and the answer doesn't seem to work for me anyway (ggplot unicode characters without Cairo?
3) The most common solution seems to involve cairo_pdf()
, e.g. as suggested in this post: using Unicode 'dingbat-like' glyphs in R graphics, across devices & platforms, especially PDF.
However, this is about pdf output rather than R Studio preview window, in which I'd also like to see unicode labels.
In any case when I precede the ggplot call in my example with cairo_pdf()
, the ggplot call just hangs and I have to terminate R.
4) Comments on some of the posts above suggest the problem is related to using Windows with an English locale, but I'm on OS X with a UTF-8 locale.
I'd appreciate any suggestions!