How to Display Emojis in Ggplot2 Using Emo Package in R

Using FontAwesome in ggplot2 as a replacement for points

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

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

ggplot legend with the use of emojis

One option to achieve your desired result would be via a custom key glyph function which replaces the default rects used by geom_bar by your symbols:

  1. For the custom draw_key_symbol function I adpated ggplot2::draw_key_text to your use case.
  2. To pick the right symbols I first set up palettes of colors and symbols which assign colors and symbols to genders. The right symbol is then picked based on the fill color via symbols[names(match.arg(data$fill, pal))]
  3. Instead of symbol I map Gender on the fill aes and use scale_fill_manual to set the colors.

Note: While I use the default ggplot2 colors the code below should also work for custom colors for the genders.

library(ggplot2)
library(grid)
library(emojifont)

load.emojifont("OpenSansEmoji.ttf")

gender <- data.frame(
`Gender` = c("Male", "Female"),
`Proportion` = c(45, 55),
symbol = c("\UF182", "\UF183")
)

# Color palette
pal <- scales::hue_pal()(2)
names(pal) <- c("Female", "Male")
# Symbol palette
symbols <- c("\UF182", "\UF183")
names(symbols) <- c("Female", "Male")

draw_key_symbol <- function(data, params, size) {
data$label <- symbols[names(match.arg(data$fill, pal))]

grid::textGrob(data$label, 0.5, 0.5,
gp = grid::gpar(col = data$fill,
fontfamily = "fontawesome-webfont",
fontsize = 3.88 * .pt))
}

ggplot(gender, aes(x = "", Proportion, fill = Gender)) +
geom_bar(width = 1, stat = "identity", key_glyph = "symbol") +
scale_fill_manual(values = pal) +
coord_polar("y", start = 0) +
labs(x = NULL, y = NULL)

Sample Image

geom_emoji() function does not accept input if it's stored in a variable

I don't think the developer anticipated this use. The relevant section of their code here on Github assumes that you're passing a named parameter with a string literal all the time.

This code below will work for your use. But I don't think you can easily plot multiple emojis on a single plot using aesthetics as you would for other ggplot2 based extensions. There may be other packages that plot symbols that might fit your needs.

library(emoGG)
#> Loading required package: ggplot2
library(ggplot2)
Pic <- "1f337"
ggplot(iris, aes(Sepal.Length, Sepal.Width, color = Species)) +
do.call(geom_emoji, list(emoji = Pic))

Sample Image

Created on 2019-11-22 by the reprex package (v0.3.0)

Save graph with emojis in label as pdf in R

Using the comment by @charlie-gallagher, I was able to figure out a solution.

The first step is to export the full graph (with text labels, arrows, etc.) directly as an .svg.

svg = export_svg(grViz("mygraph.dot"))
write(svg, "mygraph.svg")

After the .svg file is created, I just used another application to convert the svg to a pdf.

emo::ji won't render in knitted documents

After hours of searching I found that the problem was with the native encoding of my session. As presented in this answer, I did File -> Reopen with Encoding... and selected UTF-8, and for good measure selected UTF-8 to be the default in the future. Knitted the file - worked like a charm.



Related Topics



Leave a reply



Submit