在ggplot2中使用FontAwesome代替点 [英] Using FontAwesome in ggplot2 as a replacement for points

查看:81
本文介绍了在ggplot2中使用FontAwesome代替点的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这可能是很基本的事情,但是我似乎找不到有意义的答案.

This is probably a fairly basic thing, but I can't seem to find a meaningful answer.

我正在尝试使用R中的fontawesome包将表情符号用作ggplot2图表中的点,但是我很难弄清楚如何为每个变量分配不同的表情符号.

I am trying to use the fontawesome package in R to use emojis as points in a ggplot2 chart, but I am having a hard time figuring out how to assign a different emoji to each variable.

以下是一些示例数据(我们将数据框称为示例"):

Here's some sample data (we'll call the data frame 'sample'):

sample <- structure(list(month = c(1L, 2L, 3L, 4L, 5L, 6L, 1L, 2L, 3L, 
4L, 5L, 6L), number = c(456550L, 374616L, 462086L, 436376L, 497526L, 
488574L, 119884L, 126356L, 129338L, 177496L, 128404L, 156754L
), variable = c("var1", "var1", "var1", "var1", "var1", "var1", 
"var2", "var2", "var2", "var2", "var2", "var2")), class = "data.frame", row.names = c(NA, 
-12L))

我查看了示例代码来自软件包作者,但是我想不出一种方法来为var1和var2分配一个单独的表情符号.

I've looked at the example code from the package author, however I can't figure out a way to assign a separate emoji to var1 and var2.

使用 FontAwesome速查表,假设我希望var1为"fa-windows"和var2成为"fa-linux".这是我从程序包作者的示例中编写的一些绘图代码:

Using the FontAwesome cheatsheet, let's say that I wanted var1 to be 'fa-windows' and var2 to be 'fa-linux'. Here's a bit of plot code I worked up from the package author's example:

library(ggplot2)
library(emojifont)
library(scales)

load.fontawesome()
testLabels <- fontawesome(c('fa-windows','fa-linux'))

windows() #only necessary if using RStudio

ggplot(sample,aes(x=month,y=number,color=variable))+
  geom_text(aes(label=testLabels),family='fontawesome-webfont', size=6)+
  scale_y_continuous(labels = comma)+
  theme(legend.text=element_text(family='fontawesome-webfont'))

这将引发错误:错误:美学必须长度为1或与数据相同",这并不奇怪,因为我没有将var1和var2设置为与testLabels中任何地方的相关FontAwesome值相对应代码.但是,我不知道该怎么做,也无法在线找到答案(也许我的搜索技能不足).

This throws an error: "Error: Aesthetics must be either length 1 or the same as the data", which is not surprising as I haven't set var1 and var2 to correspond with the relevant FontAwesome values in testLabels anywhere in the code. However, I don't know how to do that, and I can't find the answer online (perhaps my search skills are lacking).

我确信拥有更多ggplot2经验的用户可以帮助我快速找到答案-预先感谢!

I am sure that users with more ggplot2 experience can help me find the answer quickly - thanks in advance!

推荐答案

labs <- data.frame(variable=c("var1", "var2"),
                   label = fontawesome(c('fa-windows','fa-linux')))
d <- merge(sample, labs, by.x="variable", by.y="variable")
ggplot(d,aes(x=month,y=number,color=variable))+
  geom_text(aes(label=label),family='fontawesome-webfont', size=6)+
  scale_y_continuous(labels = comma)+
  theme(legend.text=element_text(family='fontawesome-webfont'))

请勿在aes中使用仅应用于美观映射的变量.

never use variable in aes which should only use for aesthetic mapping.

这篇关于在ggplot2中使用FontAwesome代替点的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆