Search code examples
rggplot2rstudioemoji

Why aren't emojis rendering properly in ggplot?


I have failed many times to incorporate emojis into ggplot and am including a basic example below where I'm simply trying to add an emoji to both axes titles on a blank plot. Grateful for any and all help:

library(tidyverse)
#remotes::install_github("hadley/emo")
library(emo)

ggplot() +
  xlab(paste0('Mileage', emo::ji("fuel_pump"))) +
  ylab(paste0('Vehicle', emo::ji("car"))) +
  theme(text = element_text(family = "Trebuchet MS"))

This yields: enter image description here

I also tried using the emojifont package.

library(emojifont)

ggplot() +
  xlab(paste0('Mileage', emojifont::emoji("fuel_pump"))) +
  ylab(paste0('Vehicle', emojifont::emoji("car"))) +
  theme(text = element_text(family = "Trebuchet MS"))

With this second attempt, not only do the emojis not render properly, but I receive the following warning and the font family I selected is replaced with something generic: "1: In grid.Call(C_textBounds, as.graphicsAnnot(x$label), x$x, x$y, : font family 'Trebuchet MS' not found, will use 'wqy-microhei' instead"

enter image description here

Finally, including sessionInfo() below:

R version 4.3.3 (2024-02-29)
Platform: aarch64-apple-darwin20 (64-bit)
Running under: macOS Sonoma 14.4.1

Matrix products: default
BLAS:   /System/Library/Frameworks/Accelerate.framework/Versions/A/Frameworks/vecLib.framework/Versions/A/libBLAS.dylib 
LAPACK: /Library/Frameworks/R.framework/Versions/4.3-arm64/Resources/lib/libRlapack.dylib;  LAPACK version 3.11.0

locale:
[1] en_US.UTF-8/en_US.UTF-8/en_US.UTF-8/C/en_US.UTF-8/en_US.UTF-8

time zone: America/Los_Angeles
tzcode source: internal

attached base packages:
[1] stats     graphics  grDevices utils     datasets  methods   base     

other attached packages:
 [1] scales_1.3.0    lubridate_1.9.3 forcats_1.0.0   stringr_1.5.1   dplyr_1.1.4     purrr_1.0.2    
 [7] readr_2.1.5     tidyr_1.3.1     tibble_3.2.1    ggplot2_3.5.0   tidyverse_2.0.0 RPostgres_1.4.6
[13] emo_0.0.0.9000 

loaded via a namespace (and not attached):
  [1] cfbfastR_1.9.0     DBI_1.2.2          remotes_2.5.0      writexl_1.5.0      rlang_1.1.3       
  [6] magrittr_2.0.3     snakecase_0.11.1   furrr_0.3.1        compiler_4.3.3     mgcv_1.9-1        
 [11] callr_3.7.6        png_0.1-8          systemfonts_1.0.6  vctrs_0.6.5        reshape2_1.4.4    
 [16] sysfonts_0.8.9     rvest_1.0.4        profvis_0.3.8      crayon_1.5.2       pkgconfig_2.0.3   
 [21] fastmap_1.1.1      backports_1.4.1    ellipsis_0.3.2     labeling_0.4.3     utf8_1.2.4        
 [26] promises_1.2.1     rmarkdown_2.26     sessioninfo_1.2.2  tzdb_0.4.0         ps_1.7.6          
 [31] bit_4.0.5          xfun_0.43          showtext_0.9-7     cachem_1.0.8       jsonlite_1.8.8    
 [36] blob_1.2.4         later_1.3.2        jpeg_0.1-10        broom_1.0.5        parallel_4.3.3    
 [41] R6_2.5.1           stringi_1.8.3      espnscrapeR_0.8.0  parallelly_1.37.1  pkgload_1.3.4     
 [46] assertthat_0.2.1   Rcpp_1.0.12        knitr_1.45         usethis_2.2.3      pacman_0.5.1      
 [51] nnet_7.3-19        Matrix_1.6-5       httpuv_1.6.15      splines_4.3.3      timechange_0.3.0  
 [56] tidyselect_1.2.1   rstudioapi_0.16.0  yaml_2.3.8         codetools_0.2-19   miniUI_0.1.1.1    
 [61] websocket_1.4.1    curl_5.2.1         processx_3.8.4     listenv_0.9.1      pkgbuild_1.4.4    
 [66] lattice_0.22-6     plyr_1.8.9         withr_3.0.0        shiny_1.8.1        evaluate_0.23     
 [71] desc_1.4.3         future_1.33.1      RcppParallel_5.1.7 urlchecker_1.0.1   xml2_1.3.6        
 [76] pillar_1.9.0       generics_0.1.3     chromote_0.2.0     hms_1.1.3          munsell_0.5.0     
 [81] globals_0.16.3     xtable_1.8-4       glue_1.7.0         janitor_2.2.0      mlbplotR_1.1.0    
 [86] tools_4.3.3        data.table_1.15.2  fs_1.6.3           grid_4.3.3         devtools_2.4.5    
 [91] colorspace_2.1-0   nlme_3.1-164       patchwork_1.2.0    showtextdb_3.0     proto_1.0.0       
 [96] baseballr_1.6.0    cli_3.6.2          fansi_1.0.6        gt_0.10.1          V8_4.4.2          
[101] gtable_0.3.4       digest_0.6.35      farver_2.1.1       htmlwidgets_1.6.4  memoise_2.0.1     
[106] htmltools_0.5.8    lifecycle_1.0.4    httr_1.4.7         mime_0.12          bit64_4.0.5

Solution

  • In Rstudio ; change your graphics device backend to 'AGG'

    Tools > Global Options > Graphics >>> AGG