Change fonts in ggplot2, and create xkcd style graphs

[This article was first published on fibosworld » R, and kindly contributed to R-bloggers]. (You can report issue about the content on this page here)
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.

Installing and changing fonts in your plots comes now easy with the extrafonts-package.

There is a excellent tutorial on the extrafonts github site, still I will shortly demonstrate how it worked for me.

First, install the package and load it.


You can now install the desired system fonts (at the moment only TrueType fonts):


The pattern argument just specifies the fonts to be installed; if you leave it out, the function will search automatically and install all fonts (see the help function for font_import in R.

You can now look at the fonts loaded to be used with R:


Alright, if you want to use the fonts to display graphs on-screen (what you normally want to do, e.g. in the plots-window in RStudio), you have to load the fonts to the Windows device:


Now we can just plot a graph. I will use the ggplot2 package to do so (and this is taken nearly verbatim from the github-site mentioned earlier) :

ggplot(mtcars, aes(x=wt, y=mpg)) + geom_point() +
  ggtitle("Fuel Efficiency of 32 Cars") +
  xlab("Weight (x1000 lb)") + ylab("Miles per Gallon") +
  theme(text=element_text(size=16, family="Comic Sans MS"))

Let’s now draw a chart using the xkcd-style. This has been debated on stackoverflow, and the guys there have come up with great examples! I basically just take their approach and modify it slightly to draw a graph on US GDP (gross domestic product) development. I download the data from the St. Louis Fed website, using the excellent quantmod package.
But first I need to get the font in xkcd style, and I’ve chosen to download it from here.
You now just have to install it, by right-clicking on the .ttf file and chose install.

getSymbols("GDPC1", src="FRED")
data <- data.frame(time=index(GDPC1), GDP=GDPC1)

Now data is donwloaded, the font is installed.
Next step: Create a ggplot2-theme that has xkcd “properties” – and this is what I’ve basically copied from stackoverflow. Note: Unlike on stackoverflow, I basically use only the font. I do not jitter the line, since it is real data that is “jittered” anyway.
The rest is just ggplot2 plotting:

### XKCD theme
theme_xkcd <- theme(
  panel.background = element_rect(fill="white"),
  #axis.ticks = element_line(colour=NA),
  panel.grid = element_line(colour="white"),
  #axis.text.y = element_text(colour=NA),
  axis.text.x = element_text(colour="black"),
  text = element_text(size=16, family="Humor Sans")

### Plot the chart
p <- ggplot(data=data, aes(x=time, y=GDPC1))+
  geom_line(colour="red", size=1)+
  ggtitle("development of US gross domestic product") +

If you want to save this chart now as pdf, you need to do another step: use ggsave. But you need to embed the fonts in the PDF, and therefore you have to install Ghostscript. Tell R where Ghostscript sits on your machine, and the just embed the fonts:

ggsave("font_ggplot.pdf", plot=p,  width=12, height=4)
## needed for Windows - make sure YOU have the correct path for your machine:
Sys.setenv(R_GSCMD = "C:\\Program Files (x86)\\gs\\gs9.06\\bin\\gswin32c.exe")

There might be some warnings issued, but usually it works.

And finally: I do not take any credit for this work, it’s basically the developers of the extrafont package and Mark Bulling on stackoverflow who made all this possible. Any remaining errors are mine.

To leave a comment for the author, please follow the link and comment on their blog: fibosworld » R. offers daily e-mail updates about R news and tutorials about learning R and many other topics. Click here if you're looking to post or find an R/data-science job.
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.

Never miss an update!
Subscribe to R-bloggers to receive
e-mails with the latest R posts.
(You will not see this message again.)

Click here to close (This popup will not appear again)