【问题标题】:Plot emojis/emoticons in R with ggplot使用 ggplot 在 R 中绘制表情符号/表情符号
【发布时间】:2026-01-17 06:10:01
【问题描述】:

我正在尝试在 ggplot2 中使用表情符号制作情节。我想要的是显示表情符号而不是标签或显示为 geom。 Stack 中的This question 和我的问题很相似,但也没有解决,答案很手动,我需要自动(我的真实数据框有 3000 行)。

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

在尝试了不同的解决方案后,我感到很困惑,因此我们将不胜感激。我阅读了不同的方法来解决问题,例如hereherehere

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

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

但它不起作用。

我的数据是这样的,name_emoji 来自emo package,我也可以改成字节:

               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"

在这里复制:

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

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

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

这是我的会议:

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  

【问题讨论】:

标签: r plot ggplot2 emoticons


【解决方案1】:

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

library(emojifont)
library(ggplot2)
load.emojifont("EmojiOne.ttf")
quartz()
ggplot(foo, aes(name_emoji, n, label = emoji)) + 
  geom_bar(stat = "identity") +
  geom_text(family = "EmojiOne", size = 6, vjust = -.5) +
  scale_x_discrete(breaks = foo$name_emoji, labels = foo$emoji) +
  theme(axis.title.x=element_blank(),
        axis.text.x=element_blank(),
    axis.ticks.x=element_blank())

需要用到quartz就看this

你也可以这样用:

library(gridSVG)
ps = grid.export("emoji.svg", addClass=T)

你会得到?✌️:

我认为将表情符号调整到水平轴可能是一种手动解决方案,但现在对表情符号来说已经足够了。但是,如果有人知道为什么以前的解决方案可以在 Linux 上运行,但不能在 Window 或 Mac 上运行,我会留下这个问题

【讨论】:

  • 请注意,如果您在 R Markdown 文档中使用 emojifont,则需要设置块选项fig.showtext = TRUE
【解决方案2】:

我尝试了以下方法:

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

【讨论】:

  • 这对我不起作用,而不是表情符号出现白色方块,我用输出和我的信息会话编辑我的问题... :'(
  • 您能否提供更多关于您如何获得它的信息?我看到 RStudio 中的绘图无法正常工作。但是将绘图保存为 .svg 也无法正常工作...
  • 我发布的方法是在 Linux 机器上制作的,但在 Windows 上我也只能得到白色方块......
  • 感谢您的信息!这让我发疯了。使用geom_text 似乎更容易绘图。