Day73: Displaying emojis (Bug)

Posted by csiu on May 8, 2017 | with: 100daysofcode

I spent the past couple hours trying to figure out why emojis are not being display properly on ggplot, but no luck.

Minimal example

Loading libraries + data

library(ggplot2)
library(dplyr)

# Specify alien monster emoji
my_emoji <- '\U0001f47e'

# Create base plot using iris data set
base_plot <-
  iris %>%
  ggplot(aes(x=Sepal.Length, y=Sepal.Width)) +
  geom_point(size=.5)

Plotting

base_plot +
  geom_text(x=5, y=4, label="Adding regular text works") +
  geom_text(x=6, y=4, label=my_emoji)

Adding regular text works, but using unicode doesn’t (only rectangles are displayed).

# Setting 'dev="cairo_pdf"' also does not work
base_plot + geom_text(x=6, y=4, label=my_emoji)

Displaying the emoji: print vs cat

print(my_emoji)
## [1] "\U0001f47e"
cat(my_emoji)
## 👾

cat renders the emoji, but the emoji is not rendered in ggplot – why?

Unicode in the title

As per Stackoverflow: Comfortable way to use unicode characters in a ggplot graph, μ is displayed in the title, but 👾 is not – why?

base_plot + ggtitle(sprintf('5%sg', '\u03BC'))

base_plot + ggtitle(sprintf('5%sg', my_emoji))

SessionInfo

sessionInfo()
## R version 3.3.2 (2016-10-31)
## Platform: x86_64-apple-darwin13.4.0 (64-bit)
## Running under: OS X El Capitan 10.11.3
##
## locale:
## [1] en_CA.UTF-8/en_CA.UTF-8/en_CA.UTF-8/C/en_CA.UTF-8/en_CA.UTF-8
##
## attached base packages:
## [1] stats     graphics  grDevices utils     datasets  methods   base     
##
## other attached packages:
## [1] dplyr_0.5.0   ggplot2_2.2.1
##
## loaded via a namespace (and not attached):
##  [1] Rcpp_0.12.10     knitr_1.15       magrittr_1.5     munsell_0.4.3   
##  [5] colorspace_1.3-1 R6_2.2.0         stringr_1.1.0    plyr_1.8.4      
##  [9] tools_3.3.2      grid_3.3.2       gtable_0.2.0     DBI_0.5-1       
## [13] htmltools_0.3.5  yaml_2.1.14      lazyeval_0.2.0   rprojroot_1.1   
## [17] digest_0.6.12    assertthat_0.1   tibble_1.3.0     evaluate_0.10   
## [21] rmarkdown_1.3    labeling_0.3     stringi_1.1.2    scales_0.4.1    
## [25] backports_1.0.4