-
Notifications
You must be signed in to change notification settings - Fork 0
/
FromStaticToInteractive.html
591 lines (553 loc) · 39.4 KB
/
FromStaticToInteractive.html
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
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml" lang="en" xml:lang="en"><head>
<meta charset="utf-8">
<meta name="generator" content="quarto-1.3.353">
<meta name="viewport" content="width=device-width, initial-scale=1.0, user-scalable=yes">
<meta name="author" content="Norah Jones- Modified by Pablo Adames">
<title>The observable plot example</title>
<style>
code{white-space: pre-wrap;}
span.smallcaps{font-variant: small-caps;}
div.columns{display: flex; gap: min(4vw, 1.5em);}
div.column{flex: auto; overflow-x: auto;}
div.hanging-indent{margin-left: 1.5em; text-indent: -1.5em;}
ul.task-list{list-style: none;}
ul.task-list li input[type="checkbox"] {
width: 0.8em;
margin: 0 0.8em 0.2em -1em; /* quarto-specific, see https://github.com/quarto-dev/quarto-cli/issues/4556 */
vertical-align: middle;
}
/* CSS for syntax highlighting */
pre > code.sourceCode { white-space: pre; position: relative; }
pre > code.sourceCode > span { display: inline-block; line-height: 1.25; }
pre > code.sourceCode > span:empty { height: 1.2em; }
.sourceCode { overflow: visible; }
code.sourceCode > span { color: inherit; text-decoration: inherit; }
div.sourceCode { margin: 1em 0; }
pre.sourceCode { margin: 0; }
@media screen {
div.sourceCode { overflow: auto; }
}
@media print {
pre > code.sourceCode { white-space: pre-wrap; }
pre > code.sourceCode > span { text-indent: -5em; padding-left: 5em; }
}
pre.numberSource code
{ counter-reset: source-line 0; }
pre.numberSource code > span
{ position: relative; left: -4em; counter-increment: source-line; }
pre.numberSource code > span > a:first-child::before
{ content: counter(source-line);
position: relative; left: -1em; text-align: right; vertical-align: baseline;
border: none; display: inline-block;
-webkit-touch-callout: none; -webkit-user-select: none;
-khtml-user-select: none; -moz-user-select: none;
-ms-user-select: none; user-select: none;
padding: 0 4px; width: 4em;
}
pre.numberSource { margin-left: 3em; padding-left: 4px; }
div.sourceCode
{ }
@media screen {
pre > code.sourceCode > span > a:first-child::before { text-decoration: underline; }
}
</style>
<script src="FromStaticToInteractive_files/libs/clipboard/clipboard.min.js"></script>
<script src="FromStaticToInteractive_files/libs/quarto-html/quarto.js"></script>
<script src="FromStaticToInteractive_files/libs/quarto-html/popper.min.js"></script>
<script src="FromStaticToInteractive_files/libs/quarto-html/tippy.umd.min.js"></script>
<script src="FromStaticToInteractive_files/libs/quarto-html/anchor.min.js"></script>
<link href="FromStaticToInteractive_files/libs/quarto-html/tippy.css" rel="stylesheet">
<link href="FromStaticToInteractive_files/libs/quarto-html/quarto-syntax-highlighting.css" rel="stylesheet" id="quarto-text-highlighting-styles">
<script src="FromStaticToInteractive_files/libs/bootstrap/bootstrap.min.js"></script>
<link href="FromStaticToInteractive_files/libs/bootstrap/bootstrap-icons.css" rel="stylesheet">
<link href="FromStaticToInteractive_files/libs/bootstrap/bootstrap.min.css" rel="stylesheet" id="quarto-bootstrap" data-mode="light">
<script type="module" src="FromStaticToInteractive_files/libs/quarto-ojs/quarto-ojs-runtime.js"></script>
<link href="FromStaticToInteractive_files/libs/quarto-ojs/quarto-ojs.css" rel="stylesheet">
<script src="https://polyfill.io/v3/polyfill.min.js?features=es6"></script>
<script src="https://cdn.jsdelivr.net/npm/mathjax@3/es5/tex-chtml-full.js" type="text/javascript"></script>
</head>
<body class="fullcontent">
<div id="quarto-content" class="page-columns page-rows-contents page-layout-article">
<main class="content" id="quarto-document-content">
<header id="title-block-header" class="quarto-title-block default">
<div class="quarto-title">
<h1 class="title">The observable plot example</h1>
</div>
<div class="quarto-title-meta">
<div>
<div class="quarto-title-meta-heading">Author</div>
<div class="quarto-title-meta-contents">
<p>Norah Jones- Modified by Pablo Adames </p>
</div>
</div>
</div>
</header>
<section id="motivation" class="level2">
<h2 class="anchored" data-anchor-id="motivation">Motivation</h2>
<p>When an interactive visualization is the goal, the recent observable plot module is pretty convenient.</p>
<p>The data can be obtained up to date from https://www.ncdc.noaa.gov/cdo-web/search for the Seattle-Tacoma international airport weather station dating back to 1941.</p>
</section>
<section id="some-data-cleaning" class="level2">
<h2 class="anchored" data-anchor-id="some-data-cleaning">Some data cleaning</h2>
<div class="cell">
<details>
<summary>Code</summary>
<div class="sourceCode cell-code" id="cb1"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb1-1"><a href="#cb1-1" aria-hidden="true" tabindex="-1"></a>raw_data <span class="ot"><-</span> <span class="fu">read.csv</span>(<span class="st">"data/3868957.csv"</span>, <span class="at">header=</span><span class="cn">TRUE</span>, <span class="at">stringsAsFactors=</span><span class="cn">FALSE</span>) </span>
<span id="cb1-2"><a href="#cb1-2" aria-hidden="true" tabindex="-1"></a>df <span class="ot"><-</span> <span class="fu">data.frame</span>( <span class="at">dates=</span><span class="fu">as.Date</span>(raw_data<span class="sc">$</span>DATE, <span class="at">format =</span> <span class="st">"%Y-%m-%d"</span>))</span>
<span id="cb1-3"><a href="#cb1-3" aria-hidden="true" tabindex="-1"></a>df[<span class="st">"precipitation"</span>] <span class="ot"><-</span> raw_data<span class="sc">$</span>PRCP</span>
<span id="cb1-4"><a href="#cb1-4" aria-hidden="true" tabindex="-1"></a>df<span class="sc">$</span>STATION <span class="ot"><-</span> raw_data<span class="sc">$</span>STATION</span>
<span id="cb1-5"><a href="#cb1-5" aria-hidden="true" tabindex="-1"></a>df<span class="sc">$</span>NAME <span class="ot"><-</span> raw_data<span class="sc">$</span>NAME</span>
<span id="cb1-6"><a href="#cb1-6" aria-hidden="true" tabindex="-1"></a><span class="fu">head</span>(df<span class="sc">$</span>precipitation)</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
</details>
<div class="cell-output cell-output-stdout">
<pre><code>[1] 0.00 0.43 0.03 0.80 0.05 0.10</code></pre>
</div>
<details>
<summary>Code</summary>
<div class="sourceCode cell-code" id="cb3"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb3-1"><a href="#cb3-1" aria-hidden="true" tabindex="-1"></a><span class="fu">write.csv</span>(df, <span class="st">"data/seatle-data.csv"</span>, <span class="at">row.names =</span> <span class="cn">FALSE</span>)</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
</details>
</div>
</section>
<section id="seattle-precipitation-by-day-2012-to-2023" class="level2">
<h2 class="anchored" data-anchor-id="seattle-precipitation-by-day-2012-to-2023">Seattle Precipitation by Day (2012 to 2023)</h2>
<p>Now we can create the interactive visualization to investigate how net precipitation has evolved over the past 11 years at the Seattle-Tacoma International airport weather station.</p>
<div class="cell">
<details>
<summary>Code</summary>
<div class="sourceCode cell-code" id="cb4" data-startfrom="32" data-source-offset="-0"><pre class="sourceCode js code-with-copy"><code class="sourceCode javascript" style="counter-reset: source-line 31;"><span id="cb4-32"><a href="#cb4-32" aria-hidden="true" tabindex="-1"></a>precipitations <span class="op">=</span> <span class="fu">FileAttachment</span>(<span class="st">"data/seatle-data.csv"</span>)</span>
<span id="cb4-33"><a href="#cb4-33" aria-hidden="true" tabindex="-1"></a> <span class="op">.</span><span class="fu">csv</span>({<span class="dt">typed</span><span class="op">:</span> <span class="kw">true</span>})</span>
<span id="cb4-34"><a href="#cb4-34" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb4-35"><a href="#cb4-35" aria-hidden="true" tabindex="-1"></a><span class="co">//create views</span></span>
<span id="cb4-36"><a href="#cb4-36" aria-hidden="true" tabindex="-1"></a>viewof beginYear <span class="op">=</span> Inputs<span class="op">.</span><span class="fu">range</span>(</span>
<span id="cb4-37"><a href="#cb4-37" aria-hidden="true" tabindex="-1"></a> [<span class="dv">2012</span><span class="op">,</span> <span class="dv">2023</span>]<span class="op">,</span> </span>
<span id="cb4-38"><a href="#cb4-38" aria-hidden="true" tabindex="-1"></a> {<span class="dt">value</span><span class="op">:</span> <span class="dv">2012</span><span class="op">,</span> <span class="dt">step</span><span class="op">:</span> <span class="dv">1</span><span class="op">,</span> <span class="dt">label</span><span class="op">:</span> <span class="st">"Begin year:"</span>}</span>
<span id="cb4-39"><a href="#cb4-39" aria-hidden="true" tabindex="-1"></a>)</span>
<span id="cb4-40"><a href="#cb4-40" aria-hidden="true" tabindex="-1"></a>viewof endYear <span class="op">=</span> Inputs<span class="op">.</span><span class="fu">range</span>(</span>
<span id="cb4-41"><a href="#cb4-41" aria-hidden="true" tabindex="-1"></a> [beginYear<span class="op">,</span> <span class="dv">2023</span>]<span class="op">,</span> </span>
<span id="cb4-42"><a href="#cb4-42" aria-hidden="true" tabindex="-1"></a> { <span class="dt">value</span><span class="op">:</span> [beginYear]<span class="op">,</span> </span>
<span id="cb4-43"><a href="#cb4-43" aria-hidden="true" tabindex="-1"></a> <span class="dt">step</span><span class="op">:</span> <span class="dv">1</span><span class="op">,</span> <span class="dt">label</span><span class="op">:</span> <span class="st">"End year:"</span></span>
<span id="cb4-44"><a href="#cb4-44" aria-hidden="true" tabindex="-1"></a> }</span>
<span id="cb4-45"><a href="#cb4-45" aria-hidden="true" tabindex="-1"></a>)</span>
<span id="cb4-46"><a href="#cb4-46" aria-hidden="true" tabindex="-1"></a>viewof stat <span class="op">=</span> Inputs<span class="op">.</span><span class="fu">radio</span>(</span>
<span id="cb4-47"><a href="#cb4-47" aria-hidden="true" tabindex="-1"></a> [<span class="st">"mean"</span><span class="op">,</span> <span class="st">"max"</span><span class="op">,</span> <span class="st">"min"</span>]<span class="op">,</span> </span>
<span id="cb4-48"><a href="#cb4-48" aria-hidden="true" tabindex="-1"></a> { <span class="dt">value</span><span class="op">:</span> <span class="st">"mean"</span><span class="op">,</span> </span>
<span id="cb4-49"><a href="#cb4-49" aria-hidden="true" tabindex="-1"></a> <span class="dt">label</span><span class="op">:</span> <span class="st">"Statistic:"</span></span>
<span id="cb4-50"><a href="#cb4-50" aria-hidden="true" tabindex="-1"></a> }</span>
<span id="cb4-51"><a href="#cb4-51" aria-hidden="true" tabindex="-1"></a>)</span>
<span id="cb4-52"><a href="#cb4-52" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb4-53"><a href="#cb4-53" aria-hidden="true" tabindex="-1"></a><span class="co">//filter the data read from file</span></span>
<span id="cb4-54"><a href="#cb4-54" aria-hidden="true" tabindex="-1"></a>filtered <span class="op">=</span> precipitations<span class="op">.</span><span class="fu">filter</span>(<span class="kw">function</span>(precipitations) {</span>
<span id="cb4-55"><a href="#cb4-55" aria-hidden="true" tabindex="-1"></a> <span class="kw">let</span> years <span class="op">=</span> <span class="kw">new</span> <span class="bu">Date</span>(precipitations<span class="op">.</span><span class="at">dates</span>)<span class="op">.</span><span class="fu">getYear</span>() <span class="op">+</span> <span class="dv">1900</span><span class="op">;</span></span>
<span id="cb4-56"><a href="#cb4-56" aria-hidden="true" tabindex="-1"></a> <span class="cf">return</span> (years <span class="op">>=</span> beginYear) <span class="op">&&</span> (years <span class="op"><=</span> endYear)</span>
<span id="cb4-57"><a href="#cb4-57" aria-hidden="true" tabindex="-1"></a>})</span>
<span id="cb4-58"><a href="#cb4-58" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb4-59"><a href="#cb4-59" aria-hidden="true" tabindex="-1"></a><span class="co">// use the filtered data </span></span>
<span id="cb4-60"><a href="#cb4-60" aria-hidden="true" tabindex="-1"></a>Plot<span class="op">.</span><span class="fu">plot</span>({</span>
<span id="cb4-61"><a href="#cb4-61" aria-hidden="true" tabindex="-1"></a> <span class="dt">width</span><span class="op">:</span> <span class="dv">800</span><span class="op">,</span> <span class="dt">height</span><span class="op">:</span> <span class="dv">500</span><span class="op">,</span> <span class="dt">padding</span><span class="op">:</span> <span class="dv">0</span><span class="op">,</span></span>
<span id="cb4-62"><a href="#cb4-62" aria-hidden="true" tabindex="-1"></a> <span class="dt">color</span><span class="op">:</span> { <span class="dt">scheme</span><span class="op">:</span> <span class="st">"blues"</span><span class="op">,</span> <span class="dt">type</span><span class="op">:</span> <span class="st">"sqrt"</span>}<span class="op">,</span></span>
<span id="cb4-63"><a href="#cb4-63" aria-hidden="true" tabindex="-1"></a> <span class="dt">y</span><span class="op">:</span> { <span class="dt">tickFormat</span><span class="op">:</span> i <span class="kw">=></span> <span class="st">"JFMAMJJASOND"</span>[i] }<span class="op">,</span></span>
<span id="cb4-64"><a href="#cb4-64" aria-hidden="true" tabindex="-1"></a> <span class="dt">marks</span><span class="op">:</span> [</span>
<span id="cb4-65"><a href="#cb4-65" aria-hidden="true" tabindex="-1"></a> Plot<span class="op">.</span><span class="fu">cell</span>(filtered<span class="op">,</span> Plot<span class="op">.</span><span class="fu">group</span>({<span class="dt">fill</span><span class="op">:</span> stat}<span class="op">,</span> {</span>
<span id="cb4-66"><a href="#cb4-66" aria-hidden="true" tabindex="-1"></a> <span class="dt">x</span><span class="op">:</span> d <span class="kw">=></span> <span class="kw">new</span> <span class="bu">Date</span>(d<span class="op">.</span><span class="at">dates</span>)<span class="op">.</span><span class="fu">getDate</span>()<span class="op">,</span></span>
<span id="cb4-67"><a href="#cb4-67" aria-hidden="true" tabindex="-1"></a> <span class="dt">y</span><span class="op">:</span> d <span class="kw">=></span> <span class="kw">new</span> <span class="bu">Date</span>(d<span class="op">.</span><span class="at">dates</span>)<span class="op">.</span><span class="fu">getMonth</span>()<span class="op">,</span></span>
<span id="cb4-68"><a href="#cb4-68" aria-hidden="true" tabindex="-1"></a> <span class="dt">fill</span><span class="op">:</span> <span class="st">"precipitation"</span><span class="op">,</span> </span>
<span id="cb4-69"><a href="#cb4-69" aria-hidden="true" tabindex="-1"></a> <span class="dt">inset</span><span class="op">:</span> <span class="fl">0.5</span></span>
<span id="cb4-70"><a href="#cb4-70" aria-hidden="true" tabindex="-1"></a> }))</span>
<span id="cb4-71"><a href="#cb4-71" aria-hidden="true" tabindex="-1"></a> ]</span>
<span id="cb4-72"><a href="#cb4-72" aria-hidden="true" tabindex="-1"></a>})</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
</details>
<div class="cell-output cell-output-display">
<div>
<div id="ojs-cell-1-1" data-nodetype="declaration">
</div>
</div>
</div>
<div class="cell-output cell-output-display">
<div>
<div id="ojs-cell-1-2" data-nodetype="declaration">
</div>
</div>
</div>
<div class="cell-output cell-output-display">
<div>
<div id="ojs-cell-1-3" data-nodetype="declaration">
</div>
</div>
</div>
<div class="cell-output cell-output-display">
<div>
<div id="ojs-cell-1-4" data-nodetype="declaration">
</div>
</div>
</div>
<div class="cell-output cell-output-display">
<div>
<div id="ojs-cell-1-5" data-nodetype="declaration">
</div>
</div>
</div>
<div class="cell-output cell-output-display">
<div>
<div id="ojs-cell-1-6" data-nodetype="expression">
</div>
</div>
</div>
</div>
<hr>
</section>
<section id="improvements" class="level2">
<h2 class="anchored" data-anchor-id="improvements">Improvements</h2>
<p>The numerical result of the statistical functions is mapped to the intensity of the blue colour. However the range of the colour is dependent on the years we are filtering by.</p>
<p>To make this dimension more useful to discover real trends in precipitation over the years, let’s transform the precipitation using the Z-score standarization for the whole data set. We center the data using the mean and normalize using the standard deviation.</p>
<p><span class="math display">\[\begin{eqnarray*} p_z &=& \frac{p - \mu_p}{\sigma_p}\\ \end{eqnarray*}\]</span></p>
<div class="cell">
<details>
<summary>Code</summary>
<div class="sourceCode cell-code" id="cb5"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb5-1"><a href="#cb5-1" aria-hidden="true" tabindex="-1"></a>df<span class="sc">$</span>precipitation_std <span class="ot"><-</span> (df<span class="sc">$</span>precipitation <span class="sc">-</span> <span class="fu">mean</span>(df<span class="sc">$</span>precipitation, <span class="at">na.rm =</span> <span class="cn">TRUE</span>))<span class="sc">/</span><span class="fu">sd</span>(df<span class="sc">$</span>precipitation, <span class="at">na.rm =</span> <span class="cn">TRUE</span>)</span>
<span id="cb5-2"><a href="#cb5-2" aria-hidden="true" tabindex="-1"></a><span class="fu">head</span>(df<span class="sc">$</span>precipitation_std)</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
</details>
<div class="cell-output cell-output-stdout">
<pre><code>[1] -0.44302515 1.23461996 -0.32598014 2.67817505 -0.24795014 -0.05287513</code></pre>
</div>
<details>
<summary>Code</summary>
<div class="sourceCode cell-code" id="cb7"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb7-1"><a href="#cb7-1" aria-hidden="true" tabindex="-1"></a><span class="fu">write.csv</span>(df, <span class="st">"data/seatle-data-std.csv"</span>, <span class="at">row.names =</span> <span class="cn">FALSE</span>)</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
</details>
</div>
<div class="cell">
<details>
<summary>Code</summary>
<div class="sourceCode cell-code" id="cb8" data-startfrom="97" data-source-offset="-0"><pre class="sourceCode js code-with-copy"><code class="sourceCode javascript" style="counter-reset: source-line 96;"><span id="cb8-97"><a href="#cb8-97" aria-hidden="true" tabindex="-1"></a>precipitations_std <span class="op">=</span> <span class="fu">FileAttachment</span>(<span class="st">"data/seatle-data-std.csv"</span>)</span>
<span id="cb8-98"><a href="#cb8-98" aria-hidden="true" tabindex="-1"></a> <span class="op">.</span><span class="fu">csv</span>({<span class="dt">typed</span><span class="op">:</span> <span class="kw">true</span>})</span>
<span id="cb8-99"><a href="#cb8-99" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb8-100"><a href="#cb8-100" aria-hidden="true" tabindex="-1"></a><span class="co">//create views</span></span>
<span id="cb8-101"><a href="#cb8-101" aria-hidden="true" tabindex="-1"></a>viewof beginYear_std <span class="op">=</span> Inputs<span class="op">.</span><span class="fu">range</span>(</span>
<span id="cb8-102"><a href="#cb8-102" aria-hidden="true" tabindex="-1"></a> [<span class="dv">2012</span><span class="op">,</span> <span class="dv">2023</span>]<span class="op">,</span> </span>
<span id="cb8-103"><a href="#cb8-103" aria-hidden="true" tabindex="-1"></a> {<span class="dt">value</span><span class="op">:</span> <span class="dv">2012</span><span class="op">,</span> <span class="dt">step</span><span class="op">:</span> <span class="dv">1</span><span class="op">,</span> <span class="dt">label</span><span class="op">:</span> <span class="st">"Begin year:"</span>}</span>
<span id="cb8-104"><a href="#cb8-104" aria-hidden="true" tabindex="-1"></a>)</span>
<span id="cb8-105"><a href="#cb8-105" aria-hidden="true" tabindex="-1"></a>viewof endYear_std <span class="op">=</span> Inputs<span class="op">.</span><span class="fu">range</span>(</span>
<span id="cb8-106"><a href="#cb8-106" aria-hidden="true" tabindex="-1"></a> [beginYear<span class="op">,</span> <span class="dv">2023</span>]<span class="op">,</span> </span>
<span id="cb8-107"><a href="#cb8-107" aria-hidden="true" tabindex="-1"></a> { <span class="dt">value</span><span class="op">:</span> [beginYear]<span class="op">,</span> </span>
<span id="cb8-108"><a href="#cb8-108" aria-hidden="true" tabindex="-1"></a> <span class="dt">step</span><span class="op">:</span> <span class="dv">1</span><span class="op">,</span> <span class="dt">label</span><span class="op">:</span> <span class="st">"End year:"</span></span>
<span id="cb8-109"><a href="#cb8-109" aria-hidden="true" tabindex="-1"></a> }</span>
<span id="cb8-110"><a href="#cb8-110" aria-hidden="true" tabindex="-1"></a>)</span>
<span id="cb8-111"><a href="#cb8-111" aria-hidden="true" tabindex="-1"></a>viewof stat_std <span class="op">=</span> Inputs<span class="op">.</span><span class="fu">radio</span>(</span>
<span id="cb8-112"><a href="#cb8-112" aria-hidden="true" tabindex="-1"></a> [<span class="st">"mean"</span><span class="op">,</span> <span class="st">"max"</span><span class="op">,</span> <span class="st">"min"</span>]<span class="op">,</span> </span>
<span id="cb8-113"><a href="#cb8-113" aria-hidden="true" tabindex="-1"></a> { <span class="dt">value</span><span class="op">:</span> <span class="st">"mean"</span><span class="op">,</span> </span>
<span id="cb8-114"><a href="#cb8-114" aria-hidden="true" tabindex="-1"></a> <span class="dt">label</span><span class="op">:</span> <span class="st">"Statistic:"</span></span>
<span id="cb8-115"><a href="#cb8-115" aria-hidden="true" tabindex="-1"></a> }</span>
<span id="cb8-116"><a href="#cb8-116" aria-hidden="true" tabindex="-1"></a>)</span>
<span id="cb8-117"><a href="#cb8-117" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb8-118"><a href="#cb8-118" aria-hidden="true" tabindex="-1"></a><span class="co">//filter the data read from file</span></span>
<span id="cb8-119"><a href="#cb8-119" aria-hidden="true" tabindex="-1"></a>filtered_std <span class="op">=</span> precipitations_std<span class="op">.</span><span class="fu">filter</span>(<span class="kw">function</span>(precipitations_std) {</span>
<span id="cb8-120"><a href="#cb8-120" aria-hidden="true" tabindex="-1"></a> <span class="kw">let</span> years <span class="op">=</span> <span class="kw">new</span> <span class="bu">Date</span>(precipitations_std<span class="op">.</span><span class="at">dates</span>)<span class="op">.</span><span class="fu">getYear</span>() <span class="op">+</span> <span class="dv">1900</span><span class="op">;</span></span>
<span id="cb8-121"><a href="#cb8-121" aria-hidden="true" tabindex="-1"></a> <span class="cf">return</span> (years <span class="op">>=</span> beginYear_std) <span class="op">&&</span> (years <span class="op"><=</span> endYear_std)</span>
<span id="cb8-122"><a href="#cb8-122" aria-hidden="true" tabindex="-1"></a>})</span>
<span id="cb8-123"><a href="#cb8-123" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb8-124"><a href="#cb8-124" aria-hidden="true" tabindex="-1"></a><span class="co">// use the filtered data </span></span>
<span id="cb8-125"><a href="#cb8-125" aria-hidden="true" tabindex="-1"></a>Plot<span class="op">.</span><span class="fu">plot</span>({</span>
<span id="cb8-126"><a href="#cb8-126" aria-hidden="true" tabindex="-1"></a> <span class="dt">width</span><span class="op">:</span> <span class="dv">800</span><span class="op">,</span> <span class="dt">height</span><span class="op">:</span> <span class="dv">500</span><span class="op">,</span> <span class="dt">padding</span><span class="op">:</span> <span class="dv">0</span><span class="op">,</span></span>
<span id="cb8-127"><a href="#cb8-127" aria-hidden="true" tabindex="-1"></a> <span class="dt">color</span><span class="op">:</span> { <span class="dt">scheme</span><span class="op">:</span> <span class="st">"blues"</span><span class="op">,</span> <span class="dt">type</span><span class="op">:</span> <span class="st">"sqrt"</span>}<span class="op">,</span></span>
<span id="cb8-128"><a href="#cb8-128" aria-hidden="true" tabindex="-1"></a> <span class="dt">y</span><span class="op">:</span> { <span class="dt">tickFormat</span><span class="op">:</span> i <span class="kw">=></span> <span class="st">"JFMAMJJASOND"</span>[i] }<span class="op">,</span></span>
<span id="cb8-129"><a href="#cb8-129" aria-hidden="true" tabindex="-1"></a> <span class="dt">marks</span><span class="op">:</span> [</span>
<span id="cb8-130"><a href="#cb8-130" aria-hidden="true" tabindex="-1"></a> Plot<span class="op">.</span><span class="fu">cell</span>(filtered_std<span class="op">,</span> Plot<span class="op">.</span><span class="fu">group</span>({<span class="dt">fill</span><span class="op">:</span> stat_std}<span class="op">,</span> {</span>
<span id="cb8-131"><a href="#cb8-131" aria-hidden="true" tabindex="-1"></a> <span class="dt">x</span><span class="op">:</span> d <span class="kw">=></span> <span class="kw">new</span> <span class="bu">Date</span>(d<span class="op">.</span><span class="at">dates</span>)<span class="op">.</span><span class="fu">getDate</span>()<span class="op">,</span></span>
<span id="cb8-132"><a href="#cb8-132" aria-hidden="true" tabindex="-1"></a> <span class="dt">y</span><span class="op">:</span> d <span class="kw">=></span> <span class="kw">new</span> <span class="bu">Date</span>(d<span class="op">.</span><span class="at">dates</span>)<span class="op">.</span><span class="fu">getMonth</span>()<span class="op">,</span></span>
<span id="cb8-133"><a href="#cb8-133" aria-hidden="true" tabindex="-1"></a> <span class="dt">fill</span><span class="op">:</span> <span class="st">"precipitation_std"</span><span class="op">,</span> </span>
<span id="cb8-134"><a href="#cb8-134" aria-hidden="true" tabindex="-1"></a> <span class="dt">inset</span><span class="op">:</span> <span class="fl">0.5</span></span>
<span id="cb8-135"><a href="#cb8-135" aria-hidden="true" tabindex="-1"></a> }))</span>
<span id="cb8-136"><a href="#cb8-136" aria-hidden="true" tabindex="-1"></a> ]</span>
<span id="cb8-137"><a href="#cb8-137" aria-hidden="true" tabindex="-1"></a>})</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
</details>
<div class="cell-output cell-output-display">
<div>
<div id="ojs-cell-2-1" data-nodetype="declaration">
</div>
</div>
</div>
<div class="cell-output cell-output-display">
<div>
<div id="ojs-cell-2-2" data-nodetype="declaration">
</div>
</div>
</div>
<div class="cell-output cell-output-display">
<div>
<div id="ojs-cell-2-3" data-nodetype="declaration">
</div>
</div>
</div>
<div class="cell-output cell-output-display">
<div>
<div id="ojs-cell-2-4" data-nodetype="declaration">
</div>
</div>
</div>
<div class="cell-output cell-output-display">
<div>
<div id="ojs-cell-2-5" data-nodetype="declaration">
</div>
</div>
</div>
<div class="cell-output cell-output-display">
<div>
<div id="ojs-cell-2-6" data-nodetype="expression">
</div>
</div>
</div>
</div>
</section>
<section id="conclusion" class="level2">
<h2 class="anchored" data-anchor-id="conclusion">Conclusion</h2>
<p>The interactivity brings up questions about the data set and the operations carried out on the data to achieve the visualization itself. Z-score standarization was used to normalized the precipitation over th range of years of data before filtering it to investigate the effect of the passage of time. Min-max normalization could have been investigated as well. The effect of this transformation is not too dramatic but the range of time in this data set may not be sufficiently long to observe large contrast.</p>
<p>Interactivity can be useful to express a time series over a complex time plane to study seasonality effects.</p>
</section>
</main>
<!-- /main column -->
<script type="ojs-module-contents">
{"contents":[{"methodName":"interpret","cellName":"ojs-cell-1","inline":false,"source":"precipitations = FileAttachment(\"data/seatle-data.csv\")\n .csv({typed: true})\n\n//create views\nviewof beginYear = Inputs.range(\n [2012, 2023], \n {value: 2012, step: 1, label: \"Begin year:\"}\n)\nviewof endYear = Inputs.range(\n [beginYear, 2023], \n { value: [beginYear], \n step: 1, label: \"End year:\"\n }\n)\nviewof stat = Inputs.radio(\n [\"mean\", \"max\", \"min\"], \n { value: \"mean\", \n label: \"Statistic:\"\n }\n)\n\n//filter the data read from file\nfiltered = precipitations.filter(function(precipitations) {\n let years = new Date(precipitations.dates).getYear() + 1900;\n return (years >= beginYear) && (years <= endYear)\n})\n\n// use the filtered data \nPlot.plot({\n width: 800, height: 500, padding: 0,\n color: { scheme: \"blues\", type: \"sqrt\"},\n y: { tickFormat: i => \"JFMAMJJASOND\"[i] },\n marks: [\n Plot.cell(filtered, Plot.group({fill: stat}, {\n x: d => new Date(d.dates).getDate(),\n y: d => new Date(d.dates).getMonth(),\n fill: \"precipitation\", \n inset: 0.5\n }))\n ]\n})\n"},{"methodName":"interpret","cellName":"ojs-cell-2","inline":false,"source":"precipitations_std = FileAttachment(\"data/seatle-data-std.csv\")\n .csv({typed: true})\n\n//create views\nviewof beginYear_std = Inputs.range(\n [2012, 2023], \n {value: 2012, step: 1, label: \"Begin year:\"}\n)\nviewof endYear_std = Inputs.range(\n [beginYear, 2023], \n { value: [beginYear], \n step: 1, label: \"End year:\"\n }\n)\nviewof stat_std = Inputs.radio(\n [\"mean\", \"max\", \"min\"], \n { value: \"mean\", \n label: \"Statistic:\"\n }\n)\n\n//filter the data read from file\nfiltered_std = precipitations_std.filter(function(precipitations_std) {\n let years = new Date(precipitations_std.dates).getYear() + 1900;\n return (years >= beginYear_std) && (years <= endYear_std)\n})\n\n// use the filtered data \nPlot.plot({\n width: 800, height: 500, padding: 0,\n color: { scheme: \"blues\", type: \"sqrt\"},\n y: { tickFormat: i => \"JFMAMJJASOND\"[i] },\n marks: [\n Plot.cell(filtered_std, Plot.group({fill: stat_std}, {\n x: d => new Date(d.dates).getDate(),\n y: d => new Date(d.dates).getMonth(),\n fill: \"precipitation_std\", \n inset: 0.5\n }))\n ]\n})\n"},{"methodName":"interpretQuiet","source":"shinyInput('beginYear')"},{"methodName":"interpretQuiet","source":"shinyInput('endYear')"},{"methodName":"interpretQuiet","source":"shinyInput('stat')"},{"methodName":"interpretQuiet","source":"shinyInput('beginYear_std')"},{"methodName":"interpretQuiet","source":"shinyInput('endYear_std')"},{"methodName":"interpretQuiet","source":"shinyInput('stat_std')"}]}
</script>
<script type="module">
if (window.location.protocol === "file:") { alert("The OJS runtime does not work with file:// URLs. Please use a web server to view this document."); }
window._ojs.paths.runtimeToDoc = "../../..";
window._ojs.paths.runtimeToRoot = "../../..";
window._ojs.paths.docToRoot = "";
window._ojs.selfContained = false;
window._ojs.runtime.interpretFromScriptTags();
</script>
<script id="quarto-html-after-body" type="application/javascript">
window.document.addEventListener("DOMContentLoaded", function (event) {
const toggleBodyColorMode = (bsSheetEl) => {
const mode = bsSheetEl.getAttribute("data-mode");
const bodyEl = window.document.querySelector("body");
if (mode === "dark") {
bodyEl.classList.add("quarto-dark");
bodyEl.classList.remove("quarto-light");
} else {
bodyEl.classList.add("quarto-light");
bodyEl.classList.remove("quarto-dark");
}
}
const toggleBodyColorPrimary = () => {
const bsSheetEl = window.document.querySelector("link#quarto-bootstrap");
if (bsSheetEl) {
toggleBodyColorMode(bsSheetEl);
}
}
toggleBodyColorPrimary();
const icon = "";
const anchorJS = new window.AnchorJS();
anchorJS.options = {
placement: 'right',
icon: icon
};
anchorJS.add('.anchored');
const isCodeAnnotation = (el) => {
for (const clz of el.classList) {
if (clz.startsWith('code-annotation-')) {
return true;
}
}
return false;
}
const clipboard = new window.ClipboardJS('.code-copy-button', {
text: function(trigger) {
const codeEl = trigger.previousElementSibling.cloneNode(true);
for (const childEl of codeEl.children) {
if (isCodeAnnotation(childEl)) {
childEl.remove();
}
}
return codeEl.innerText;
}
});
clipboard.on('success', function(e) {
// button target
const button = e.trigger;
// don't keep focus
button.blur();
// flash "checked"
button.classList.add('code-copy-button-checked');
var currentTitle = button.getAttribute("title");
button.setAttribute("title", "Copied!");
let tooltip;
if (window.bootstrap) {
button.setAttribute("data-bs-toggle", "tooltip");
button.setAttribute("data-bs-placement", "left");
button.setAttribute("data-bs-title", "Copied!");
tooltip = new bootstrap.Tooltip(button,
{ trigger: "manual",
customClass: "code-copy-button-tooltip",
offset: [0, -8]});
tooltip.show();
}
setTimeout(function() {
if (tooltip) {
tooltip.hide();
button.removeAttribute("data-bs-title");
button.removeAttribute("data-bs-toggle");
button.removeAttribute("data-bs-placement");
}
button.setAttribute("title", currentTitle);
button.classList.remove('code-copy-button-checked');
}, 1000);
// clear code selection
e.clearSelection();
});
function tippyHover(el, contentFn) {
const config = {
allowHTML: true,
content: contentFn,
maxWidth: 500,
delay: 100,
arrow: false,
appendTo: function(el) {
return el.parentElement;
},
interactive: true,
interactiveBorder: 10,
theme: 'quarto',
placement: 'bottom-start'
};
window.tippy(el, config);
}
const noterefs = window.document.querySelectorAll('a[role="doc-noteref"]');
for (var i=0; i<noterefs.length; i++) {
const ref = noterefs[i];
tippyHover(ref, function() {
// use id or data attribute instead here
let href = ref.getAttribute('data-footnote-href') || ref.getAttribute('href');
try { href = new URL(href).hash; } catch {}
const id = href.replace(/^#\/?/, "");
const note = window.document.getElementById(id);
return note.innerHTML;
});
}
let selectedAnnoteEl;
const selectorForAnnotation = ( cell, annotation) => {
let cellAttr = 'data-code-cell="' + cell + '"';
let lineAttr = 'data-code-annotation="' + annotation + '"';
const selector = 'span[' + cellAttr + '][' + lineAttr + ']';
return selector;
}
const selectCodeLines = (annoteEl) => {
const doc = window.document;
const targetCell = annoteEl.getAttribute("data-target-cell");
const targetAnnotation = annoteEl.getAttribute("data-target-annotation");
const annoteSpan = window.document.querySelector(selectorForAnnotation(targetCell, targetAnnotation));
const lines = annoteSpan.getAttribute("data-code-lines").split(",");
const lineIds = lines.map((line) => {
return targetCell + "-" + line;
})
let top = null;
let height = null;
let parent = null;
if (lineIds.length > 0) {
//compute the position of the single el (top and bottom and make a div)
const el = window.document.getElementById(lineIds[0]);
top = el.offsetTop;
height = el.offsetHeight;
parent = el.parentElement.parentElement;
if (lineIds.length > 1) {
const lastEl = window.document.getElementById(lineIds[lineIds.length - 1]);
const bottom = lastEl.offsetTop + lastEl.offsetHeight;
height = bottom - top;
}
if (top !== null && height !== null && parent !== null) {
// cook up a div (if necessary) and position it
let div = window.document.getElementById("code-annotation-line-highlight");
if (div === null) {
div = window.document.createElement("div");
div.setAttribute("id", "code-annotation-line-highlight");
div.style.position = 'absolute';
parent.appendChild(div);
}
div.style.top = top - 2 + "px";
div.style.height = height + 4 + "px";
let gutterDiv = window.document.getElementById("code-annotation-line-highlight-gutter");
if (gutterDiv === null) {
gutterDiv = window.document.createElement("div");
gutterDiv.setAttribute("id", "code-annotation-line-highlight-gutter");
gutterDiv.style.position = 'absolute';
const codeCell = window.document.getElementById(targetCell);
const gutter = codeCell.querySelector('.code-annotation-gutter');
gutter.appendChild(gutterDiv);
}
gutterDiv.style.top = top - 2 + "px";
gutterDiv.style.height = height + 4 + "px";
}
selectedAnnoteEl = annoteEl;
}
};
const unselectCodeLines = () => {
const elementsIds = ["code-annotation-line-highlight", "code-annotation-line-highlight-gutter"];
elementsIds.forEach((elId) => {
const div = window.document.getElementById(elId);
if (div) {
div.remove();
}
});
selectedAnnoteEl = undefined;
};
// Attach click handler to the DT
const annoteDls = window.document.querySelectorAll('dt[data-target-cell]');
for (const annoteDlNode of annoteDls) {
annoteDlNode.addEventListener('click', (event) => {
const clickedEl = event.target;
if (clickedEl !== selectedAnnoteEl) {
unselectCodeLines();
const activeEl = window.document.querySelector('dt[data-target-cell].code-annotation-active');
if (activeEl) {
activeEl.classList.remove('code-annotation-active');
}
selectCodeLines(clickedEl);
clickedEl.classList.add('code-annotation-active');
} else {
// Unselect the line
unselectCodeLines();
clickedEl.classList.remove('code-annotation-active');
}
});
}
const findCites = (el) => {
const parentEl = el.parentElement;
if (parentEl) {
const cites = parentEl.dataset.cites;
if (cites) {
return {
el,
cites: cites.split(' ')
};
} else {
return findCites(el.parentElement)
}
} else {
return undefined;
}
};
var bibliorefs = window.document.querySelectorAll('a[role="doc-biblioref"]');
for (var i=0; i<bibliorefs.length; i++) {
const ref = bibliorefs[i];
const citeInfo = findCites(ref);
if (citeInfo) {
tippyHover(citeInfo.el, function() {
var popup = window.document.createElement('div');
citeInfo.cites.forEach(function(cite) {
var citeDiv = window.document.createElement('div');
citeDiv.classList.add('hanging-indent');
citeDiv.classList.add('csl-entry');
var biblioDiv = window.document.getElementById('ref-' + cite);
if (biblioDiv) {
citeDiv.innerHTML = biblioDiv.innerHTML;
}
popup.appendChild(citeDiv);
});
return popup.innerHTML;
});
}
}
});
</script>
</div> <!-- /content -->
</body></html>