Getting a unicode invalid in R

Viewed 56

I am trying to create a plot using ggplot containing unicode characters for points, in particular the ear of rice character (, or U+1F33E). However, this unicode string gives the following Error: invalid \u{xxxx} sequence. Note that it has to be written as "\u{1f33e}" in R, because \u1f33e contains the unicode for another character for "small greek letter iota with dasia and varia" (ἳ, or U+1F33).

The following (adapted from this post) works for me with another character (U+2620):

library(ggplot2)
df <- read.table(text="x y
                 1    3 
                 2    4 
                 3    6 
                 4    7 ", header=TRUE) 
ggplot(data = df, aes(x, y)) +  
  geom_text(label="\u2620", size = 10, family = "Arial Unicode MS") 

enter image description here

But with the ear of rice, I just get the error.

ggplot(data = df, aes(x, y)) +  
  geom_text(label="\u{1f33e}", size = 10, family = "Arial Unicode MS")

Error: invalid \u{xxxx} sequence (line 2)

How can I get the ear of rice character in R? I don't think it is an issue with installed fonts, because I specify the font and according to ear of rice page on fontspace.com, Arial Unicode MS contains the character.

Edit to add R session information

> sessionInfo()
R version 4.0.2 (2020-06-22)
Platform: x86_64-apple-darwin17.0 (64-bit)
Running under: OS X  12.5.1

Matrix products: default
LAPACK: /Library/Frameworks/R.framework/Versions/4.0/Resources/lib/libRlapack.dylib

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_3.3.2

loaded via a namespace (and not attached):
 [1] magrittr_2.0.3   tidyselect_1.1.2 munsell_0.5.0    colorspace_2.0-2
 [5] R6_2.5.1         rlang_1.0.2      fansi_1.0.3      blob_1.2.1      
 [9] dplyr_1.0.8      tools_4.0.2      grid_4.0.2       gtable_0.3.0    
[13] utf8_1.2.2       cli_3.3.0        DBI_1.1.0        withr_2.5.0     
[17] ellipsis_0.3.2   digest_0.6.29    assertthat_0.2.1 tibble_3.1.6    
[21] lifecycle_1.0.1  farver_2.0.3     purrr_0.3.4      vctrs_0.4.1     
[25] glue_1.6.2       labeling_0.3     compiler_4.0.2   pillar_1.8.1    
[29] generics_0.1.3   scales_1.1.1     pkgconfig_2.0.3 

> capabilities()
       jpeg         png        tiff       tcltk         X11        aqua    http/ftp 
       TRUE        TRUE        TRUE        TRUE        TRUE        TRUE        TRUE 
    sockets      libxml        fifo      cledit       iconv         NLS     profmem 
       TRUE        TRUE        TRUE        TRUE        TRUE        TRUE        TRUE 
      cairo         ICU long.double     libcurl 
       TRUE        TRUE        TRUE        TRUE 
2 Answers

This actually works,

library(ggplot2)
library(Unicode)
df <- read.table(text="x y
                 1    3 
                 2    4 
                 3    6 
                 4    7 ", header=TRUE) 
ggplot(data = df, aes(x, y)) +  
  geom_text(label="\U1F33E", size = 10, family = "Arial Unicode MS") 

You can see the results below, Output

Mac

It works on Mac when using ggsave with type = 'cairo' and as png instead of pdf. Also you can use the unicode: \U1F33E. Here is a reproducible example:

library(ggplot2)
df <- read.table(text="x y
                 1    3 
                 2    4 
                 3    6 
                 4    7 ", header=TRUE) 
p <- ggplot(data = df, aes(x, y)) +  
  geom_text(label='\U1F33E', size = 10, family = "Arial Unicode MS")
ggsave(p, filename = "example.png", dpi = 300, type = "cairo")

enter image description here Created on 2022-09-09 with reprex v2.0.2


> sessionInfo()
R version 4.2.1 (2022-06-23)
Platform: x86_64-apple-darwin17.0 (64-bit)
Running under: macOS Monterey 12.5.1

Matrix products: default
LAPACK: /Library/Frameworks/R.framework/Versions/4.2/Resources/lib/libRlapack.dylib

locale:
[1] nl_NL.UTF-8/nl_NL.UTF-8/nl_NL.UTF-8/C/nl_NL.UTF-8/nl_NL.UTF-8

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

other attached packages:
[1] ggplot2_3.3.6

loaded via a namespace (and not attached):
 [1] rstudioapi_0.14   magrittr_2.0.3    tidyselect_1.1.2  munsell_0.5.0     colorspace_2.0-3  R6_2.5.1          ragg_1.2.2        rlang_1.0.5      
 [9] fansi_1.0.3       dplyr_1.0.10      tools_4.2.1       grid_4.2.1        gtable_0.3.1      utf8_1.2.2        cli_3.3.0         DBI_1.1.3        
[17] withr_2.5.0       systemfonts_1.0.4 digest_0.6.29     assertthat_0.2.1  tibble_3.1.8      lifecycle_1.0.1   textshaping_0.3.6 farver_2.1.1     
[25] purrr_0.3.4       vctrs_0.4.1       glue_1.6.2        labeling_0.4.2    compiler_4.2.1    pillar_1.8.1      generics_0.1.3    scales_1.2.1     
[33] pkgconfig_2.0.3  

> capabilities()
       jpeg         png        tiff       tcltk         X11        aqua    http/ftp     sockets      libxml        fifo      cledit       iconv 
       TRUE        TRUE       FALSE        TRUE        TRUE        TRUE        TRUE        TRUE       FALSE        TRUE        TRUE        TRUE 
        NLS       Rprof     profmem       cairo         ICU long.double     libcurl 
       TRUE        TRUE        TRUE        TRUE        TRUE        TRUE        TRUE 
Related