-
Notifications
You must be signed in to change notification settings - Fork 1
/
pgog.Rmd
395 lines (266 loc) · 8.59 KB
/
pgog.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
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
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
---
title: "Examples"
author: "Vignette Author"
date: "`r Sys.Date()`"
output: rmarkdown::html_vignette
vignette: >
%\VignetteIndexEntry{Vignette Title}
%\VignetteEncoding{UTF-8}
%\VignetteEngine{knitr::rmarkdown}
editor_options:
chunk_output_type: inline
---
editor_options:
chunk_output_type: console
```{r setup, include = FALSE}
knitr::opts_chunk$set(
collapse = TRUE,
comment = "#>"
)
library(tidyverse)
library(gridExtra)
library(ggplot2)
library(pgog)
```
# Override default themes
```{r}
custom_theme <- theme_minimal() +
theme(axis.title.y = element_blank(),
axis.text.y = element_blank(),
axis.text.x = element_text(vjust=5),
panel.grid = element_blank(),
axis.ticks = element_blank(),
# axis.ticks = element_line(),
legend.position = "none"
)
theme_set(custom_theme)
colorbrewer2 <- rev(c(
"#f7fcf0",
"#e0f3db",
"#ccebc5",
"#a8ddb5",
"#7bccc4",
"#4eb3d3",
"#2b8cbe",
"#0868ac",
"#084081"))
colorbrewer2_warm <- rev(c(
"#ffffb2",
"#fecc5c",
"#fd8d3c",
"#e31a1c"
))
names(colorbrewer2) <- 1:length(colorbrewer2)
# https://stackoverflow.com/questions/10504724/change-the-default-colour-palette-in-ggplot
# scale_aesthetic_discrete <- function(...) scale_fill_manual(values = colorbrewer2)
scale_fill_discrete <- function(...) scale_fill_manual(values = colorbrewer2)
# to our great dismay scale_color_* doesn't work
scale_colour_discrete <- function(...) scale_colour_manual(values = colorbrewer2_warm)
```
# geom_bloc
Assuming B,C, etc. are discrete variables
## Product/area plots for discrete A
### {w,h} <- P(A)
```{r}
ggplot(mtcars) + geom_bloc(aes(width = c(P(cyl))))
```
### {w,h} <- P(A), {x,y} <- A
```{r}
ggplot(mtcars) + geom_bloc(aes(height = c(P(cyl)), x = c(cyl)))
```
TODO: new combination
w <- P(A)
x <-A
### {w,h} <- P(A|B), {x,y} <- (A),B
```{r}
ggplot(mtcars) + geom_bloc(aes(height = c(P(cyl|gear)), x = c(gear), fill = factor(cyl)))
```
```{r}
ggplot(mtcars) + geom_bloc(aes(height = c(P(cyl|gear)), x = c(cyl), y = c(gear),fill = factor(gear)))
```
### {w,h} <- P(A|B,C), {x,y} <- B,C
```{r}
ggplot(mtcars) + geom_bloc(aes(height = c(P(cyl|gear, carb)), x = c(gear), y=c(carb), fill = factor(cyl)))
```
### 1+ probabilistic variables
```{r}
ggplot(mtcars) + geom_bloc(aes(height = c(P(gear|am), P(cyl|gear, am)),
x = c(am)))
ggplot(mtcars) + geom_bloc(aes(height = c(P(gear|am)),
width = c(P(cyl|gear, am)),
y = c(am)))
ggplot(mtcars) + geom_bloc(aes(height = c(P(gear|am), P(cyl|gear, am)),
x = gear,
y = am))
```
## Density plots for continuous A
Currently PGoG only includes `x, height` or `y, width` combinations for density plots. All of the examples in this subsection should work `y, width` as well.
### {w,h} <- P(A)
Doesn't exist yet in the grammar.
### h <- P(A), x <- A
```{r}
ggplot(mtcars) + geom_bloc(aes(x = c(mpg), height = c(P(mpg))))
```
Alternatively, w <- P(A), y <- A
TODO: this is not rotated hmmm issue #59
```{r}
ggplot(mtcars) + geom_bloc(aes(y = c(mpg), width = c(P(mpg))))
ggplot(mtcars) + geom_bloc(aes(y = c(mpg), width = c(P(mpg))),side = "up")
ggplot(mtcars) + geom_bloc(aes(y = c(mpg), width = c(P(mpg))),side = "down")
ggplot(mtcars) + geom_bloc(aes(y = c(mpg), width = c(P(mpg))),side = "both")
```
### h <- P(A|B), x <- A, f <- B
`fill = cyl` cannot be omitted
```{r}
ggplot(mtcars) + geom_bloc(aes(x = mpg, fill = cyl, height = c(P(mpg | cyl))))
ggplot(mtcars) + geom_bloc(aes(x = mpg, fill = cyl, height = c(P(mpg | cyl))),side = "up")
ggplot(mtcars) + geom_bloc(aes(x = mpg, fill = cyl, height = c(P(mpg | cyl))),side = "down")
ggplot(mtcars) + geom_bloc(aes(x = mpg, fill = cyl, height = c(P(mpg | cyl))),side = "both")
```
### h <- P(A|B), x <- A, y <- B
Ridge plots
```{r}
ggplot(mtcars) + geom_bloc(aes(x = c(mpg), height = c(P(mpg | cyl)), y = c(cyl), fill = cyl))
```
### h <- P(A|B,C), x <- A, y <- B, f <- C
More conditionals
```{r}
ggplot(mtcars) + geom_bloc(
aes(
x = c(mpg),
y = c(gear),
fill = factor(cyl), # TODO: factor() not working
height = c(P(mpg | gear, cyl))
))
```
Issue #58 TODO/bug: this should be different from above; should be faceting?
```{r}
ggplot(mtcars) + geom_bloc(aes(x = c(mpg,cyl), y = c(gear), fill=(cyl), height = c(P(mpg | gear, cyl))))
```
### h <- P(B|A), x <- A, f <- B
TODO/bug: position needs to be "fill" in `geom_bloc`
```{r}
ggplot(mtcars) +
geom_bloc(aes(x = c(mpg),
height = c(P(cyl | mpg)),
fill = factor(cyl)))
```
Issue #58 TODO/bug
```{r}
ggplot(mtcars) + geom_bloc(aes(x = c(mpg, gear), height = c(P(cyl | gear,mpg))))
```
### 1+ probabilistic variables
More than one probabilistic variables in the spec
```{r}
common_bw <- 1.5
ggplot(mtcars) + geom_bloc(aes(
x = c(mpg),
height = c(P(cyl|mpg), P(mpg)),
fill = factor(cyl)), bw = common_bw) +
xlab("mpg")
```
```{r}
# TODO: this is wrong
ggplot(mtcars) +
geom_bloc(aes(x = c(hp, gear),
fill = factor(cyl),
height = c(P(cyl|hp, gear), P(hp|gear))))
```
# geom_icon
## Discrete var (icon arrays)
### {w,h} <- P(A)
aka spine plots
```{r}
ggplot(mtcars) + geom_icon(aes(height=c(P(cyl))))
```
```{r}
ggplot(mtcars) + geom_icon(aes(width=c(P(cyl))))
```
TODO: can't add fill color Computation failed in `stat_icon()`: could not find function "divider"
https://github.com/MUCollective/pgog/issues/60
```{r}
# ggplot(mtcars) + geom_icon(aes(height=c(P(cyl)), fill= cyl))
```
### {w,h} <- P(A), x,y <- A
bar charts filled with icons
```{r}
#ggplot(mtcars) + geom_icon(aes(
# height = c(P(cyl)),
# x = c(cyl),
# fill = c(factor(cyl)
# )))
```
This is just ugly
|
V
```{r}
#ggplot(mtcars) + geom_icon(aes(
# width = c(P(cyl)),
# y = c(cyl),
# fill = c(factor(cyl)
# )))
```
### {w,h} <- P(A|B), {x,y} <- (A),B
```{r}
ggplot(mtcars) + geom_icon(aes(height = c(P(cyl|gear)), x = c(gear)))
```
TODO: this one is supposed to be different from the prev one?
```{r}
ggplot(mtcars) + geom_icon(aes(height = c(P(cyl|gear)), x = c(cyl, gear)))
```
TODO: these spec are wrong why does the parser not catch it
```{r}
# ggplot(mtcars) + geom_icon(aes(height = c(P(gear)), x = c(gear, cyl)))
# ggplot(mtcars) + geom_icon(aes(height = c(P(cyl|gear), P(gear)), x = c(gear, vs)))
```
### {w,h} <- P(A|B,C), {x,y} <- B,C
```{r}
ggplot(mtcars) +
geom_icon(aes(height = c(P(cyl|gear, vs)), x = c(gear), y=c(vs)))
```
## Continuous var (dotplots)
TODO: need implementing
### {w,h} <- P(A), {x,y} <- A
```{r}
# ggplot(mtcars) + geom_icon(aes(height=c(P(mpg))))
```
###
```{r}
```
## x.width and y.height
```{r}
ggplot(mtcars) + geom_bloc(aes(width = c(P(cyl)), x = c(cyl)))
```
```{r}
# ggplot(mtcars) + geom_icon(aes(width=c(P(mpg))))
```
### {w,h} <- P(A), x,y <- A
### {w,h} <- P(A|B), {x,y} <- (A),B
### {w,h} <- P(A|B,C), {x,y} <- B,C
```{r}
df_test_1 = data.frame(generation = c("Post-Millennials in 2018"),
race = c(rep("White",52),rep("Hispanic",25),rep("Black",14),rep("Asian",6),rep("Other",4)))
df_test_2 = data.frame(generation = c("Millennials in 2002"),
race = c(rep("White",61),rep("Hispanic",18),rep("Black",15),rep("Asian",4),rep("Other",1)))
df_test_3 = data.frame(generation = c("Gen-Xer in 1986"),
race = c(rep("White",70),rep("Hispanic",12),rep("Black",15),rep("Other",3)))
df_test_4 = data.frame(generation = c("Early Boomers in 1986"),
race = c(rep("White",82),rep("Hispanic",4),rep("Black",13),rep("Asian",1),rep("Other",1)))
df_total = rbind(df_test_1,df_test_2,df_test_3,df_test_4)
df_total = df_total %>% mutate(generation = fct_relevel(generation, "Early Boomers in 1986","Gen-Xer in 1986","Millennials in 2002", "Post-Millennials in 2018"), race = fct_relevel(race, "White", "Hispanic", "Black", "Asian", "Other"))
ggplot(df_total) + geom_bloc(aes(width = c(P(race|generation)), x = c(race), y = c(generation),fill = factor(generation))) + theme_bw()
```
```{r}
#ggplot(df_total) + geom_bloc(aes(width = c(P(race|generation)), x = c(race), y = c(generation),fill = #generation), fill = "black") + theme_bw()
```
test is_continuous() in stat_Bloc
```{r}
ggplot(mtcars) + geom_bloc(aes(height = c(P(cyl)), x = c(cyl)))
ggplot(mtcars) + geom_bloc(aes(x = c(mpg), height = c(P(mpg))))
```
```{r}
ggplot(mtcars) + geom_bloc(aes(y = c(mpg), width = c(P(mpg | cyl)), x = c(cyl), fill = cyl))
```
```{r}
ggplot(mtcars) + geom_bloc(aes(x = c(mpg), height = c(P(mpg | cyl)), y = c(cyl), fill = cyl))
```