How to display emojis in ggplot2 using emo package in R?

醉酒当歌 提交于 2019-12-21 04:21:00

问题


I am trying to use ggplot2 to plot some graphs with emojis shown as labels using the emo package. I've learned it from this post, but it's simply not working.

I have tried the emojifont package before, but it's a font type that renders emojis in black and white and it requires opening a new graphics device using e.g. quartz().

To go around the color problem, Tino has suggested (refer to the post above) using the gridSVG package, i.e. after creating a new graphics device and plotting with emojifont, save the graph ps = grid.export("emoji.svg", addClass=T) on local disk as a .svg file that renders emojis in a colorful style.

I would really appreciate a solution that (a) gives colorful emojis and (b) showing the graph directly, which is compatible with routine ggplot use cases.

library(ggplot2)
library(emo)

names = c("smile","school","office","blush","smirk","heart_eyes")
n = length(names):1
e = sapply(names, emo::ji)
dat = data.frame(emoji_name = names, n = n, emoji = e, stringsAsFactors = F)

ggplot(data=dat, aes(emoji_name, n)) + 
  geom_bar(stat = "identity") +
  scale_x_discrete(breaks = dat$emoji_name, labels = dat$emoji) +
  coord_flip()

My R version is

> sessionInfo()
R version 3.4.4 (2018-03-15)
Platform: x86_64-apple-darwin15.6.0 (64-bit)
Running under: macOS Sierra 10.12

回答1:


use the emojifont package :)

read more here

library(ggplot2)
library(emojifont)

names = c("smile","school","office","blush","smirk","heart_eyes")
n = length(names):1
e = sapply(names, emojifont::emoji)
dat = data.frame(emoji_name = names, n = n, emoji = e, stringsAsFactors = F)

ggplot(data=dat, aes(emoji_name, n)) + 
  geom_bar(stat = "identity") +
  scale_x_discrete(breaks = dat$emoji_name, labels = dat$emoji) +
  theme( axis.text.y =element_text( size=20 ) ) +
  coord_flip()

> sessionInfo()
R version 3.5.1 (2018-07-02)
Platform: x86_64-w64-mingw32/x64 (64-bit)
Running under: Windows >= 8 x64 (build 9200)


来源:https://stackoverflow.com/questions/52378661/how-to-display-emojis-in-ggplot2-using-emo-package-in-r

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!