Site icon R-bloggers

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 s in your plots comes now easy with the extras-package.

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

First, install the package and load it.

install.packages("extra")
library(extra)

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

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

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

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

s()
table()

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

loads(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 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.

_import(pattern="[H/h]umor")
library(quantmod)
getSymbols("GDPC1", src="FRED")
data <- data.frame(time=index(GDPC1), GDP=GDPC1)

Now data is donwloaded, the 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 . 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 s in the PDF, and therefore you have to install Ghostscript. Tell R where Ghostscript sits on your machine, and the just embed the s:

ggsave("_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_s("_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>extra</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 their blog: fibosworld » R.

R-bloggers.com 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.