Xkcd style graphs with r

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
library(extrafont)loadfonts()library(xkcd)# Bring in the data! #   workout=c(1:5) - creates a range from 1-5, so 1,2,3,4,5#   c(16...) - These are my percentiles for the five workouts, resepectivelyscores <- data.frame(workout=c(1:5), rank=c(16.71,4.21,19.61,4.9,19.38))# Define how much of the X and Y access to show.# In our case, we'll show all of the Y access,# but only 1-5 on the X access side.xrange <- range(scores$workout)yrange <- range(c(0,100))ratioxy <- diff(xrange)/ diff(yrange)# Let's create XKCD style stick figure# I blatantly copied this part from the sample codemapping <- aes(x,  y,                scale,                ratioxy,                angleofspine ,                anglerighthumerus,                anglelefthumerus,                anglerightradius,                angleleftradius,                anglerightleg,                angleleftleg,                angleofneck)# The c(1.5,4.5) reprents the X coordinates of each of the 2 stick figures --# likewise for the y coordinate. The rest of these control the arms and legsdataman <- data.frame( x= c(1.5,4.5), y=c(80,70),                       scale =17,                       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))# Those squigly lines that connect text to a character are easy to draw.# Each needs an x/y start point and an x/y end point. The library does the rest.datalines <- data.frame(xbegin=c(1.9,4.2,2),                        ybegin=c(80,70,77),                        xend=c(2.1,3.9,2.8),                        yend=c(88,80,68))# Using ggplot to do the actual graphing -- an versatile graphing library for Rp <- ggplot()+ geom_smooth(mapping=aes(x=workout, y =rank),                            data=scores,                            method="loess")# Do ALL the generating!# This includes putting everything together and adding the sample text we want to write.# Of course, this text should be written using the xkcd font.p + xkcdaxis(xrange,yrange)+   ylab("Percentile")+   xkcdman(mapping, dataman)+   annotate("text", x=2.4, y =93, label ="There's a lot of\nroom up here", family="xkcd")+   annotate("text", x=4.1, y =83, label ="Let's do 7 minutes of Burpees!", family="xkcd")+   annotate("text", x=2.8, y =62, label ="I will use your face\nas a wallball target...", family="xkcd")+   xkcdline(aes(xbegin=xbegin,ybegin=ybegin,xend=xend,yend=yend),datalines, xjitteramount =0.11)

Leave a Reply

Your email address will not be published. Required fields are marked *