xkcd styled graphs using the xkcd package in R. Steps done on R version 3.0.1 (2013-05-16) and on Windows, i386-w64-mingw32/i386 (32-bit). Steps followed are from the xkcd-intro.pdf file i.e. the xkcd vignette.

install.packages(“xkcd”)

library(xkcd)

library(extrafont)

library(ggplot2)

#do you have xkcd fonts?

if( “xkcd” %in% fonts()) {

p <- ggplot() + geom_point(aes(x=mpg, y=wt), data=mtcars) +
theme(text = element_text(size = 16, family = "xkcd"))
} else {
warning("Not xkcd fonts installed!")
p <- ggplot() + geom_point(aes(x=mpg, y=wt), data=mtcars)
}
#Warning message:
#Not xkcd fonts installed!
#download the font and install it
#http://simonsoftware.se/other/xkcd.ttf
#then import all fonts
font_import()
#load all fonts
loadfonts()
#Despite loading all fonts
#I had to restart R to get the fonts loaded properly
#opens the xkcd-intro.pdf
vignette("xkcd-intro")
xrange <- range(mtcars$mpg)
yrange <- range(mtcars$wt)
set.seed(123) # for reproducibility
p <- ggplot() + geom_point(aes(mpg, wt), data=mtcars) + xkcdaxis(xrange,yrange)
p
[/sourcecode]

### Cartoon characters

To include cartoon characters in the graph, use the xkcdman() function.

ratioxy <- diff(xrange)/diff(yrange) mapping <- aes(x, y, scale, ratioxy, angleofspine ,anglerighthumerus, anglelefthumerus, anglerightradius, angleleftradius, anglerightleg, angleleftleg, angleofneck, linetype=city) dataman <- data.frame(x= c(15,30), y=c(3, 4), scale = c(0.3,0.51), ratioxy = ratioxy, angleofspine = -pi/2, anglerighthumerus = c(pi/4, -pi/6), anglelefthumerus = c(pi/2 + pi/4, pi +pi/6), anglerightradius = c(pi/3, -pi/3), angleleftradius = c(pi/3, -pi/3), anglerightleg = 3*pi/2 - pi / 12, angleleftleg = 3*pi/2 + pi / 12, angleofneck = runif(1, 3*pi/2-pi/10, 3*pi/2+pi/10), city=c("Liliput","Brobdingnag")) q <- ggplot() + geom_point(aes(mpg, wt, colour=as.character(vs)), data=mtcars) + xkcdaxis(xrange,yrange) + xkcdman(mapping, dataman) [/sourcecode]

### Scatterplot

volunteers <- data.frame(year=c(2007:2011), number=c(56470, 56998, 59686, 61783, 64251)) xrange <- range(volunteers$year) yrange <- range(volunteers$number) ratioxy <- diff(xrange) / diff(yrange) datalines <- data.frame(xbegin=c(2008.3,2010.5),ybegin=c(63000,59600), xend=c(2008.5,2010.3), yend=c(63400,59000)) p <- ggplot() + geom_smooth(mapping=aes(x=year, y =number), data =volunteers, method="loess") + xkcdaxis(xrange,yrange) + ylab("Volunteers at Caritas Spain") p [/sourcecode]

### Bar charts

data <- data.frame(year=c(2007:2011), number=c(56470, 56998, 59686, 61783, 64251)) data$xmin <- data$year - 0.1 data$xmax <- data$year + 0.1 data$ymin <- 50000 data$ymax <- data$number xrange <- range(min(data$xmin)-0.1, max(data$xmax) + 0.1) yrange <- range(min(data$ymin)+500, max(data$ymax) + 1000) mapping <- aes(xmin=xmin,ymin=ymin,xmax=xmax,ymax=ymax) p <- ggplot() + xkcdrect(mapping,data) + xkcdaxis(xrange,yrange) + xlab("Year") + ylab("Volunteers at Caritas Spain") p [/sourcecode]

### Barchart of piRNA lengths from NONCODE

Previously I had used the SeqinR package in R to get the length distribution of all human piRNAs in the NONCODE database. Here a produce a xkcd styled bar chart of the piRNA lengths.

data <- data.frame(size = c(26,27,28,29,30,31,32), freq = c(3552, 3540, 4127, 7166, 8216, 4410, 1141)) data size freq 1 26 3552 2 27 3540 3 28 4127 4 29 7166 5 30 8216 6 31 4410 7 32 1141 data$xmin <- data$size - 0.1 data$xmax <- data$size + 0.1 data$ymin <- 0 data$ymax <- data$freq xrange <- range(min(data$xmin)-0.1, max(data$xmax) + 0.1) yrange <- range(min(data$ymin)+500, max(data$ymax) + 1000) mapping <- aes(xmin=xmin,ymin=ymin,xmax=xmax,ymax=ymax) p <- ggplot() + xkcdrect(mapping,data) + xkcdaxis(xrange,yrange) + xlab("piRNA size") + ylab("Frequency") p [/sourcecode]

### See more

This work is licensed under a Creative Commons

Attribution 4.0 International License.

Not working with dataman:

Warnung: Ignoring unknown aesthetics: xbegin, ybegin, xend, yend

Warnung: Ignoring unknown aesthetics: xbegin, ybegin, xend, yend

null device

1

Fehler: Unerwartete(s) ‘=’ in:

“+ anglerightleg, angleleftleg, angleofneck,

+ linetype =”