3MW (Use custom fonts and icons in ggplot)

Guten Tag!

Many greetings from Ulm, Germany. As always, a short announcement before we dive into this week’s newsletter:

I’ve released a new Quarto video on YouTube. In this week’s video, I show you how to use your favorite Google fonts in Quarto while also complying with GDPR, i.e. data privacy laws in Europe. Check it out on YouTube.

Speaking about fonts, in today’s newsletter I want to show you how to use your favorite fonts (or even icons) in ggplot. This sounds pretty easy but for some reason this was a major headache on Windows for me.

Astonishingly, Windows often had trouble finding the installed font. But using the {ragg} package makes it super easy to use any font you like. Even on Windows. Here’s how it works. (And of course all the code is available on GitHub.)

Setup the {ragg} package

It couldn’t be easier to setup {ragg}. Just do a good old-fashioned install.packages('ragg') and set RStudio’s graphics device to AGG.

Once that is done, you can use any font that you have installed on your computer locally without any hassle. Fancy trying this out? Just head to Google fonts and download, say, the Tektur font, and install it locally (just unzip and install all the .ttf-files).

Now, we can do a basic plot that has a title and style that title to use the Tektur font with theme().

The resulting plot isn’t really pretty but the font is cool. Check it out:

Use FontAwesome Icons with {ragg}

The same idea works for icons like FontAwesome icons. Just head over to their website and download the free icons (for desktop).

Once you’ve downloaded and installed the icon files, you can just add, say, a heart icon to our previous plot with an annotation.

Notice that the text label here is just the icon name that you can get from the FontAwesome website.

Also, the name of the font family may be a bit confusing. Who would have guessed that you’d have to use the name 'Font Awesome 6 Free'? Not me. That’s why I had to look up the name under which Windows installed my font.

Thankfully, looking for the word “Awesome” in the installed fonts returned the correct name in no time. The way to do that is to combine the output of systemfonts::system_fonts() with filter() and str_detect().

And armed with the font family name, you can enhance your plot with all the icons you desired. In our case, we added a nice little heart.

What’s with the solid fonts?

Now if you’re like me, then you’re not satisfied with just a heart outline. You want the heart to be filled. This means that you need the solid FontAwesome icon and not the regular one. But here’s a really dumb story: Both, the solid and the regular font, have the same family name. They are only distinguishable by their “solid” and “regular” style (which ggplot does not understand). 🤦 

Notice that the family name is the same for two different font names. One of these files has the style “solid”. Thus, this is likely the font we need to access to fill our heart.

So let’s get the path for this font file and pass it to systemfonts::register_font(). This way, we can register the font files under a name that we desire. How about “Font Awesome 6 Free Solid”?

You should keep this technique in mind in case you run into other fonts with styles that ggplot does not understand. But beware, you will have to redo this fix every time you reload your R session.

In any case, we can use this new name to get the icon that our heart truly desires (yes this is a dumb pun. You know, because apparently our heart desires a heart icon).

That’s it for today. Hope you’ve enjoyed this week’s newsletter. If you want to reach out to me, just reply to this mail or find me on Twitter.

See next week,
Albert 👋

If you like my content, you may also enjoy these:

Reply

or to participate.