R365: Day 18 – XKCD!

I am a HUGE fan of the internet comic XKCD (xkcd.com), which is “a webcomic about romance, sarcasm, math, and language”. Reading that description, I can’t help but think of how perfectly that matches the ambiance of the comic strip. I have never felt such emotional attachment and investment as I have for those silly little stick figures. Of course, I am not alone, and lots and lots of people LOVE XKCD, and it only figures that at least one person decided to make the distinctive XKCD font available for the unwashed masses and make a theme available for R. You can download the XKCD font (“Humor Sans” .ttf) from several places (Dropbox, Github, Sourceforge, etc). Using the {extrafont} package described in the last post, you can load in the package. Its official name in R is “Humor Sans”, and I did not need an underscore inbetween the words to get it to work. A really awesome example of the magic that Randall Monroe makes happen is this strip (http://xkcd.com/1319/) that both highlights the insightful graphs and my own problem when deciding whether to automate code or not. In honor of the wonderful comic strip, lets see if I can make some absolutely absurd correlations happen (in the next 10 minutes).
…What has happened in the last 45 minutes:
Step 1: try to come up with clever/funny graphs.
Step 2: realize I am neither clever nor funny.
Step 3: cry.

But lets look at {swiss} data on the relationship between education, fertility, and child mortality.
Rplot01 Rplot02 Rplot03
> ggplot(swiss, aes(x=Fertility, y=Education)) + geom_point() +
+     ggtitle(“Fertility by Education”) +
+     xlab(“Fertility”) + ylab(“Education”) +
+     theme(text=element_text(size=16, family=”Humor Sans”))

One user on a Stackoverflow site suggested using jitter() to replicate the hand drawn look for most of the graphs. I got it to work but it was not working out for the dataset I was using.

The {xkcd} package also lets you draw stick figures on all your graphs, but it seems to require quite a bit of tweaking to make it look good. This is their example:

Rplot05

Or after a little playing around, you can make them look pretty stupid.

Rplot06

Have Fun!

datascaled <- data.frame(x=c(-3,3),y=c(-30,30))
> p <- ggplot(data=datascaled, aes(x=x,y=y)) + geom_point()
> xrange <- range(datascaled$x)
> yrange <- range(datascaled$y)
> ratioxy <- diff(xrange) / diff(yrange)
>
> mapping <- aes(x=x,
+                y=y,
+                scale=scale,
+                ratioxy=ratioxy,
+                angleofspine = angleofspine,
+                anglerighthumerus = anglerighthumerus,
+                anglelefthumerus = anglelefthumerus,
+                anglerightradius = anglerightradius,
+                angleleftradius = angleleftradius,
+                anglerightleg =  anglerightleg,
+                angleleftleg = angleleftleg,
+                angleofneck = angleofneck,
+                color = color )
>
> dataman <- data.frame( x= c(-1,0,1), y=c(-10,0,10),
+                        scale = c(10,7,5),
+                        ratioxy = ratioxy,
+                        angleofspine =  seq(- pi / 2, -pi/2 + pi/8, l=3) ,
+                        anglerighthumerus = -pi/6,
+                        anglelefthumerus = pi + pi/6,
+                        anglerightradius = 0,
+                        angleleftradius = rnorm(3,- pi/4, pi/4),
+                        angleleftleg = 6*pi/2  + pi / 12 ,
+                        anglerightleg = 8*pi/2  – pi / 12,
+                        angleofneck = runif(3, min = 3 * pi / 2 – pi/10 , max = 3 * pi / 2 + pi/10),
+                        color=c(“A”,”B”,”C”))
>
> p + xkcdman(mapping,dataman)

A big shoutout to Erin H-B for pointing out this awesome package! Thanks Erin!

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s