2

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
    `stringi::stri_enc_fromutf32(0x0001F33E)` returns the ear of rice symbol when entered in an R terminal; it doesn't show up in the plot though, which means that Arial Unicode MS does not include a symbol for U+1F33E. So I think there are two issues here: (1) How to input these U+1**** symbols in R using `\u`? (2) Finding a font that supports these symbols in `ggplot`. – Maurits Evers Sep 09 '22 at 04:52
  • 1
    `plot(1, pch=intToUtf8(0x1F33E))` seems to work over here on Windows 10, which I found via `?Unicode` – thelatemail Sep 09 '22 at 05:03
  • @thelatemail Interesting; I'm getting a box replacement instead of the symbol on MacOS Catalina. I was hoping Claus Wilke's `ggtext` might help but unsuccessful so far... – Maurits Evers Sep 09 '22 at 05:12
  • After further testing, `plot(1, pch="\U1f33e")` actually works. That's with a capital `U` not a lower-case `u`. I can also confirm it works on *ggplot2* as well. – thelatemail Sep 09 '22 at 05:26
  • I can't get the symbol to show with `plot(1, pch="\U1f33e")`; not sure whether this is OS or locale specific. – Maurits Evers Sep 09 '22 at 06:07
  • I'm having the same issue: `plot(1, pch="\U1f33e")` works on a Windows machine I have access to, but not on my Mac. – Gopal Penny Sep 09 '22 at 13:28

2 Answers2

1

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

Luiy_coder
  • 321
  • 2
  • 11
  • 1
    This works on a Windows machine I have access to, but it's not working on my Mac. I also tried running `extrafont::loadfonts("all")`, but this does not seem to change anything on the Mac. – Gopal Penny Sep 09 '22 at 13:27
0

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 
Quinten
  • 35,235
  • 5
  • 20
  • 53
  • I've tried your code but it's not working for me, unfortunately. It's strange because setting the font works (I can change it when plotting other characters, including the skull+crossbones) but not with the ear of rice. Furthermore I know that I can use ear of rice with Arial Unicode MS, because it works in MS Word. I'm not sure where the disconnect is. Your plot looks fantastic by the way. Perhaps I'll try on another machine. – Gopal Penny Sep 11 '22 at 09:13
  • @GopalPenny, maybe try with a clean environment and restart your Rstudio. Could you also share your `sessioninfo` in your question? – Quinten Sep 11 '22 at 09:15
  • I just added my `sessionInfo()` to the post, along with `capabilities()`. The session is pretty close to yours, although I noticed that you also have `systemfonts` loaded via namespace. I also tried installing the package and loading it, but it made no difference in the saved plot. – Gopal Penny Sep 12 '22 at 02:55