//
you're reading...
Data Visualisation, R, Topical, Uncategorized

Clegg vs Pleb: An XKCD-esque chart

I saw an interesting “challenge” on StackOverflow last night to create an XKCD style chart in R. A couple of hours later & going in a very similar direction to a couple of the answers on SO, I got to something that looked pretty good, using the sin and cos curves for simple and reproducible replication.

Tonight, I thought I’d try and apply the theme and styling to some real word and slightly XKCD content: UK politics. Two of the biggest stories of the month in the UK have been Nick Clegg’s apology over reneging on the Liberal Democrat’s tuition fees pledge and Andrew Mitchell’s “incident” trying to cycle out of Downing Street, the so-called GateGate.

Using the newly redesigned Google Insights for Search, I looked at searches for clegg and pleb over the last 30 days. A quick manipulation into a csv and applying the XKCD theme and some geom_smoothing gives this:

Looks like Andrew Mitchell might be Nick Clegg’s new best friend in terms of deflecting some of the attention away from the sorry song…

And here’s the code (note that you need to have installed the Humor Sans font using install_fonts() ):

library(ggplot2)
library(extrafont)

### Already have read in fonts (see previous answer on how to do this)
loadfonts()

### Clegg and Pleb data
pleb.clegg <- read.csv("pleb and clegg.csv")
pleb.clegg$Date <- as.Date(pleb.clegg$Date, format="%d/%m/%Y")
pleb.clegg$xaxis <- -4

### 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=pleb.clegg, aes(x=Date, y=Pleb))+
geom_smooth(aes(y=Clegg), colour="gold", size=1, position="jitter", fill=NA)+
geom_smooth(colour="white", size=3, position="jitter", fill=NA)+
geom_smooth(colour="dark blue", size=1, position="jitter", fill=NA)+
geom_text(data=pleb.clegg[10, ], family="Humor Sans", aes(x=Date), colour="gold", y=20, label="Searches for clegg")+
geom_text(data=pleb.clegg[22, ], family="Humor Sans", aes(x=Date), colour="dark blue", y=4, label="Searches for pleb")+
geom_line(aes(y=xaxis), position = position_jitter(h = 0.1), colour="black")+
coord_cartesian(ylim=c(-5, 40))+
labs(x="", y="", title="Pleb vs Clegg: Google Keyword Volumes")+
theme_xkcd

ggsave("xkcd_cleggpleb.jpg", plot=p, width=8, height=5)
About these ads

Discussion

15 thoughts on “Clegg vs Pleb: An XKCD-esque chart

  1. Where’s that install_fonts() function?

    Posted by Greg Tucker-Kellogg | October 3, 2012, 12:05 pm
    • Sorry, my bad, the function that you’re after is called load_fonts() and it’s within the extrafonts package that you need. There are pretty good instructions from another of the answers on stack overflow.

      I found getting the font to work was the biggest issue i encountered in the process – if you’re on a windows machine, it’s a lot easier (I’ll dig out a good link which will help you out).

      Posted by markbulling | October 3, 2012, 1:13 pm
      • Be a man :-) …. write a function to generate each letter as a spline thru a bunch of points and call the function to build and plot the text.

        Posted by cellocgw | October 3, 2012, 6:26 pm
  2. Is that the link you meant to give for the xkcd SO question?. I think you meant http://stackoverflow.com/questions/12675147/xkcd-style-graphs-in-r

    Posted by g | October 3, 2012, 9:13 pm
  3. Hi, I am trying to use this, but it’s complaining that the `theme` function doesn’t exist. I replaced it with `ggplot2:theme_update`, but now it complains about `element_rect` and I can’t find what would be a replacement for this one. Are these coming from another package? or a different version of ggplot2? Thanks

    Posted by drmortimer | October 19, 2012, 1:04 pm
  4. Also, check out http://gicentre.org/handy/#about – amazing Processing library for a more generalized hand-finished charts library

    Posted by markbulling | October 19, 2012, 8:45 pm
  5. Is there a way to add a y-axis in the same way with jitter?

    Posted by BruceWayne | October 31, 2012, 12:56 am

Trackbacks/Pingbacks

  1. Pingback: datanalytics » Gráficos en R a la xkcd - October 10, 2012

  2. Pingback: xkcd-style charts in R, JavaScript, and Python « Nate Crowder - October 19, 2012

  3. Pingback: xkcd-style charts in R, JavaScript, and Python | Dank Logic News - October 19, 2012

  4. Pingback: michaelgalloy.com — Sketchy plots - October 26, 2012

  5. Pingback: Somewhere else, part 18 | Freakonometrics - January 30, 2013

  6. Pingback: All Things of World - February 2, 2013

  7. Pingback: R code for xkcd-like graphs! | chia kaivalya - October 16, 2013

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

Blog Stats

  • 287,947 hits

Enter your email address to follow this blog and receive notifications of new posts by email.

Join 480 other followers

Follow

Get every new post delivered to your Inbox.

Join 480 other followers

%d bloggers like this: