Clegg vs Pleb: An XKCD-esque chart

October 2, 2012

(This article was first published on Drunks&Lampposts » R, and kindly contributed to R-bloggers)

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() ):


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

### 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")+

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

To leave a comment for the author, please follow the link and comment on his blog: Drunks&Lampposts » R. offers daily e-mail updates about R news and tutorials on topics such as: visualization (ggplot2, Boxplots, maps, animation), programming (RStudio, Sweave, LaTeX, SQL, Eclipse, git, hadoop, Web Scraping) statistics (regression, PCA, time series, trading) and more...

If you got this far, why not subscribe for updates from the site? Choose your flavor: e-mail, twitter, RSS, or facebook...

Comments are closed.