Randall Helms Randall Helms - 1 year ago 131
R Question

Using FontAwesome in ggplot2 as a replacement for points

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

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'):

month number variable
1 456550 var1
2 374616 var1
3 462086 var1
4 436376 var1
5 497526 var1
6 488574 var1
1 119884 var2
2 126356 var2
3 129338 var2
4 177496 var2
5 128404 var2
6 156754 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.

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:


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

windows() #only necessary if using RStudio

geom_text(aes(label=testLabels),family='fontawesome-webfont', size=6)+
scale_y_continuous(labels = comma)+

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

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

Answer Source
labs <- data.frame(variable=c("var1", "var2"),
                   label = fontawesome(c('fa-windows','fa-linux')))
d <- merge(sample, labs, by.x="variable", by.y="variable")
  geom_text(aes(label=label),family='fontawesome-webfont', size=6)+
  scale_y_continuous(labels = comma)+

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

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download