-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathvega-prez.html
290 lines (258 loc) · 15.5 KB
/
vega-prez.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
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Vega, Vega-Lite & Altair</title>
<meta name="description" content="Vega, Vega-Lite & Altair">
<!-- Disabled for now - embeds HTML in author string -->
<!-- <meta name="author" content="OH MY!" /> -->
<meta name="apple-mobile-web-app-capable" content="yes" />
<meta name="apple-mobile-web-app-status-bar-style" content="black-translucent" />
<meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no">
<link rel="stylesheet" href="reveal.js/css/reveal.css">
<link rel="stylesheet" href="src/theme/csmall.css" id="theme">
<!-- For syntax highlighting -->
<link rel="stylesheet" href="reveal.js/lib/css/zenburn.css">
<!-- If the query includes 'print-pdf', use the PDF print sheet -->
<script>
document.write( '<link rel="stylesheet" href="reveal.js/css/print/' +
( window.location.search.match( /print-pdf/gi ) ? 'pdf' : 'paper' ) +
'.css" type="text/css" media="print">' );
</script>
<!--[if lt IE 9]>
<script src="reveal.js/lib/js/html5shiv.js"></script>
<![endif]-->
<!-- Note escaped dollars -->
<script type="text/x-mathjax-config">
MathJax.Hub.Config({
tex2jax: {inlineMath: [['$','$'], ['\\(','\\)']]}
});
</script>
</head>
<body>
<div class="reveal">
<!-- Any section element inside of this container is displayed as a slide -->
<div class="slides">
<section>
<h1>Vega, Vega-Lite & Altair</h1>
<h3>OH MY!</h3>
<p>
<h4></h4>
</p>
</section>
<section id="outline" class="level1">
<h1>Outline</h1>
<ul>
<li>Vega</li>
<li>Vega-Lite</li>
<li>Altair</li>
<li>Other ecosystem things</li>
<li>Opinions</li>
</ul>
</section>
<section id="grammar-of-graphics" class="level1">
<h1>Grammar of Graphics</h1>
<p>Declarative data visualizations</p>
<ul>
<li>how do you map properties of data to visual aesthetics?</li>
<li>focus on the guts of the problem over implementation details</li>
<li>very successful: influenced ggplot2, d3, etc</li>
</ul>
</section>
<section id="vega" class="level1">
<h1>Vega</h1>
<p>Grammar of Graphics as a data specification</p>
<ul>
<li>declarative to the max</li>
<li>built on (compiles to) d3.js</li>
<li>extremely powerful/configurable</li>
<li>interactive features, dataflow, etc</li>
<li>not suitable for day to day visualization tasks by itself (lots of boilerplate)</li>
<li>great compile-to target</li>
</ul>
</section>
<section id="example-vega" class="level1">
<h1>Example Vega</h1>
<pre><code>{"$schema":"https://vega.github.io/schema/vega/v3.0.json",
"autosize":"pad",
"padding":5,
"width":200,
"height":200,
"style":"cell",
"data":[{"name":"source_0","url":"https://vega.github.io/editor/data/cars.json",
"format":{"type":"json","parse":{"Horsepower":"number","Miles_per_Gallon":"number"}},
"transform":[{"type":"filter","expr":"datum[\"Horsepower\"] !== null && !isNaN(datum[\"Horsepower\"]) && datum[\"Miles_per_Gallon\"] !== null && !isNaN(datum[\"Miles_per_Gallon\"])"}]}],
"marks":[{"name":"marks",
"type":"symbol",
"style":["point"],
"from":{"data":"source_0"},
"encode":{"update": {"opacity":{"value":0.7},"fill":{"value":"transparent"},"stroke":{"scale":"color","field":"Origin"},"x":{"scale":"x","field":"Horsepower"},"y":{"scale":"y","field":"Miles_per_Gallon"}}}}],
"scales":[{"name":"x","type":"linear","domain":{"data":"source_0","field":"Horsepower"},"range":[0,{"signal":"width"}],"nice":true,"zero":true},
{"name":"y","type":"linear","domain":{"data":"source_0","field":"Miles_per_Gallon"},"range":[{"signal":"height"},0],"nice":true,"zero":true},
{"name":"color","type":"ordinal","domain":{"data":"source_0","field":"Origin","sort":true},"range":"category"}],
"axes":[{"scale":"x","orient":"bottom","title":"Horsepower","labelFlush":true,"labelOverlap":true,"tickCount":{"signal":"ceil(width/40)"},"zindex":1},
{"scale":"x","orient":"bottom","grid":true,"tickCount":{"signal":"ceil(width/40)"},"gridScale":"y","domain":false,"labels":false,"maxExtent":0,"minExtent":0,"ticks":false,"zindex":0},
{"scale":"y","orient":"left","title":"Miles_per_Gallon","labelOverlap":true,"tickCount":{"signal":"ceil(height/40)"},"zindex":1},{"scale":"y","orient":"left","grid":true,"tickCount":{"signal":"ceil(height/40)"},"gridScale":"x","domain":false,"labels":false,"maxExtent":0,"minExtent":0,"ticks":false,"zindex":0}],
"legends":[{"stroke":"color","title":"Origin","encode":{"symbols":{"update":{"opacity":{"value":0.7}}}}}],
"config":{"axisY":{"minExtent":30}}}</code></pre>
</section>
<section id="vega-lite" class="level1">
<h1>Vega-Lite</h1>
<p>Higher-level manifestation of Vega for day to day viz</p>
<ul>
<li>both restricts and extends vega with focus on productivity over control (do more with less)</li>
<li>compiles to Vega</li>
<li>Vega with sane defaults (style guide?)</li>
<li>early Vega-Lite was a bit limited, but v2 is phenomenally composable</li>
</ul>
</section>
<section id="example-vega-lite" class="level1">
<h1>Example Vega-Lite</h1>
<div class="sourceCode"><pre class="sourceCode json"><code class="sourceCode json"><span class="fu">{</span><span class="dt">"data"</span><span class="fu">:</span> <span class="fu">{</span><span class="dt">"url"</span><span class="fu">:</span> <span class="st">"https://vega.github.io/editor/data/cars.json"</span><span class="fu">},</span>
<span class="dt">"mark"</span><span class="fu">:</span> <span class="st">"point"</span><span class="fu">,</span>
<span class="dt">"encoding"</span><span class="fu">:</span> <span class="fu">{</span><span class="dt">"x"</span><span class="fu">:</span> <span class="fu">{</span><span class="dt">"field"</span><span class="fu">:</span> <span class="st">"Horsepower"</span><span class="fu">},</span>
<span class="dt">"y"</span><span class="fu">:</span> <span class="fu">{</span><span class="dt">"field"</span><span class="fu">:</span> <span class="st">"Miles_per_Gallon"</span><span class="fu">},</span>
<span class="dt">"color"</span><span class="fu">:</span> <span class="fu">{</span><span class="dt">"field"</span><span class="fu">:</span> <span class="st">"Origin"</span><span class="fu">}}}</span></code></pre></div>
</section>
<section id="json-ftw" class="level1">
<h1>JSON FTW</h1>
<p>Vega and Vega-Lite are just data</p>
<ul>
<li>can be created from any language with a natural interpretation of JSON (dictionaries, lists, numbers, strings, boolean)</li>
<li>can be easily passed between programs or written to disk</li>
<li>synergy!</li>
</ul>
</section>
<section id="server-side-rendering" class="level1">
<h1>Server side rendering</h1>
<p>The only way to compile Vega-Lite or Vega to SVG/PNG is with JS</p>
<ul>
<li>fine if you are targeting the browser (see <a href="https://github.com/vega/vega-embed">vega-embed</a>)</li>
<li>for generating SVG/PNG programatically, need a node (“server-side JS”) environment</li>
</ul>
<p>Thankfully SciComp has set some modules up for us to use this</p>
<div class="sourceCode"><pre class="sourceCode bash"><code class="sourceCode bash"><span class="ex">ml</span> nodejs-bundle</code></pre></div>
</section>
<section id="basic-cli-usage" class="level1">
<h1>Basic CLI usage</h1>
<p>Installing the vega/vega-lite node modules (and deps) gives you the following executables</p>
<div class="sourceCode"><pre class="sourceCode bash"><code class="sourceCode bash"><span class="ex">vl2svg</span> carviz.json
<span class="ex">vl2png</span> carviz.json
<span class="ex">vl2vg</span> carviz.json</code></pre></div>
</section>
<section id="altair" class="level1">
<h1>Altair</h1>
<p>OOP Python wrapper around Vega-Lite</p>
<ul>
<li>validation against Vega-Lite specs, for help debugging</li>
<li>adds constructors and conventions for improving concision, by obscuring some of vega-lite (pros/cons)</li>
<li>requires python3 and pandas; (me: kicking and screaming)</li>
</ul>
<pre><code>ml Python/3.6.4-foss-2016b-fh2</code></pre>
</section>
<section id="altair-example" class="level1">
<h1>Altair example</h1>
<div class="sourceCode"><pre class="sourceCode python"><code class="sourceCode python"><span class="im">import</span> altair <span class="im">as</span> alt
<span class="im">from</span> vega_datasets <span class="im">import</span> data
<span class="co"># data must be a data frame; for convenience, here the vega_datasets data</span>
data <span class="op">=</span> data.cars()
<span class="co"># Set mark type and encodings</span>
c <span class="op">=</span> alt.Chart(data).mark_point().encode(
x<span class="op">=</span><span class="st">'Horsepower:Q'</span>,
y<span class="op">=</span><span class="st">'Miles_per_Gallon:Q'</span>,
color<span class="op">=</span><span class="st">'Origin:N'</span>)</code></pre></div>
</section>
<section id="altair-to-vega-lite" class="level1">
<h1>Altair to Vega-Lite</h1>
<div class="sourceCode"><pre class="sourceCode python"><code class="sourceCode python"><span class="co"># ↓ This is all Altair really is...</span>
c.to_dict()
{<span class="st">'$schema'</span>: <span class="st">'https://vega.github.io/schema/vega-lite/v2.json'</span>,
<span class="st">'data'</span>: {<span class="st">'values'</span>: ...},
<span class="st">'config'</span>: {<span class="st">'view'</span>: {<span class="st">'height'</span>: <span class="dv">300</span>, <span class="st">'width'</span>: <span class="dv">400</span>}},
<span class="st">'mark'</span>: <span class="st">'point'</span>,
<span class="st">'encoding'</span>: {<span class="st">'color'</span>: {<span class="st">'field'</span>: <span class="st">'Origin'</span>, <span class="st">'type'</span>: <span class="st">'nominal'</span>},
<span class="st">'x'</span>: {<span class="st">'field'</span>: <span class="st">'Horsepower'</span>,
<span class="st">'type'</span>: <span class="st">'quantitative'</span>},
<span class="st">'y'</span>: {<span class="st">'field'</span>: <span class="st">'Miles_per_Gallon'</span>,
<span class="st">'type'</span>: <span class="st">'quantitative'</span>}}}</code></pre></div>
</section>
<section id="vega-lite-to-altair" class="level1">
<h1>Vega-Lite to Altair</h1>
<p>You can also do this</p>
<pre><code>import json
# Import from vega
with open("carviz.json") as json_handle:
vl_data = json.load(json_handle.read())
c = alt.from_dict(vl_data)</code></pre>
</section>
<section id="things-i-dont-like-about-altair" class="level1">
<h1>Things I don’t like about Altair</h1>
<ul>
<li>py3k</li>
<li>pandas</li>
<li>weird indentation</li>
<li>obfuscates data model, challenging more intricate setups</li>
<li>no vega</li>
</ul>
</section>
<section id="server-side-without-altair" class="level1">
<h1>Server side without altair</h1>
<pre><code>import subprocess, json
c = {'data': ...,
'mark': ...,
'encoding': ...}
with open('my-vega-lite.json', 'w') as json_handle:
json.dump(c, json_handle)
subprocess.call(['vl2svg', 'my-vega-lite.json', 'rendered.svg'])</code></pre>
<p>I’ll probably release a snappy library with helpers along these lines…</p>
</section>
<section id="other-ecosystem-things" class="level1">
<h1>Other ecosystem things</h1>
<ul>
<li><a href="http://vega.github.io/polestar/">polestar</a> - Tableau like browser tool based on vega-lite
<ul>
<li>exports as json</li>
</ul></li>
<li><a href="https://vega.github.io/editor/">vega-editor</a> - Live renders Vega/Vega-Lite code as you type
<ul>
<li>enables gist-based sharing/editing/colaboration</li>
</ul></li>
<li>more examples of <a href="https://vega.github.io/vega/examples/">Vega</a> and <a href="https://vega.github.io/vega-lite/examples/">Vega-Lite</a></li>
<li><a href="https://github.com/Zsailer/phylovega">phylovega</a> - vega-lite for phylogenetic trees!?</li>
</ul>
</section>
</div>
<script src="reveal.js/lib/js/head.min.js"></script>
<script src="reveal.js/js/reveal.min.js"></script>
<script>
// Full list of configuration options available here:
// https://github.com/hakimel/reveal.js#configuration
Reveal.initialize({
controls: true,
progress: true,
history: true,
center: false,
slideNumber: true,
rollingLinks: false,
// available themes are in /css/theme
theme: Reveal.getQueryHash().theme || 'csmall',
// default/cube/page/concave/zoom/linear/fade/none
transition: Reveal.getQueryHash().transition || 'fade',
// Optional libraries used to extend on reveal.js
dependencies: [
{ src: 'reveal.js/lib/js/classList.js', condition: function() { return !document.body.classList; } },
{ src: 'reveal.js/plugin/markdown/showdown.js', condition: function() { return !!document.querySelector( '[data-markdown]' ); } },
{ src: 'reveal.js/plugin/markdown/markdown.js', condition: function() { return !!document.querySelector( '[data-markdown]' ); } },
{ src: 'reveal.js/plugin/highlight/highlight.js', async: true, callback: function() { hljs.initHighlightingOnLoad(); } },
{ src: 'reveal.js/plugin/zoom-js/zoom.js', async: true, condition: function() { return !!document.body.classList; } },
{ src: 'MathJax/MathJax.js?config=TeX-AMS-MML_HTMLorMML'}
// { src: 'reveal.js/plugin/notes/notes.js', async: true, condition: function() { return !!document.body.classList; } }
// { src: 'reveal.js/plugin/remotes/remotes.js', async: true, condition: function() { return !!document.body.classList; } }
]
});
Reveal.addEventListener( 'slidechanged', function( event ) {
MathJax.Hub.Rerender();
} );
</script>
</body>
</html>