-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpatchwork.Rmd
More file actions
257 lines (207 loc) · 7.43 KB
/
Copy pathpatchwork.Rmd
File metadata and controls
257 lines (207 loc) · 7.43 KB
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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
---
title: "The Aplotalypse"
author: "Emily Nordmann"
date: "13/04/2020"
output: html_document
---
```{r}
library(extrafont)
library(tidyverse)
library(readxl)
library(patchwork)
library(gridExtra)
library(ggpubr)
library(waffle)
library(magick)
library(png)
library(grid)
patchwork_game <- read_excel("games.xlsx", sheet = "Patchwork")%>%
drop_na()
```
# Set up colours for the graphs
```{r}
patchwork_colours <- c("#67A400", "#F1BF00")
```
# Theme stuff
```{r}
my_theme <- theme(plot.title = element_text(size=20,
family = "Verdana",
face = "bold",
hjust = 0.5),
plot.subtitle = element_text(size = 10,
family = "Verdana",
hjust = 0.5),
axis.line = element_blank(),
axis.ticks = element_blank(),
plot.background = element_rect("#C6D2FA"),
legend.background = element_rect("#C6D2FA"),
legend.text = element_text(family = "Verdana"),
panel.background = element_rect("#C6D2FA"))
```
# Patchwork
```{r}
# calculate percent wins for each
patchwork_wins <- patchwork_game %>%
group_by(Winner) %>%
count() %>%
ungroup() %>%
mutate(prop = n/sum(n),
percent = prop * 100,
ymax = cumsum(prop),
ymin = c(0, head(ymax, n=-1)),
labelPosition = (ymax + ymin) /2,
label = paste0(Winner, "\n ", round(percent,2), "%"))
```
Waffle plot - who wins Patchwork?
```{r}
p1 <- patchwork_game %>%
count(Winner) %>%
ggplot(aes(fill = Winner, values = n)) +
labs(title = "Winner", fill = NULL, colour = NULL) +
guides(fill = NULL, colour = NULL, x = NULL, y = NULL) +
geom_waffle(n_rows = 10, # number of rows
size = .8, # thickness of tile line
colour = "black", # colour of tile line
flip = F, # controls if split left/right or up/down
radius = unit(4, "pt"), # adds gap between tiles
show.legend = F,
make_proportional = TRUE) +
scale_fill_manual(values = patchwork_colours) + # add patchwork colours
geom_label(family = "Verdana",
x=c(3.5, 8),
aes(y=5.5,
label=patchwork_wins$label),
colour = c("black"),
show.legend = F,
size = 4,
fill = "white",
label.padding = unit(.5, "lines"),
label.r = unit(0.75, "lines"), #roundness of outer label line
label.size = 1.5) + # thickness of outer label line
theme_enhance_waffle() +
my_theme
```
```{r}
p2 <- patchwork_game %>%
summarise(emily_buttons = mean(`E-buttons`),
kathleen_buttons = mean(`K-buttons`),
emily_spaces = mean(`E-spaces`),
kathleen_spaces = mean(`K-spaces`)) %>%
pivot_longer(cols = emily_buttons:kathleen_spaces,
names_to = "player",
values_to = "avg_number") %>%
separate(col = player, into = c("player", "component")) %>%
ggplot(aes(x = component, y = avg_number, fill = player)) +
geom_col(position = "dodge", colour = "black", size = .9) +
labs(x = NULL, y = NULL) +
scale_x_discrete(labels = c("Buttons (1pt)", "Spaces (-2pts)"))+
scale_fill_manual(values = patchwork_colours, name = "Player",
labels = c("Emily", "Kathleen"))+
scale_y_continuous(breaks = seq(0, 25, 5))+
labs(title= "Strategy (win or lose)", subtitle = "Avg. buttons and spaces at endgame") +
theme(axis.line = element_blank(),
axis.ticks = element_blank()) +
my_theme
```
```{r}
patchwork_game <- patchwork_game %>%
mutate(special = recode(`E-special`, "7" = "Emily",
"0" = "Kathleen"))
p3 <- patchwork_game %>%
count(special) %>%
ggplot(aes(fill = special, values = n)) +
expand_limits(x=c(0,0), y=c(0,0)) +
labs(title = "Special tile",
subtitle = "Cover 7x7 squares for 7 bonus points",
fill = NULL, colour = NULL) +
guides(fill = NULL, colour = NULL) +
theme_enhance_waffle() +
geom_waffle(n_rows = 10, # number of rows
size = .8, # thickness of tile line
colour = "black", # colour of tile line
flip = F, # controls if split left/right or up/down
radius = unit(4, "pt"), # adds gap between tiles
show.legend = F,
make_proportional = TRUE) +
scale_fill_manual(values = patchwork_colours) +
geom_label(family = "Verdana",
x=c(3.5, 8),
aes(y=5.5, label= paste0(special,"\n ", round((n/sum(n)*100),2), "%"),size=4), colour = c("black"),
show.legend = F,
size = 4,
fill = "white",
label.padding = unit(.5, "lines"),
label.r = unit(0.75, "lines"), #roundness of outer label line
label.size = 1.5) + # thickness of outer label line) +
my_theme
```
Difference score
```{r}
mean_diff <- group_by(patchwork_game, Winner) %>%
summarise(mean_diff = abs(mean(Difference))) %>%
mutate(label = paste0(Winner , " Average \n ", round(mean_diff,2)))
p4 <- ggplot(patchwork_game,
aes(x = reorder(Game, Difference), y = Difference, fill = Winner))+
geom_col(colour = "black", show.legend = T, size = .7) +
scale_fill_manual(values = patchwork_colours) +
scale_x_discrete(breaks = NULL) +
scale_y_continuous(breaks = seq(-50, 50, by = 10),
labels = c(50,40,30,20,10,0,10,20,30,40,50)) +
labs(title = "Win margin",
subtitle = "How much does each player win by?",
x = NULL, y = "Point difference") +
coord_flip()+
my_theme
```
Who goes first and who wins?
```{r}
p5 <- patchwork_game %>%
group_by(First, Winner)%>%
count() %>%
ungroup() %>%
group_by(First) %>%
mutate(percent = n/(sum(n))*100) %>%
ggplot(aes(x = First, y = percent, fill = Winner)) +
geom_col(position = "dodge", colour = "black") +
labs(title = "Pole position",
subtitle = "Does the player who goes first have an advantage?",
x = "Who goes first?", y = "% wins") +
scale_x_discrete(labels = c("Emily", "Kathleen"))+
scale_fill_manual(values = patchwork_colours, name = "Who wins?",
labels = c("Emily", "Kathleen"))+
my_theme
```
```{r}
patchwork_game %>%
group_by(Winner, special) %>%
count() %>%
ungroup(special) %>%
mutate(prop = n/sum(n)) %>%
ggplot(aes(x = Winner, y =prop, fill = special)) +
geom_col(colour = "black") +
scale_fill_manual(values = patchwork_colours, name = "Special tile") +
scale_y_continuous(labels = scales::percent) +
labs(y = NULL, title = "Special tile vs Winner", subtitle = "How often does the person who wins have the special tile?") +
my_theme +
theme(legend.position = "bottom")
ggsave("special_tile.png", width = 8, height = 7)
```
Put it all together
```{r fig.width=10, fig.height=8}
logo <- image_read("patchwork.png") # from png
# stitch together plots using patchwork
p6 <- ggplot() +
background_image(logo) + # from ggpubr
labs(title = "Game 1: Patchwork",
subtitle = paste0("Games = ", nrow(patchwork_game))) +
coord_fixed() +
my_theme
p6 + p5 + p1 + p4 + p3 + p2 +
plot_layout(ncol = 2,
nrow = 3,
widths = c(2,2)) +
plot_annotation(
caption = '@emilynordmann'
)
ggsave(filename = "patchwork_full.png", width = 8, height = 8)
```