6

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):

enter image description here

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!

arvi1000
  • 9,393
  • 2
  • 42
  • 52

1 Answers1

2

sprintf works for me on windows and Rstudio 1.0.143

comfortable way to use unicode characters in a ggplot graph

library(ggplot2)

facets <- sprintf(c('✓', '✗'))
facets2 <- sprintf(c('\u2713', '\u2717'))
facets3 <- sprintf(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')

Session info:

R version 3.4.0 (2017-04-21)
Platform: x86_64-w64-mingw32/x64 (64-bit)
Running under: Windows 7 x64 (build 7601) Service Pack 1

Matrix products: default

locale:
[1] LC_COLLATE=French_France.1252  LC_CTYPE=French_France.1252    LC_MONETARY=French_France.1252 LC_NUMERIC=C                  
[5] LC_TIME=French_France.1252    

attached base packages:
[1] stats     graphics  grDevices utils     datasets  methods   base     

other attached packages:
[1] ggplot2_2.2.1    xts_0.9-7        zoo_1.8-0        dygraphs_1.1.1.4

loaded via a namespace (and not attached):
 [1] Rcpp_0.12.11     lattice_0.20-35  digest_0.6.12    plyr_1.8.4       grid_3.4.0       jsonlite_1.4     gtable_0.2.0     magrittr_1.5    
 [9] scales_0.4.1     rlang_0.1.1      lazyeval_0.2.0   labeling_0.3     tools_3.4.0      htmlwidgets_0.8  munsell_0.4.3    yaml_2.1.14     
[17] compiler_3.4.0   colorspace_1.3-2 htmltools_0.3.6  tibble_1.3.3   
arvi1000
  • 9,393
  • 2
  • 42
  • 52
s.brunel
  • 1,003
  • 10
  • 24
  • `facets <- sprintf(c('✓', '✗'))` doesn't make any difference in the plot for me. Are you able to reproduce the error with the code as posted? What's your `sessionInfo()` ? – arvi1000 Jun 13 '17 at 15:48
  • Are you saying you didn't see the error with the original code -- i.e. the characters display in the plot using both the `facets` and `facets2` vectors? – arvi1000 Jun 14 '17 at 18:48
  • https://stackoverflow.com/questions/5886018/using-unicode-dingbat-like-glyphs-in-r-graphics-across-devices-platforms-e/19610909#19610909 this seems to be the real solution – s.brunel Jun 15 '17 at 09:10
  • Unfortunately that doesn't work for me either. My hunch is this is locale-dependent. I notice your system is `French_France.1252`, while mine is `en_US.UTF-8` -- that may be why you aren't even reproducing the error. – arvi1000 Jun 15 '17 at 16:27