r - ggplot : legend for emojis in plot

标签 r ggplot2 legend emoji

以下代码生成了一个带有表情符号而不是点形状的图表。 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

这能实现吗?

最佳答案

我之前对表情符号字体的了解不多,但以下内容对您有用吗?

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

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

plot with emoji symbols

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

关于r - ggplot : legend for emojis in plot,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53713233/

相关文章:

r - 第一个文档的 `_bookdown.yml` 、 `_output.yml` 和 yaml header 有什么区别?

r - 提取日期和时间并建立索引,其中 R 中的详细 OHLC 数据具有不同的打开和关闭时间

r - R Markdown ioslides 的自定义 CSS 应该存储在哪里?

ggplot 中facet_grid 中的按行着色

matlab - 如何在matlab中添加条形图的三个图例?

r - 如何指定emacs中ESS session 打开的R版本?

r - 您可以在现有轴下添加标签吗?

r - 如何在R中创建n个具有不同颜色的图?

python - 如何设置图例标记大小和 alpha?

asp.net - 图表.js。销售点图例模板