5 steps to import new fonts in R
A summary after reading many StackOverflow posts, now with codes!


Recently, I was trying to add some retro flavor into my R charts and came across this article. I had a great time exploring the retro themes but I also find it difficult to add the custom fonts into the charts.
This article summarizes the steps I used to import the new fonts into R after reading many Q&A in StackOverflow.
1. Download the new fonts online
There are many free fonts online to download. When you download the fonts, they are typically TTF (True Type font) files in a zip folder.
2. Open the downloaded TTF (True Type font) file (in the zip folder)
Unzip the zipped file and locate the True Type font file within. Double click and install it.


3. Open a new R session and install the extrafonts
package
Execute the following codes to install the extrafonts
package:
#Install extrafont from CRAN will automatically install extrafontdb and Rttf2pt1:
install.packages('extrafont')library(extrafont)
Check this page out for more info on extrafontdb
and Rttf2pt1.
4. Import the new fonts into R
Use the font_import
function (specify your path) to import the new fonts:
#need to do this everytime a new true font type file is installed on pcfont_import(paths = "C:/Users/urUserName/AppData/Local/Microsoft/Windows/Fonts")Then, R console will prompt: Importing fonts may take a few minutes, depending on the number of fonts and the speed of the system.
Continue? [y/n] - type y and enter
If you use font_import()
only, R will install all the already available fonts such as Arial, Calibri, etc. And when you use the same function for the second time, you might see in the console that R has found FontName for 0 fonts
and the newly installed fonts are still nowhere to be found in R.
You might also see this error message if you use font_import()
only:
Error in data.frame(fontfile = ttfiles, FontName = "", stringsAsFactors = FALSE) :
arguments imply differing number of rows: 0, 1
Hence, it is important to direct R into the right path, by stating paths
in the font_import()
function.
If you are not able to find AppData folder on your pc, check this page out for a short tutorial on how to make the folder visible.
After importing the fonts, you can use extrafont::loadfonts()
to check if the new fonts are already imported into R.
5. Load the fonts into plots
Then, use the following codes to load the fonts into the plot output:
# Register fonts for Windows bitmap output
loadfonts(device=”win”)
Now that’s a pretty chart!
