Right now I am trying to put icons in ggplot visualizations. To this end, I have checked a couple of tutorials on the web and when I run the following code example of Claus Wilke,
library(ggtext)
library(ggplot2)
ggplot(mtcars) +
aes(mpg, disp) +
geom_point() +
labs(title = "This font is awesome:
<span style='font-family: \"Font Awesome 6 Free Solid\"'>  </span>") +
theme(plot.title = element_markdown())
everything seems fine as you can see in the following result
However, to imitate this code for another visualization of mine, let's say the last week's TidyTuesday
challenge of Babynames
data, I am using the following code,
library(ggplot2)
library(ggtext)
library(tidyverse)
library(showtext)
library(ggrepel)
font_add_google("Martel", family = "title")
font_add_google("Libre Caslon Display", family = "subtitle")
font_add_google("Space Mono", family = "axis")
font_add_google("Spartan", family = "caption")
showtext_auto()
tuesdata <- tidytuesdayR::tt_load('2022-03-22')
babynames <- tuesdata$babynames
babynames %>%
group_by(year) %>%
top_n(1, n) %>%
ggplot(aes(x = year, y = prop)) +
geom_step(size = 0.5, show.legend = FALSE, direction = "hv") +
theme_bw() +
labs(x = "Year",
y = "Proportion",
title = "How Unique are Names in the United States?",
subtitle = "This visualization illustrates the proportion of most given baby names in that year between 1880 and 2017",
caption = "Source: {babynames} package | Plot: <span style='font-family: \"Font Awesome 6 Brands\"'></span> muhammetozkrca | TidyTuesday-Week 12") +
background_grid(major = 'none', minor = "none") +
theme(
plot.title = element_text(hjust = 0.5, family = "title", size = 20),
plot.subtitle = element_markdown(hjust = 0.5, family = "subtitle", size = 14), # note that to color numbers differently, I used element_markdown not element_text function
plot.caption = element_markdown(hjust = 0.5, size = 7),
legend.position = c(0.9, 0.6),
legend.justification = "center",
legend.title = element_text(family = "caption", hjust = 1, vjust = 0.7),
legend.title.align = 0.5,
axis.title.x = element_text(family = "axis"),
axis.title.y = element_text(family = "axis"),
panel.border = element_blank(),
axis.ticks = element_blank())
and I received this error
Error in grid.Call(C_textBounds, as.graphicsAnnot(x$label), x$x, x$y, :
polygon edge not found
Sometimes, I also receive this error as well
In grid.Call.graphics(C_text, as.graphicsAnnot(x$label), x$x, x$y, :
font family 'Font Awesome 6 Free Solid' not found, will use 'wqy-microhei' instead
To sum up, I do not really understand what's going on. When I searched similar inquiries, I saw that the problem is mostly due to the fact that users do not have font files on their pc. Yet, if this would be the problem for me, I do not think that I can reproduce Wilke's example. Therefore, if you can assist me in solving this problem, I will be grateful for that. Thank you in advance.
The issue is that you use
showtext
for the text rendering. Doing so requires to add local fonts viafont_add
so that they could be used in the plot:DATA