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.
Get the released version from CRAN:
install.packages("emojifont")
Or the development version from github:
## install.packages("devtools")
devtools::install_github("GuangchuangYu/emojifont")
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"
To support using emoji in R plot, we need to load emoji
font and then use family
parameter to specify using the font.
## list available emoji fonts
list.emojifonts()
## [1] "OpenSansEmoji.ttf"
## load selected emoji font
load.emojifont('OpenSansEmoji.ttf')
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')
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="OpenSansEmoji", size=6)
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="OpenSansEmoji", size=10,
color=rainbow_hcl(8))
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)
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.
load.fontawesome()
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'))
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
.
Currently, this package support Emoji Font and Font Awesome.
OpenSansEmoji.ttf
is downloaded from https://github.com/MorbZ/OpenSansEmojifontawesome-webfont.ttf
is downloaded from https://github.com/FortAwesome/Font-Awesome.Feel free to fork this package to add your favorite iconic fonts.
If you have any, let me know. Thx!
Here is the output of sessionInfo()
on the system on which this document was compiled:
## R version 3.3.0 (2016-05-03)
## Platform: x86_64-apple-darwin15.4.0 (64-bit)
## Running under: OS X 10.11.5 (El Capitan)
##
## locale:
## [1] C/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.5.6 colorspace_1.2-6 emojifont_0.3.4 ggplot2_2.1.0
##
## loaded via a namespace (and not attached):
## [1] Rcpp_0.12.5 formatR_1.4 plyr_1.8.4
## [4] XVector_0.12.0 tools_3.3.0 sysfonts_0.5
## [7] zlibbioc_1.18.0 digest_0.6.9 jsonlite_0.9.22
## [10] evaluate_0.9 tibble_1.0 gtable_0.2.0
## [13] nlme_3.1-128 lattice_0.20-33 png_0.1-7
## [16] yaml_2.1.13 parallel_3.3.0 proto_0.3-10
## [19] showtextdb_1.0 stringr_1.0.0 knitr_1.13
## [22] fftwtools_0.9-7 Biostrings_2.40.2 S4Vectors_0.10.1
## [25] IRanges_2.6.1 locfit_1.5-9.1 stats4_3.3.0
## [28] grid_3.3.0 jpeg_0.1-8 rmarkdown_0.9.6
## [31] tidyr_0.5.1 magrittr_1.5 scales_0.4.0
## [34] htmltools_0.3.5 BiocGenerics_0.18.0 abind_1.4-3
## [37] showtext_0.4-4 assertthat_0.1 EBImage_4.14.2
## [40] ape_3.5 tiff_0.1-5 labeling_0.3
## [43] stringi_1.1.1 munsell_0.4.3