Skip to content

Instantly share code, notes, and snippets.

@epijim
Last active January 2, 2016 14:39
Show Gist options
  • Save epijim/8318333 to your computer and use it in GitHub Desktop.
Save epijim/8318333 to your computer and use it in GitHub Desktop.
# SETUP #########################################################################
setwd("/Users/Jimmy/Dropbox/Personal/fun with code/xkcd")
library(xkcd)
library(extrafont)
library(ggplot2)
#Load scatter dataset
collegesdata <- read.csv("~/Dropbox/Personal/fun with code/xkcd/collegesdata.csv"
)
sapply(collegesdata, mode)
head(collegesdata)
#Load bar dataset
bardata <- read.csv("~/Dropbox/Personal/fun with code/xkcd/bardata.csv"
)
sapply(bardata, mode)
head(bardata)
#Load line graph
linedata <- read.csv("~/Dropbox/Personal/fun with code/xkcd/p.csv"
)
sapply(linedata, mode)
head(linedata)
# Check font status #################################################################################
#do you have xkcd fonts?
if( "xkcd" %in% fonts()) {
plo <- ggplot() + geom_point(aes(x=Year.founded, y=Fixed.assets), data=collegesdata) +
theme(text = element_text(size = 16, family = "xkcd"))
} else {
warning("xkcd fonts not installed! Read the text below")
}
#If warning,
#download the font and install it
#http://simonsoftware.se/other/xkcd.ttf
#On a mac, just click the link. Once downloaded click on the file
#then click install on the prompt. You're done.
# Load up fonts ##############################################################################
#then import all fonts - run line below on first run - it's SLOW
#font_import()
#load all fonts - run line below on first run
#loadfonts()
#opens the xkcd-intro.pdf
vignette("xkcd-intro")
## SCATTER #####################################################################################
xrange <- range(collegesdata$Year.founded)
yrange <- range(collegesdata$Fixed.assets)
set.seed(1) # for reproducibility, as hand effect is just jitter
# R wouldn't let me use the xkcd font in a device - I didn't try and find away round
plot <- ggplot() + geom_point(aes(Year.founded, Fixed.assets), data=collegesdata) +
xkcdaxis(xrange,yrange) +
ylab("Fixed assets (£)") + xlab("Year founded") + labs(title="Cambridge college wealth \nagainst year founded")
plot
## BAR #####################################################################################
# note - I'm being lazy and living with the errors on the xaxis. I should edit tick labels out of the theme.
data <- bardata
#width of bars
data$xmin <- data$comroom - 0.1
data$xmax <- data$comroom + 0.1
data$ymin <- 0
data$ymax <- data$number
#Give some space to fit in text
xrange <- range(min(data$xmin)-0.3, max(data$xmax) + 0.5)
yrange <- range(min(data$ymin), max(data$ymax+50))
mapping <- aes(xmin=xmin,ymin=ymin,xmax=xmax,ymax=ymax)
plot2 <- ggplot() + xkcdrect(mapping,data) +
xkcdaxis(xrange,yrange) +
xlab("Jesus College Combination room") + ylab("Number of people") +
annotate("text", x=1, y = 100, label = "Fellows", family="xkcd" ) +
annotate("text", x=2, y = 220, label = "PhD'ers", family="xkcd" ) +
annotate("text", x=3, y = 150, label = "Other grads", family="xkcd" ) +
annotate("text", x=4, y = 490, label = "Undergrads", family="xkcd" )
plot2
## Line with facets #####################################################################################
data <- linedata
yrange <- range(data$prev)
xrange <- range(data$year)
ratioxy <- 4
mapping <- aes(x, y,
scale,
ratioxy,
angleofspine ,
anglerighthumerus,
anglelefthumerus,
anglerightradius,
angleleftradius,
anglerightleg,
angleleftleg,
angleofneck)
dataman <- data.frame( x= c(2003,2020), y=c(11, 4.5),
scale = 0.6,
ratioxy = ratioxy,
angleofspine = -pi/2 ,
anglerighthumerus = c(-pi/6, -pi/6),
anglelefthumerus = c(-pi/2 - pi/6, -pi/2 - pi/6),
anglerightradius = c(pi/5, -pi/5),
angleleftradius = c(pi/5, -pi/5),
angleleftleg = 3*pi/2 + pi / 12 ,
anglerightleg = 3*pi/2 - pi / 12,
angleofneck = runif(1, 3*pi/2-pi/10, 3*pi/2+pi/10))
datalines <- data.frame(xbegin=c(2005,2021.6),ybegin=c(11,4.5), xend=c(2006,2022.1), yend=c(11.2,4.7))
p <- ggplot() + geom_smooth(mapping=aes(x=year, y =prev, colour=as.character(country)), data =data, method="loess")
p + xkcdaxis(xrange,yrange) +
ylab("Predicted raw prevelance of diabetes (%)") +
xkcdman(mapping, dataman) +
annotate("text", x=2010.5, y = 11.7, label = "Diabetes is\nincreasing!", family="xkcd" ) +
annotate("text", x=2024.5, y = 5.6, label = "Left of the dotted line \nare collected data\nright side are IDF predictions", family="xkcd" ) +
xkcdline(aes(xbegin=xbegin,ybegin=ybegin,xend=xend,yend=yend),datalines, xjitteramount = 0.12) +
geom_vline(xintercept=c(2013), linetype="dotted")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment