emojifont: using emoji font in R

emoGG is a good attemp to add emoji in ggplot2. It render emoji picture (png) and creat a layer, geom_emoji, to add emoji.

In my opinion, emoji should be treated as ordinary font in user interface, albeit it maynot be true internally.

It would be more flexible if we can use emoji as ordinary font and in this way user don’t need to learn extra stuff.

The emojifont package is designed to bring emoji font to R users and is created for users that are impatient and relutant to learn.

The package is very simple, pack some emoji fonts (currently only OpenSansEmoji.ttf) and use showtext to render the fonts, then we can use the font in either base plot or ggplot2.

Installation

devtools::install_github("GuangchuangYu/emojifont")

Emoji characters

To use emoji, we need to use their corresponding unicode. Emoji unicode can be found in http://apps.timwhitlock.info/emoji/tables/unicode, or searched using search_emoji function. The search_emoji function will return emoji aliases which can be converted to unicode by emoji function.

library(emojifont)

search_emoji('smile')
## [1] "smile"       "smiley"      "sweat_smile" "smiley_cat"  "smile_cat"
emoji(search_emoji('smile'))
## [1] "\U0001f604" "\U0001f603" "\U0001f605" "\U0001f63a" "\U0001f638"

Emoji in R plot

To support using emoji in R plot, we need to load emoji font and then use family parameter to specify using the font.

load Emoji font

## list available emoji fonts
list.emojifonts()
## [1] "OpenSansEmoji.ttf"
## load selected emoji font
load.emojifont('OpenSansEmoji.ttf')

base plot

set.seed(123)
x <- rnorm(10)
set.seed(321)
y <- rnorm(10)
plot(x, y, cex=0)
text(x, y, labels=emoji('cow'), cex=1.5, col='steelblue', family='OpenSansEmoji')

ggplot2

d <- data.frame(x=x, y=y,
     label = sample(c(emoji('cow'), emoji('camel')), 10, replace=TRUE),
     type = sample(LETTERS[1:3], 10, replace=TRUE))
require(ggplot2)
ggplot(d, aes(x, y, color=type, label=label)) + 
    geom_text(family="OpenSansEmoji", size=5)

ggtree

require(ggtree)
require(colorspace)

tree_text=paste0(
    "(","(","(",
       "(",
            "(",
               emoji("cow"), ",",
               "(",
                  emoji("whale"),",",
                  emoji("dolphin"),
               ")",
            "),",
            "(",
               emoji('pig2'),",",
               emoji('boar'),
            ")",
       "),",
       emoji("camel"),
    "),", emoji("fish"), "),", 
emoji("seedling"), ");")

ggtree(read.tree(text=tree_text)) + xlim(NA, 7) +
   geom_tiplab(family="OpenSansEmoji", size=10,
               color=rainbow_hcl(8))

Apple Color Emoji

Although R’s graphical devices don’t support AppleColorEmoji font, it’s still possible to use it. We can export the plot to svg file and render it in Safari.

library(gridSVG)
p <-  ggtree(read.tree(text=tree_text), size=2) + geom_tiplab(size=20)
p <- p %>% phylopic("79ad5f09-cf21-4c89-8e7d-0c82a00ce728", color="firebrick", alpha = .3)
p <- p + xlim(NA, 7) + ylim(NA, 8.5)
p
ps = grid.export("emoji.svg", addClass=T)

Note

OpenSansEmoji.ttf in this package is downloaded from https://github.com/MorbZ/OpenSansEmoji.

Feel free to fork this package to add your favorite Emoji Font.

Bugs/Feature requests

If you have any, let me know. Thx!

Session info

Here is the output of sessionInfo() on the system on which this document was compiled:

## R version 3.2.3 (2015-12-10)
## Platform: x86_64-apple-darwin15.2.0 (64-bit)
## Running under: OS X 10.11.3 (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] emojifont_0.3.1 ggplot2_2.0.0  
## 
## loaded via a namespace (and not attached):
##  [1] Rcpp_0.12.3      sysfonts_0.5     showtextdb_1.0   digest_0.6.9    
##  [5] plyr_1.8.3       grid_3.2.3       gtable_0.1.2     formatR_1.2.1   
##  [9] magrittr_1.5     evaluate_0.8     scales_0.3.0     stringi_1.0-1   
## [13] rmarkdown_0.9.5  proto_0.3-10     tools_3.2.3      showtext_0.4-4  
## [17] stringr_1.0.0    munsell_0.4.3    yaml_2.1.13      colorspace_1.2-6
## [21] htmltools_0.3    knitr_1.12.3