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 EmojiOne.ttf) and use showtext to render the fonts, then we can use the font in either base plot or ggplot2.

Installation

Get the released version from CRAN:

install.packages("emojifont")

Or the development version from github:

## install.packages("devtools")
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] "smiley"      "smile"       "sweat_smile" "smiley_cat"  "smile_cat"
emoji(search_emoji('smile'))
## [1] "\U0001f603" "\U0001f604" "\U0001f605" "\U0001f63a" "\U0001f638"

Emoji in R plot

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='EmojiOne')

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

library("ggplot2")
ggplot(d, aes(x, y, color=type, label=label)) +
    geom_text(family="EmojiOne", size=6)

To make it easy to used with ggplot2, I implemented a very simple layer, geom_emoji, to visualize emoji with ggplot2.

ggplot() + geom_emoji("rose", color='steelblue') + theme_void()

x = seq(0, 2*pi, length=30)
y = sin(x)
ggplot() + geom_emoji('heartbeat', x=x, y=y, size=10)

ggtree

library("ggtree")
library("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="EmojiOne", size=10,
                color=rainbow_hcl(8), vjust=.25)

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)

Font Awesome

The emojifont package was initially designed for using emoji font. I found that FontAwesome is quite interesting especially in technical world.

The usage is quite similar as using emoji.

set.seed(2016-03-09)
fa <- fontawesome(c('fa-github', 'fa-weibo', 'fa-twitter', 'fa-android', 'fa-coffee'))
d <- data.frame(x=rnorm(20),
                y=rnorm(20),
                label=sample(fa, 20, replace=T))

ggplot(d, aes(x, y, color=label, label=label)) +
    geom_text(family='fontawesome-webfont', size=6)+
    xlab(NULL)+ylab(NULL) +
    theme(legend.text=element_text(family='fontawesome-webfont'))

To make it easy to used with ggplot2, a corresponding geom_fontawesome is also provided.

ggplot() + geom_fontawesome("fa-github", color='black') + theme_void()

Limitation

RStudio using a different way to open graphics device and showtext is currently incompatible with it.

Since emojifont use showtext as backend to parse emoji font, it also incompatible with RStudio.

The solution is to manually open a window by calling X11() in Linux, quartz() in Mac or windows() in Windows, and create plots on top of that, instead of the built-in device offered by RStudio.

Note

Currently, this package support Emoji Font and Font Awesome.

Feel free to fork this package to add your favorite iconic fonts.

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.3.2 (2016-10-31)
## Platform: x86_64-apple-darwin13.4.0 (64-bit)
## Running under: OS X El Capitan 10.11.6
## 
## 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] ggtree_1.7.9     treeio_0.99.10   colorspace_1.3-2 emojifont_0.4.1 
## [5] ggplot2_2.2.1   
## 
## loaded via a namespace (and not attached):
##  [1] Rcpp_0.12.9       knitr_1.15.1      magrittr_1.5     
##  [4] sysfonts_0.5      munsell_0.4.3     lattice_0.20-34  
##  [7] ape_4.1           showtextdb_1.0    stringr_1.2.0    
## [10] plyr_1.8.4        tools_3.3.2       parallel_3.3.2   
## [13] grid_3.3.2        nlme_3.1-131      gtable_0.2.0     
## [16] htmltools_0.3.5   yaml_2.1.14       lazyeval_0.2.0   
## [19] rprojroot_1.2     digest_0.6.12     assertthat_0.1   
## [22] tibble_1.2        tidyr_0.6.1       prettydoc_0.2.0  
## [25] evaluate_0.10     rmarkdown_1.3     labeling_0.3     
## [28] stringi_1.1.2     scales_0.4.1.9000 backports_1.0.5  
## [31] showtext_0.4-6    jsonlite_1.2      proto_1.0.0