Book Image

R Data Visualization Cookbook

Book Image

R Data Visualization Cookbook

Overview of this book

Table of Contents (17 chapters)
R Data Visualization Cookbook
Credits
About the Author
About the Reviewers
www.PacktPub.com
Preface
Index

Generating plots with custom fonts


The main aim of this recipe is to introduce you to installing fonts and how to use them to label plots. We have used xkcd fonts for this recipe to introduce humor in our plots. The fonts look very similar to the XKCD cartoon strip.

Getting ready

To download and generate plots using the xkcd package, we require the sysfonts package.

How to do it…

The install.packages() and library() functions allow us to download the packages as well as load the library in R:

install.packages("sysfonts")
library(sysfonts)

Next, we just copy and paste the following lines of code to the R command window. These lines of code are used to download the xkcd fonts to our R working directory. Once you execute these lines of code, you will see a file in your working directory called xkcd.ttf. The code is as follows:

download.file("http://simonsoftware.se/other/xkcd.ttf", dest="xkcd.ttf", mode="wb")
font.paths()
system("mkdir ~/.fonts")
system("cp xkcd.ttf -t ~/.fonts")
font.files()
font...