For some reason, using other than the default font in plots has been a major problem for me in R.
Supposedly, one can use the
extrafont package to manage all of that but I found it too cumbersome.
Instead, I found out that the
showtext package can make my life easier.
Even though working with text in plot is not yet completely free of troubles,
showtext has made many things easier.
Now, I can finally choose fonts freely and even use icons.
This blogposts gives you a how-to so that you can do that too.
Import and Use Fonts with showtext
A great source for fonts is Google’s font page. What is great abut this page is that it can display texts in many different fonts.
Once we found a nice font, we can use its name to make it available within R.
This is done with
Let’s import a couple of random fonts.
Notice that we have also used
This is necessary for
showtext to take over the show.
Otherwise, the new fonts would not be usable.
Now, let’s take a look at our new fonts.
You may wonder why we have used
We did this in order to ensure that the x-axis is not centered at 0 and our example texts won’t be outside of the plot.
Personally, I find this somewhat tedious but this can’t be helped, I guess.
With text elements we always run at the risk of writing outside of the plot area.
Next, let’s make our use of fonts somewhat more practical. In my last blog post, I stressed the use of highlighting a few important things instead of using many colors. Combine this with direct labels instead of a legend and you get this plot I created using the Fira Sans font.
Now, see what it would look like had I used the Lobster font instead.
Feels different doesn’t it? And this is still different than the Anton font.
Import and Use Icon Fonts with showtext
We can not only use regular text fonts but also icons with
For example, we may want to use one of the free Fontawesome icons.
To do so, download the newest version and extract the .otf-files into your working directory.
These contain the font information that you need.
Importing these (and any other font for that matter) works with
font_add() and the path to the .otf-files.
Now that we imported the fonts, we can use
geom_richtext() and some HTML wizardry to add icons to our previously imported fonts from Google.
But first, what we need is an icon’s unicode identifier?
The easiest way to find that is to stroll through the Fontawesome icons online. Then, find one that matches the font you want to use, e.g. free and solid. Finally, find it’s unicode character in the corresponding popup menu.
Once you got this, you can add
&#x in front of the unicode and wrap
<span> tags around it.
Within these tags, you will have to specify
font-family so that the icon is rendered.
This way, you can also use icons in scatter plots.
Though, make sure to set
fill=NA if you do not want to have white boxes around the icons.
You will notice that using the two previous code chunks will generate a lot of warnings about “native encoding”. So far, I have always been able to ignore these without any trouble. I really don’t know why they appear. And if you know, please let me know in the comments below.