#the following block is used to install the fonts:
#install.packages('xkcd')
# download.file("http://simonsoftware.se/other/xkcd.ttf", dest="xkcd.ttf", mode="wb")
# system("mkdir ~/Library/Fonts/")
# system("cp xkcd.ttf ~/Library/Fonts/")
# font_import(pattern = "[X/x]kcd", prompt=FALSE)
# fonts()
# fonttable()
# if(.Platform$OS.type != "unix") {
# ## Register fonts for Windows bitmap output
# loadfonts(device="win")
# } else {
# loadfonts()
# }
The trickiest part is getting the xkcd font downloaded and put in the right directory. Once you have that figured out the plot is very straightforward:
#dataframe:
df <- data.frame(c(2,3,4,16,32,64,128,356,0,0,0,0,0),c(10,9,8,7,6,5,4,1.25,1.21,1.20,1.19,1.1,1))
colnames(df) <- c('proj','days')
library(xkcd)
library(ggplot2)
library(extrafont)
# here's just the plot:
xrange <- range(df$days)
yrange <- range(df$proj)
set.seed(123) # for reproducibility
p <- ggplot() + geom_smooth(aes(days, proj), data=df, method = 'loess',se=F) + xkcdaxis(xrange,yrange) +
xlab('Days til deadline') +
ylab('Time spent on this silly plots') +
geom_vline(xintercept = 2,colour = "red") +
annotate("text", 3.0, 250, label = "Crisis Point",colour = "red",family='xkcd') +
scale_x_reverse()
p
You can also include stickfigures! They are a bit tought to draw though. I recommend experimenting with the angles until you get it right. The vignettes do a good job of explaining how the different parameters map.
#this block makes the stick figure:
#play around with the angles to see how they change the drawing:
mapping <- aes(x, y, scale, ratioxy, angleofspine,
anglerighthumerus, anglelefthumerus,
anglerightradius, angleleftradius,
anglerightleg, angleleftleg, angleofneck)
ratioxy <- diff(xrange) / diff(yrange)
dataman <- data.frame( x= c(8), y=c(250),
scale = 75 ,
ratioxy = ratioxy,
angleofspine = -pi/2 ,
anglerighthumerus = c(-pi/6),
anglelefthumerus = c(-pi/2 - pi/6),
anglerightradius = c(pi*1.25),
angleleftradius = c(pi*1.25),
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(7),ybegin=c(250),
xend=c(6.5), yend=c(300))
# plot out the first plot with the stick figure:
p <- ggplot() + geom_smooth(aes(days, proj), data=df, method = 'loess',se=F) + xkcdaxis(xrange,yrange) +
xlab('Days til deadline') +
ylab('Time spent on silly plots') +
geom_vline(xintercept = 2,colour = "red") +
annotate("text", 3.0, 250, label = "Crisis Point",colour = "red",family='xkcd') +
scale_x_reverse() +
xkcdman(mapping, dataman) +
annotate("text", x=6, y = 320,
label = "I think I have a problem...", family="xkcd" ) +
xkcdline(aes(xbegin=xbegin,ybegin=ybegin,xend=xend,yend=yend),datalines, xjitteramount = 0.12)
p
Fun. Can’t wait to see one in someone’s dissertation seminar!
session
sessionInfo()
## R version 3.3.0 (2016-05-03)
## Platform: x86_64-apple-darwin13.4.0 (64-bit)
## Running under: OS X 10.10 (Yosemite)
##
## locale:
## [1] en_US.UTF-8/en_US.UTF-8/en_US.UTF-8/C/en_US.UTF-8/en_US.UTF-8
##
## attached base packages:
## [1] stats graphics grDevices utils datasets methods base
##
## other attached packages:
## [1] xkcd_0.0.5 extrafont_0.17 ggplot2_2.2.1
##
## loaded via a namespace (and not attached):
## [1] Rcpp_0.12.14 bindr_0.1 RColorBrewer_1.1-2
## [4] plyr_1.8.4 base64enc_0.1-3 tools_3.3.0
## [7] rpart_4.1-11 digest_0.6.13 checkmate_1.8.5
## [10] htmlTable_1.11.0 evaluate_0.10.1 tibble_1.3.4
## [13] gtable_0.2.0 lattice_0.20-35 pkgconfig_2.0.1
## [16] rlang_0.1.4 Matrix_1.2-12 rstudioapi_0.7
## [19] yaml_2.1.16 bindrcpp_0.2 gridExtra_2.3
## [22] Rttf2pt1_1.3.5 dplyr_0.7.4 stringr_1.2.0
## [25] knitr_1.17 cluster_2.0.6 htmlwidgets_0.9
## [28] rprojroot_1.3-1 grid_3.3.0 nnet_7.3-12
## [31] data.table_1.10.4-3 glue_1.2.0 R6_2.2.2
## [34] survival_2.41-3 foreign_0.8-69 rmarkdown_1.8
## [37] latticeExtra_0.6-28 Formula_1.2-2 purrr_0.2.4
## [40] tidyr_0.7.2 extrafontdb_1.0 magrittr_1.5
## [43] backports_1.1.2 scales_0.5.0 Hmisc_4.0-3
## [46] htmltools_0.3.6 splines_3.3.0 assertthat_0.2.0
## [49] colorspace_1.3-2 labeling_0.3 stringi_1.1.6
## [52] acepack_1.4.1 lazyeval_0.2.1 munsell_0.4.3