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 rect
s used by geom_bar
by your symbols:
- For the custom
draw_key_symbol
function I adpatedggplot2::draw_key_text
to your use case. - 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))]
- Instead of
symbol
I mapGender
on thefill
aes and usescale_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)
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))
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
Deleting Every N-Th Row in a Dataframe
Different Colour Palettes for Two Different Colour Aesthetic Mappings in Ggplot2
Should I Avoid Programming Packages with Pipe Operators
Extract First Word from a Column and Insert into New Column
Combinations of Multiple Vectors in R
Dictionary() Is Not Supported Anymore in Tm Package. How to Emend Code
Substitute Dt1.X with Dt2.Y When Dt1.X and Dt2.X Match in R
How to Order Bars Within All Facets
Spacing Between Boxplots in Ggplot2
Calculating Percentile of Dataset Column
Use Loop to Generate Section of Text in Rmarkdown
Programming-Safe Version of Subset - to Evaluate Its Condition While Called from Another Function
Finding Non-Numeric Data in a Data Frame or Vector
Canonical Tidyverse Method to Update Some Values of a Vector from a Look-Up Table
Aggregating All Unique Values of Each Column of Data Frame
Extract Digit from Numeric in R
Difference Between 'Names(Df[1]) <- ' and 'Names(Df)[1] <- '