-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathxkcd-stickfigures.rmd
112 lines (85 loc) · 2.83 KB
/
xkcd-stickfigures.rmd
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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
---
title: "xkcd"
output: html_document
---
```{r setup, include=FALSE}
knitr::opts_chunk$set( echo = TRUE, fig.width=10, fig.height=10, warning=F, message=F )
```
```{r, eval=F}
install.packages("xkcd",dependencies = TRUE)
install.packages("gridExtra")
install.packages("cowplot")
```
```{r}
library( ggplot2 )
library( gridExtra )
library( cowplot )
library( xkcd )
```
# xkcd Style
```{r}
xrange <- range(mtcars$mpg)
yrange <- range(mtcars$wt)
set.seed(123) # for reproducibility
p <- ggplot() + geom_point(aes(mpg, wt), data=mtcars) +
xkcdaxis(xrange,yrange)
p
```
# Add Stick Figure
```{r}
ratioxy <- diff(xrange)/diff(yrange)
mapping <- aes( x, y, scale, ratioxy,
angleofspine,
anglerighthumerus,
anglelefthumerus,
anglerightradius,
angleleftradius,
anglerightleg,
angleleftleg,
angleofneck,
linetype=city )
dataman <- data.frame(x= c(15,30), y=c(3, 4),
scale = c(0.3,0.51) ,
ratioxy = ratioxy,
angleofspine = -pi/2 ,
anglerighthumerus = c(pi/4, -pi/6),
anglelefthumerus = c(pi/2 + pi/4, pi +pi/6),
anglerightradius = c(pi/3, -pi/3),
angleleftradius = c(pi/3, -pi/3),
anglerightleg = 3*pi/2 - pi / 12,
angleleftleg = 3*pi/2 + pi / 12 ,
angleofneck = runif(1, 3*pi/2-pi/10, 3*pi/2+pi/10),
city=c("Liliput","Brobdingnag") )
p <- ggplot() + geom_point(aes(mpg, wt, colour=as.character(vs)), data=mtcars) +
xkcdaxis(xrange,yrange) +
xkcdman(mapping, dataman) +
theme(legend.position = "none")
p
```
# Grid Options
```{r}
# library( gridExtra )
grid.arrange( p, p, p, p, ncol=2, nrow=2 )
# library( cowplot )
plot_grid( p, p, p, p,
labels= c("A","B","C","D"), ncol=2, nrow=2 )
```
```{r}
dataman <- data.frame(x= c(15,30), y=c(3, 4),
scale = c(0.3,0.51) ,
ratioxy = ratioxy,
angleofspine = -pi,
anglerighthumerus = c(pi/2, -pi/3),
anglelefthumerus = c(pi/2 + pi/4, pi +pi/6),
anglerightradius = c(pi/3, -pi/3),
angleleftradius = c(pi/6, -pi/2),
anglerightleg = 3*pi/2 - pi / 8,
angleleftleg = 3*pi/2 + pi / 12 ,
angleofneck = runif(1, 3*pi/2-pi/10, 3*pi/2+pi/10),
city=c("Liliput","Brobdingnag") )
p <- ggplot() + geom_point(aes(mpg, wt, colour=as.character(vs)), data=mtcars) +
xkcdaxis(xrange,yrange) +
xkcdman(mapping, dataman) +
theme(legend.position = "none")
p
```