Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can we make xkcd style graphs?

Tags:

plot

r

ggplot2

Apparently, folk have figured out how to make xkcd style graphs in Mathematica and in LaTeX. Can we do it in R? Ggplot2-ers? A geom_xkcd and/or theme_xkcd?

I guess in base graphics, par(xkcd=TRUE)? How do I do it?

xkcd#1064

As a first stab (and as much more elegantly shown below) in ggplot2, adding the jitter argument to a line makes for a great hand-drawn look. So -

ggplot(mapping=aes(x=seq(1,10,.1), y=seq(1,10,.1))) +    geom_line(position="jitter", color="red", size=2) + theme_bw() 

It makes for a nice example - but the axes and fonts appear trickier. Fonts appear solved (below), though. Is the only way to deal with axes to blank them out and draw them in by hand? Is there a more elegant solution? In particular, in ggplot2, can element_line in the new theme system be modified to take a jitter-like argument?

like image 521
jebyrnes Avatar asked Oct 01 '12 14:10

jebyrnes


2 Answers

You might want to consider the following package:

Package xkcd: Plotting ggplot2 graphics in a XKCD style.

library(xkcd) vignette("xkcd-intro") 

Some examples (Scatterplots, Bar Charts):

  • Scatterplot:

Scatterplot

  • Bar Chart:

Bar Chart

like image 158
Emilio Torres Manzanera Avatar answered Sep 19 '22 23:09

Emilio Torres Manzanera


Thinking along the same line as some of the other answers, I've "un-ggplotted" the chart and also added on the flexibility of the x-axis label locations (which seems to be common in xkcd) and an arbitrary label on the chart.

Note that I had a few issues with loading the Humor Sans font and manually downloaded it to working directory.

enter image description here

And the code...

library(ggplot2) library(extrafont)  ### Already have read in fonts (see previous answer on how to do this) loadfonts()  ### Set up the trial dataset  data <- NULL data$x <- seq(1, 10, 0.1) data$y1 <- sin(data$x) data$y2 <- cos(data$x) data$xaxis <- -1.5  data <- as.data.frame(data)  ### 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=x, y=y1))+       geom_line(aes(y=y2), position="jitter")+       geom_line(colour="white", size=3, position="jitter")+       geom_line(colour="red", size=1, position="jitter")+       geom_text(family="Humor Sans", x=6, y=-1.2, label="A SIN AND COS CURVE")+       geom_line(aes(y=xaxis), position = position_jitter(h = 0.005), colour="black")+       scale_x_continuous(breaks=c(2, 5, 6, 9),        labels = c("YARD", "STEPS", "DOOR", "INSIDE"))+labs(x="", y="")+       theme_xkcd  ggsave("xkcd_ggplot.jpg", plot=p, width=8, height=5) 
like image 21
Mark Bulling Avatar answered Sep 20 '22 23:09

Mark Bulling