【问题标题】:ggplot: legend for emojis in plotggplot:情节中表情符号的图例
【发布时间】:2018-12-10 20:34:31
【问题描述】:

以下代码生成带有表情符号而不是点形状的图表。

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这两个词。

这可以实现吗?

【问题讨论】:

标签: r ggplot2 legend emoji


【解决方案1】:

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

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

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

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2015-11-03
    • 2021-11-03
    • 1970-01-01
    • 2014-11-18
    • 2014-03-27
    • 2016-10-28
    • 1970-01-01
    • 2023-02-06
    相关资源
    最近更新 更多