How to make xkcd-style plots in R

The webcomic xkcd hits a lot of sweet notes for me, from comedic science communication with Up Goer Five to the importance of labeling your axes.  Fortunately, we live in an age where it is now possible to make xkcd-style plots in R:

fuelefficiencyxkcdWith help from andrew and fibosworld’s posts on R-bloggers, here’s how I did it in Windows:

First, I installed the xkcd font from this site.

Second, in R, I installed and loaded the packages ggplot2 and xkcd.

# Ensuring packages ggplot2 and xkcd installed and loaded

Third, I checked that the xkcd font was indeed listed in my system fonts, and imported it into R.  Because I’m using Windows, I also had to load my fonts to the Windows device, so that I could display it graphically.

# Checking xkcd.tff listed among system fonts

# Importing the xkcd font into R and confirming it should be imported
font_import(pattern = "[X/x]kcd")

# Loading fonts to the Windows device to display graphs on-screen
loadfonts(device = "win")

Finally, I was able to use xkcd-style font and axes to make the plot above!

# Using R's built-in dataset, mtcars, to plot car weight vs. mpg
carPlot geom_point() +
ggtitle("Fuel Efficiency of Cars") +
xlab("Car Weight in Thousands of Pounds") +
ylab("Miles per Gallon") +
xkcdaxis(xrange = range(mtcars$wt), yrange = range(mtcars$mpg)) +
theme(text = element_text(size = 20, family = "xkcd"))

# Plotting, xkcd-style!

Elsewhere: Misdiagnosing mummies, finding ancient stone tools in London, using built-in R functions, and darn good science writing

Who needs an osteologist? (Installment 20): Archaeologist Kristina Killgrove dissects how doctors misdiagnosed the cause of death for an Ancient Egyptian mummy.

Liquid prehistory: The Urban Prehistorian discovers old Pepsi bottles (unsurprising) and much older knapped flint tools (very surprising!) along the banks of the River Thames in London.

Using apply, sapply, lapply in R: Pete from R-bloggers reviews several built-in R functions which improve the run speed of your R code by avoiding looping.  As someone with a background in Java/C++ who loves me some loops, this is Highly Recommended.

Bound for Pluto, Carrying Memories of Triton: Dennis Overbye at the New York Times covers recent findings about the shared parentage of Pluto and Triton, one of Neptune’s moons.  Watch the video for a fantastic example of science communication done right – Overbye uses the most marvelous analogies (“cantaloupe surface” and “egg-shaped orbit”) to effectively describe Pluto and Triton.