使用 ggplot 在 R 中绘制表情符号/表情符号

Tit*_*anz 5 plot r emoticons ggplot2

我正在尝试在 ggplot2 中用表情符号绘制一个情节。我想要的是显示表情符号而不是标签或像 geom 一样显示。Stack 中的这个问题与我的问题非常相似,但它也没有解决它,答案非常手动,我需要自动进行(我的真实数据框有 3000 行)。

只是为了在下面的图中澄清,我想显示表情符号而不是文本标签或条形图。但似乎不可能。

在此处输入图片说明

在尝试了不同的解决方案后,我堆积如山,所以任何帮助将不胜感激。我阅读了不同的方式来面对这个问题,比如这里这里这里

我最后一次尝试是这样的:

mult_format <- function() {
  function(x) emo::ji(x)
}
ggplot(foo, aes(name_emoji, n)) + 
  geom_bar(stat = "identity") +
  coord_flip() +
  scale_y_continuous(labels = mult_format())
Run Code Online (Sandbox Code Playgroud)

但它不起作用。

我的数据看起来像这样,name_emoji 来自emo 包,我也可以将其更改为字节:

               name_emoji     n        emoji
                    <chr> <int>        <chr>
 1            closed book     1 "\U0001f4d5"
 2          confused face     7 "\U0001f615"
 3                  dizzy     20 "\U0001f4ab"
 4 face with tears of joy     1 "\U0001f602"
 5          flexed biceps     1 "\U0001f4aa"
 6             light bulb     1 "\U0001f4a1"
Run Code Online (Sandbox Code Playgroud)

这里重现:

structure(list(name_emoji = c("closed book", "confused face", 
"dizzy", "face with tears of joy", "flexed biceps", "light bulb"
), n = c(1L, 7L, 20L, 1L, 1L, 1L), emoji = c("\U0001f4d5", "\U0001f615", 
"\U0001f4ab", "\U0001f602", "\U0001f4aa", "\U0001f4a1")), .Names = c("name_emoji", 
"n", "emoji"), row.names = c(NA, -6L), class = c("tbl_df", "tbl", 
"data.frame"))
Run Code Online (Sandbox Code Playgroud)

提前致谢!(我要讨厌表情符号了 :) )

使用 J_F 和 sessionInfo 回答后的输出进行编辑

foo$name_emoji <- as.factor(foo$name_emoji)
foo$emoji <- as.factor(foo$emoji)

ggplot(foo, aes(name_emoji, n)) + 
  geom_bar(stat = "identity") +
  scale_x_discrete(breaks = foo$name_emoji, labels = foo$emoji) +
  coord_flip()
Run Code Online (Sandbox Code Playgroud)

在此处输入图片说明

这是我的会话:

R version 3.4.3 (2017-11-30)
Platform: x86_64-apple-darwin15.6.0 (64-bit)
Running under: macOS High Sierra 10.13.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/3.4/Resources/lib/libRlapack.dylib

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

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

other attached packages:
 [1] emo_0.0.0.9000     emojifont_0.5.1   
 [3] DataCombine_0.2.21 forcats_0.2.0     
 [5] stringr_1.2.0      dplyr_0.7.4       
 [7] purrr_0.2.4        readr_1.1.1       
 [9] tidyr_0.7.2        tibble_1.3.4      
[11] ggplot2_2.2.1      tidyverse_1.2.1   

loaded via a namespace (and not attached):
 [1] Rcpp_0.12.14        lubridate_1.7.1    
 [3] lattice_0.20-35     twitteR_1.1.9      
 [5] sysfonts_0.7.1      assertthat_0.2.0   
 [7] digest_0.6.12       psych_1.7.8        
 [9] mime_0.5            R6_2.2.2           
[11] cellranger_1.1.0    plyr_1.8.4         
[13] httr_1.3.1          rlang_0.1.4        
[15] lazyeval_0.2.1      readxl_1.0.0       
[17] rstudioapi_0.7      data.table_1.10.4-3
[19] miniUI_0.1.1        proto_1.0.0        
[21] labeling_0.3        foreign_0.8-69     
[23] bit_1.1-12          munsell_0.4.3      
[25] shiny_1.0.5         broom_0.4.3        
[27] compiler_3.4.3      httpuv_1.3.5       
[29] modelr_0.1.1        pkgconfig_2.0.1    
[31] mnormt_1.5-5        htmltools_0.3.6    
[33] crayon_1.3.4        showtextdb_2.0     
[35] grid_3.4.3          xtable_1.8-2       
[37] nlme_3.1-131        jsonlite_1.5       
[39] gtable_0.2.0        DBI_0.7            
[41] magrittr_1.5        scales_0.5.0       
[43] cli_1.0.0           stringi_1.1.6      
[45] reshape2_1.4.2      bindrcpp_0.2       
[47] xml2_1.1.1          rjson_0.2.15       
[49] tools_3.4.3         showtext_0.5       
[51] bit64_0.9-7         glue_1.2.0         
[53] hms_0.4.0           parallel_3.4.3     
[55] yaml_2.1.15         colorspace_1.3-2   
[57] rvest_0.3.2         bindr_0.1          
[59] haven_1.1.0  
Run Code Online (Sandbox Code Playgroud)

Tit*_*anz 6

因为提供的解决方案似乎取决于操作系统,所以我在下面留下了一个粗略的解决方案,但仍然保留问题,以防有人有解决方案。感谢@J_F的支持。

\n\n
library(emojifont)\nlibrary(ggplot2)\nload.emojifont("EmojiOne.ttf")\nquartz()\nggplot(foo, aes(name_emoji, n, label = emoji)) + \n  geom_bar(stat = "identity") +\n  geom_text(family = "EmojiOne", size = 6, vjust = -.5) +\n  scale_x_discrete(breaks = foo$name_emoji, labels = foo$emoji) +\n  theme(axis.title.x=element_blank(),\n        axis.text.x=element_blank(),\n    axis.ticks.x=element_blank())\n
Run Code Online (Sandbox Code Playgroud)\n\n

在此输入图像描述\n需要使用quartz就看这个

\n\n

您还可以使用这个:

\n\n
library(gridSVG)\nps = grid.export("emoji.svg", addClass=T)\n
Run Code Online (Sandbox Code Playgroud)\n\n

你得到 \xe2\x9c\x8c\xef\xb8\x8f:\n在此输入图像描述

\n\n

我认为将表情符号调整到水平轴可能是一个手动解决方案,但目前对于表情符号来说这已经足够了。不过,我保留这个问题,以防有人知道为什么之前的解决方案在 Linux 上有效,但在 Window 或 Mac 上无效

\n


J_F*_*J_F 3

我尝试了以下方法:

foo$name_emoji <- as.factor(foo$name_emoji)
foo$emoji <- as.factor(foo$emoji)

ggplot(foo, aes(name_emoji, n)) + 
  geom_bar(stat = "identity") +
  scale_x_discrete(breaks = foo$name_emoji, labels = foo$emoji) +
  coord_flip()
Run Code Online (Sandbox Code Playgroud)

在此输入图像描述