By R on G. Yu
I have played with emoji in R
for a while. My solution of using it is different from what implemented in emoGG.
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.
I implemented my solution of using emoji
in the R package emojifont. 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")
load Emoji font
library(emojifont)
## list available emoji fonts
list.emojifonts()
## [1] "OpenSansEmoji.ttf"
## load selected emoji font
load.emojifont('OpenSansEmoji.ttf')
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 remoji package.
base plot
require(remoji)
set.seed(123)
x
Image may be NSFW.
Clik here to view.
ggplot2
d
Image may be NSFW.
Clik here to view.
We can also use emoji
in title, legend or axis label.
dd=data.frame(x=emoji(c("satisfied", "disapointed")), y=c(50, 10))
emoji_text=element_text(family="OpenSansEmoji", size=20)
ggplot(dd, aes(x, y)) + geom_bar(stat='identity', aes(fill=x)) +
ggtitle(paste(emoji(c("+1", "-1")), collapse=" "))+
theme(axis.text.x = emoji_text, legend.text=emoji_text, title=emoji_text) +
xlab(NULL)+ylab(NULL)
Image may be NSFW.
Clik here to view.
ggtree
require(ggtree)
require(colorspace)
tree_text=paste0(
"(","(","(",
"(",
"(",
emoji("cow"), ",",
"(",
emoji("whale"),",",
...read more
Source:: r-bloggers.com