-
Notifications
You must be signed in to change notification settings - Fork 0
/
05-plotly.qmd
151 lines (116 loc) · 2.91 KB
/
05-plotly.qmd
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
---
title: "Quarto & Plotly"
format:
html:
toc: true
toc-title: "TOC"
css: ./css/style.css
---
# R: Prepare data
```{r message=FALSE}
library(tidyverse)
```
```{r}
data <- palmerpenguins::penguins %>%
mutate(across(where(is.factor), as.character)) %>%
filter(!is.na(sex))
```
```{r}
data %>%
ggplot(aes(x = sex, group = island, fill = island)) +
geom_bar(position = position_dodge())
```
```{r}
head(data)
```
# OJS: Expose data for OJS
```{r}
ojs_define(data = transpose(data))
```
> d3 is needed for grouping / aggregation
```{ojs}
d3 = require("d3-array")
```
```{ojs}
Plotly = require("https://cdn.plot.ly/plotly-2.16.1.min.js")
```
```{ojs}
Inputs.table(data)
```
# Aggregate (with JS / D3)
> Goal: reduce data to group counts with JavaScript
This way we preserve runtime reactivity and don't need to rely on static R inputs
> Group by Species & Sex, Count Totals (length)
```{ojs}
data_aggregated = d3
.flatRollup(
data,
(facet) => facet.length, // index: 2
(row) => row.island, // index: 0
(row) => row.sex // index: 1
)
// abstraction / parametarisation for Plotly (x,y, name) here
// .map((entry) => ({ island: entry[0], sex: entry[1], n: entry[2] }))
.map((entry) => ({ name: entry[0], x: entry[1], y: entry[2] }))
.sort((a,b) => a.name.localeCompare(b.name)) // to put facet names in A-Z order
```
```{ojs}
data_grouped = d3.group(data_aggregated, (d) => d.name)
```
## Facet Result Template
> Create a template for an object for a single facet (~ trace)
```{ojs}
toVars = ["x", "y", "name", "type"]
```
```{ojs}
resultObject = ({})
```
```{ojs}
//| include: false
toVars.forEach((variable) => (resultObject[variable] = []))
```
```{ojs}
//| eval: false
// the manual mapping is only necessary if you don't abstract during aggregation
fromVars = [
{from: "sex", to: "x"},
{from: "n", to: "y" }
]
```
## Aggregate Facets
```{ojs}
result = [...data_grouped.entries()].map((trace) => {
const traceObj = JSON.parse(JSON.stringify(resultObject)); // copy the Object template
traceObj.name = trace[0]; // facet's name / Map's key (here: island)
traceObj.type = "bar"; // TODO: make global param
traceObj.text = trace[1].map((entry) => `n: ${entry.y}`); // TODO: make n a param
trace[1].forEach((entry) => {
// fromVars.forEach((mapping) => {
// traceObj[mapping.to].push(entry[mapping.from]);
["x", "y"].forEach((mapping) => {
traceObj[mapping].push(entry[mapping]);
});
});
return traceObj;
})
```
# Plot data with Plotly
## Plot Options
```{ojs}
options = ({
title: "Count of Penguins by Gender & Island",
barmode: doStack ? "stack" : "group" // "group", "stack", "relative"
})
```
## Reactive Stack Toogle
```{ojs}
//| echo: false
viewof doStack = Inputs.toggle({ label: "Stack", value: false })
```
## Plotly Plot
```{ojs}
//| include: false
Plotly.newPlot("plot-canvas", result, options)
```
::: {#plot-canvas}
:::