ggplot:情节中表情符号的图例

Eri*_*rth 3 r legend ggplot2 emoji

以下代码生成带有表情符号而不是点形状的图表。在此输入图像描述

library(tidyverse)
library(emojifont)
load.emojifont("OpenSansEmoji.ttf")

pal <- c("\U1f337"="blue","\U1f370"="red")
set.seed(124)
xdf <- data_frame(x=rnorm(10),y=rnorm(10),
                  label=rep(c("\U1f337","\U1f370"),5))
xdf %>% ggplot(aes(x=x,y=y,label=label,color=factor(label))) +
  geom_text(family="OpenSansEmoji") +
  scale_color_manual("object",values=pal) +
  guides(color=guide_legend(labels=FALSE)) +
  theme(legend.text=element_text(family="OpenSansEmoji"))
Run Code Online (Sandbox Code Playgroud)

很容易看出,这个传说的信息量非常大。如果能用彩色表情符号代替两倍的彩色字母就好了a,我希望用单词tulip和代替黑色表情符号cake

这能实现吗?

Z.L*_*Lin 5

我以前很少使用表情符号字体,但是以下内容对您有用吗?

数据整理(为了方便起见,我更喜欢将标签列重命名为符号,以将其预期用途分开,但您的情况可能会有所不同):

xdf2 <- xdf %>%
  rename(symbol = label) %>%
  mutate(label = ifelse(symbol == "\U0001f337", "tulip", "cake"))

> xdf2
# A tibble: 10 x 4
         x      y symbol       label
     <dbl>  <dbl> <chr>        <chr>
 1 -1.39    0.318 "\U0001f337" tulip
 2  0.0383 -1.42  "\U0001f370" cake 
 3 -0.763  -0.405 "\U0001f337" tulip
 4  0.212   0.995 "\U0001f370" cake 
 5  1.43    0.959 "\U0001f337" tulip
 6  0.744   0.918 "\U0001f370" cake 
 7  0.700  -0.151 "\U0001f337" tulip
 8 -0.229  -1.22  "\U0001f370" cake 
 9  0.197  -0.869 "\U0001f337" tulip
10  1.21   -1.04  "\U0001f370" cake 
Run Code Online (Sandbox Code Playgroud)

阴谋

xdf2 %>%
  ggplot(aes(x = x, y = y, shape = symbol, color = label)) +
  geom_point() +
  scale_shape_identity() +
  scale_color_manual(values = c("tulip" = "blue",
                                "cake" = "red"),
                     guide = guide_legend(
                       override.aes = list(shape = c("\U0001f370",
                                                     "\U0001f337"))
                     ))
Run Code Online (Sandbox Code Playgroud)

带有表情符号的绘图

(由于某种原因,我的机器上的实际郁金香/蛋糕表情符号看起来不同......)