Change fonts in ggplot2, and create xkcd style graphs

February 17, 2013
By

(This article was first published on fibosworld » R, and kindly contributed to R-bloggers)

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.

install.packages("extrafont")
library(extrafont)

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

font_import(pattern="[C/c]omic")
font_import(pattern="[A/a]rial")

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:

fonts()
fonttable()

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:

loadfonts(device="win")

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) :

library(ggplot2)
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.

font_import(pattern="[H/h]umor")
library(quantmod)
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") +
  theme_xkcd
p

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")
embed_fonts("font_ggplot.pdf")

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 <code>extrafont</code> 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 his blog: fibosworld » R.

R-bloggers.com offers daily e-mail updates about R news and tutorials on topics such as: visualization (ggplot2, Boxplots, maps, animation), programming (RStudio, Sweave, LaTeX, SQL, Eclipse, git, hadoop, Web Scraping) statistics (regression, PCA, time series, trading) and more...



If you got this far, why not subscribe for updates from the site? Choose your flavor: e-mail, twitter, RSS, or facebook...

Comments are closed.