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

3
下面的代码生成一个图表,其中点形状被表情符号替代。enter image description here
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"))

很容易看出,图例的信息量不够充分。与其两次使用有颜色的字母a,最好使用有颜色的表情符号;而黑色表情符号可以用单词tulipcake代替。

这个需求可行吗?


这个链接解释了如何从图例中删除“a”。但是让表情符号变成彩色是另一个问题。 - Jared C
你可以保存图片并使用这个解决方案:https://dev59.com/_WYs5IYBdhLWcg3wGf_F - Jared C
1个回答

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 

情节:

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

使用表情符号绘制的图形

不知为何,我电脑上实际显示的郁金香/蛋糕表情符号看起来有所不同...


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接