Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

r

ggplot2

emoji

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

enter image description here

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
like image 907
wen Avatar asked Sep 18 '18 03:09

wen


2 Answers

library(ggplot2)
library(dplyr)
library(purrr)
#remotes::install_github("hadley/emo")
library(emo)

dat <- tibble(names = c("smile","school","office","blush","smirk","heart_eyes"),
       emoji = map_chr(names, emo::ji)) %>% 
  tibble::rowid_to_column("n")

dat %>% ggplot(aes(n, emoji)) + 
  geom_col(orientation="y") +
  theme(axis.text.y = element_text(size = 18))

sessioninfo::session_info()
#> ─ Session info ───────────────────────────────────────────────────────────────
#>  setting  value                       
#>  version  R version 3.6.3 (2020-02-29)
#>  os       Ubuntu 18.04.4 LTS          
#>  system   x86_64, linux-gnu           
#>  ui       X11                         
#>  language en_GB:en                    
#>  collate  en_GB.UTF-8                 
#>  ctype    en_GB.UTF-8                 
#>  tz       Europe/Oslo                 
#>  date     2020-04-07                  
#> 
#> ─ Packages ───────────────────────────────────────────────────────────────────
#>  package     * version    date       lib source                     
#>  assertthat    0.2.1      2019-03-21 [1] CRAN (R 3.6.0)             
#>  cli           2.0.2      2020-02-28 [1] CRAN (R 3.6.3)             
#>  colorspace    1.4-1      2019-03-18 [1] CRAN (R 3.6.0)             
#>  crayon        1.3.4      2017-09-16 [1] CRAN (R 3.6.0)             
#>  digest        0.6.25     2020-02-23 [1] CRAN (R 3.6.3)             
#>  dplyr       * 0.8.5      2020-03-07 [1] CRAN (R 3.6.3)             
#>  ellipsis      0.3.0      2019-09-20 [1] CRAN (R 3.6.1)             
#>  emo         * 0.0.0.9000 2020-04-07 [1] Github (hadley/emo@3f03b11)
#>  evaluate      0.14       2019-05-28 [1] CRAN (R 3.6.0)             
#>  fansi         0.4.1      2020-01-08 [1] CRAN (R 3.6.2)             
#>  farver        2.0.3      2020-01-16 [1] CRAN (R 3.6.2)             
#>  generics      0.0.2      2018-11-29 [1] CRAN (R 3.6.0)             
#>  ggplot2     * 3.3.0      2020-03-05 [1] CRAN (R 3.6.3)             
#>  glue          1.4.0      2020-04-03 [1] CRAN (R 3.6.3)             
#>  gtable        0.3.0      2019-03-25 [1] CRAN (R 3.6.0)             
#>  highr         0.8        2019-03-20 [1] CRAN (R 3.6.0)             
#>  htmltools     0.4.0      2019-10-04 [1] CRAN (R 3.6.1)             
#>  knitr         1.28       2020-02-06 [1] CRAN (R 3.6.3)             
#>  labeling      0.3        2014-08-23 [1] CRAN (R 3.6.0)             
#>  lifecycle     0.2.0      2020-03-06 [1] CRAN (R 3.6.3)             
#>  lubridate     1.7.8      2020-04-06 [1] CRAN (R 3.6.3)             
#>  magrittr      1.5        2014-11-22 [1] CRAN (R 3.6.3)             
#>  munsell       0.5.0      2018-06-12 [1] CRAN (R 3.6.0)             
#>  pillar        1.4.3      2019-12-20 [1] CRAN (R 3.6.2)             
#>  pkgconfig     2.0.3      2019-09-22 [1] CRAN (R 3.6.1)             
#>  purrr       * 0.3.3      2019-10-18 [1] CRAN (R 3.6.1)             
#>  R6            2.4.1      2019-11-12 [1] CRAN (R 3.6.1)             
#>  Rcpp          1.0.4      2020-03-17 [1] CRAN (R 3.6.3)             
#>  rlang         0.4.5      2020-03-01 [1] CRAN (R 3.6.3)             
#>  rmarkdown     2.1        2020-01-20 [1] CRAN (R 3.6.3)             
#>  scales        1.1.0.9000 2020-04-03 [1] local                      
#>  sessioninfo   1.1.1      2018-11-05 [1] CRAN (R 3.6.0)             
#>  stringi       1.4.6      2020-02-17 [1] CRAN (R 3.6.3)             
#>  stringr       1.4.0      2019-02-10 [1] CRAN (R 3.6.0)             
#>  tibble        3.0.0      2020-03-30 [1] CRAN (R 3.6.3)             
#>  tidyselect    1.0.0      2020-01-27 [1] CRAN (R 3.6.3)             
#>  vctrs         0.2.4      2020-03-10 [1] CRAN (R 3.6.3)             
#>  withr         2.1.2      2018-03-15 [1] CRAN (R 3.6.0)             
#>  xfun          0.12       2020-01-13 [1] CRAN (R 3.6.2)             
#>  yaml          2.2.1      2020-02-01 [1] CRAN (R 3.6.2)             
#> 
#> [1] /home/dm0737pe/R/x86_64-pc-linux-gnu-library/3.6
#> [2] /usr/local/lib/R/site-library
#> [3] /usr/lib/R/site-library
#> [4] /usr/lib/R/library

Created on 2020-04-07 by the reprex package (v0.3.0)

like image 170
dmi3kno Avatar answered Oct 22 '22 04:10

dmi3kno


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

enter image description here

> sessionInfo()
R version 3.5.1 (2018-07-02)
Platform: x86_64-w64-mingw32/x64 (64-bit)
Running under: Windows >= 8 x64 (build 9200)
like image 26
Wimpel Avatar answered Oct 22 '22 03:10

Wimpel