Add icons on your R plot
May 23, 2017
R plot fonts icons
showtext
utils
graphics
Kevin Cazelles, David Beauchesne
Table of Contents
Introduction
Icons are very popular on websites to improve the visual grepping (i.e. visual optimization of eyeball search). We all know how to go at the top or the bottom of a website clicking on chevrons ( ). We are also familiar with simple symbols that allow us to click and edit the text we are typing ( ). Moreover, some web applications are so widespread than their logo appears in thousands of websites (i.e Twitter or GitHub ). Here, we explain how to use these icons in your R plots. Why use icons on an R plot? It simply increases the possibilities for the number of markers you could use and thus efficiently and usefully refine your plots.
There are two strategies to include icons on a R plot:
- using icons as a font when possible;
- using icons as images.
Here, we exemplify the first strategy and leave the second one for another post (hopefully). We will use icons from three different iconic frameworks:
All of these frameworks propose either a .ttf
(TrueType Font) or a .otf
file (OpenType Font) or both. These files make our life very easy and we can
make a direct link with a previous blog post.
Setting up R
List of packages to be used
- the
showtext
package and its dependencysysfont
(see details on line ); - the
magrittr
package for the pipes (see the associated websites).
Here is line of code you need to install these packages:
|
|
Then, we load them:
|
|
Material to be downloaded
We first need to download the 3 fonts we will be using. Links to retrieve them are available below and the next code chunk describes how use the R to get these fonts.
If you have decided to downloaded theses fonts, you can then either install them
on your computer (there is most likely an application on your computer to do so
and you will be able to use them in other pieces of software e.g. LibreOffice) or
store them in a dedicated folder whose path will be denoted dr
hereafter.
|
|
Loading the fonts
To activate the facilities showtext
provides, use showtext_auto()
(by the way, the author of the package deserves a medal!):
|
|
We add the path to our fonts:
|
|
Then, we add academicons.ttf
, FontAwesome.otf
and ionicons.ttf
to our
session with the font_add()
function:
|
|
Here, things are going to be a little bit trickier than they were in the Hylian blog post, as we should first locate the unicode characters to be used. Fortunately, this is fairly doable:
- FontAwesome provides a very helpful cheatsheet;
.ttf
files are easy to explore, for instance using LibreOffice and inserting a special character (see the figure below).
To use unicode characters in R, there are several possibilities, this blog post explains it well. Below, we use different way to deal with the unicode characters.
Let’s add icons our plot
Academicons
There are 36 unicode characters available. They are numbered like so: “E9” +
numbers ranging from 00 to 35 (version 1.8.0). To use them, we create a
sequence using sprintf()
and paste0()
function:
|
|
We now plot them as if they were on a regular grid and we add the decimal value below:
|
|
Adding FontAwesome’s icons
The unicode hexadecimal for the FontAwesome’s icons range from f000
() to f2e0
() in version 4.7. Version
|
|
Adding Ionicons
The unicode decimal value of Ionicons’s icons range from 61696 to 62711.
There are 765 icons available (version 2.0.1), therefore some hexidecimal are
not assigned. Here we’ll use the intToUtf8()
function to convert the decimal
value into an unicode character.
|
|
Combining them
To combine, we have to switch from one font to another. Here is a simple example that combines the three icon frameworks.
|
|
Additional remarks
-
Note that the steps described here are useful to include any kind of police you have on your computer or you may find online. Therefore this post should help you deal with font if you intend to publish a figure in a journal that requests a specific font. By the way,
showtext
allows you to search directly on Google Fonts (see thefont_add_google()
function). -
As you may have realized reading this post there are different ways to write a character in unicode. Fortunately, many online converters help you get from one standard to another (e.g. unicodeconverter does the job).
-
You may also want to use emoji to cheer your plot up! See the emojifont package available on CRAN and Github. It now support fontawesome as explained here.
-
fontstruct is a great web application that helps you creating your own font (you can save your font as a
.tff
on your computer and now you know how to deal with it!)
Session info
|
|
Edits
Nov 11, 2017 -- Fix paths to icon files
Apr 15, 2018 -- Change URLs to download Font-Awesome
Mar 18, 2020 -- Fix image links
Apr 23, 2022 -- Add 'archived' badge.
Feb 4, 2023 -- Edit headers.