How to display emojis in ggplot2 using emo package in R?
Asked Answered
G

2

14

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
Gonzales answered 18/9, 2018 at 3:53 Comment(6)
That extra ) was a typo. Removing it still does not fix things. I am using a Mac OS; I was wondering if the problem is OS-dependent.Gonzales
thanks for raising the OS issue. Well, I'm using windows 7 OS and I get the white squares too.. So, I think its not OS dependent.Jocundity
Did you try installing a preview version of Rstudio ? I faced some problems with emojis and Rstudio (1.1.456) on Windows, gone with a preview version.Ophite
@Gonzales When I run your code it works fine. I'm on Ubuntu 18.04 LTS. R version 3.4.4, ggplot2 version 3.0.0Jansson
There have been a few posts about this before, and it seems like an OS issue with printing unicode characters. Here's one: #47731089Bags
And here's a related blog post: seankross.com/2017/05/30/…Bags
I
10

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)
Interstadial answered 20/9, 2018 at 16:22 Comment(3)
Thanks, but I know about this solution. I prefer not using emojifont, because (a) emojifonts are fonts (they are not colorful; they are black and white), and (b) it oftentimes requires open a new graphics device using quartz(), or else it is either not displayed or not displayed properly.Gonzales
sorry.. I can't try using emo, since it is not available for R 3.5.1 Warning in install.packages : package ‘emo’ is not available (for R version 3.5.1)Interstadial
@Interstadial it's a misleading warning message that actually means it's not available on CRAN, not that it won't work with your R version. emo is only on github: github.com/hadley/emoBags
V
8
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)

Vharat answered 7/4, 2020 at 13:7 Comment(1)
What system font are you using? For me your code plots the emojis without color.Leathaleather

© 2022 - 2024 — McMap. All rights reserved.