|
267 | 267 | "body_mass_g" "species island") |
268 | 268 | rotate-spec) |
269 | 269 |
|
270 | | -;;; # TODO And finally put all the pieces together. |
271 | | - |
272 | | -;;; not working yet, violin plot won't layer with other ones |
273 | | - |
274 | | - |
275 | | -(defn everything-plot |
276 | | - [data value-field group-field] |
277 | | - { |
278 | | - ;; Data is in common |
279 | | - :data data |
280 | | - :config {:facet {:spacing 0} |
281 | | - :view {:stroke nil}} |
282 | | - :facet |
283 | | - {:row {:field group-field |
284 | | - :type :nominal |
285 | | - :header {:labelAngle 0 |
286 | | - :labelAlign "left" }}} |
287 | | - :spec |
288 | | - {:layer |
289 | | - [(dot-plot-g nil value-field group-field |
290 | | - :jitter? true) |
291 | | - (box-plot-g nil value-field group-field |
292 | | - :overrides {:mark {:box {:strokeWidth 1.5 :stroke "gray"}}}) |
293 | | - (violin-plot nil value-field group-field) |
294 | | - ] |
295 | | - :width 800} |
296 | | - }) |
297 | | - |
298 | | -;; ^:kind/vega-lite |
299 | | -;; (-> (everything-plot {:url penguin-data-url |
300 | | -;; :format {:type "tsv"}} |
301 | | -;; "body_mass_g" "species island") |
302 | | -;; #_ rotate-spec) |
303 | | - |
304 | | -;;; well that doesn't work |
305 | | - |
| 270 | +;;; # And finally put all the pieces together. |
306 | 271 |
|
| 272 | +;;; What we'd like to do is make something like box-dot-plot above, but with a 3rd layer – the violins – generated by its own fn. Unfortunately that doesn't quite work due to some technical issues. So we'll expand everything out for the grand finale, which also makes it a bit easier to tweak things so they fit together better. |
307 | 273 |
|
308 | 274 |
|
309 | 275 | ^:kind/vega-lite |
|
317 | 283 | :spec |
318 | 284 | {:height 50 |
319 | 285 | :width 800 |
320 | | - :resolve {:scale {:y "independent"}} |
| 286 | + :resolve {:scale {:y "independent"}} ;each layer uses the y axis differently |
| 287 | + :encoding |
| 288 | + {:x {:field "body_mass_g" |
| 289 | + :type :quantitative |
| 290 | + :title "Body Mass (g)"}} |
321 | 291 | :layer |
322 | 292 | [ |
323 | 293 | ;; Violins |
324 | 294 | {:mark {:type :area :orient :vertical} |
325 | 295 | :transform [{:density "body_mass_g" |
326 | 296 | :groupby ["species island"] |
327 | | - :bandwidth 80 |
328 | | - :extent [2200 6800]}] |
| 297 | + :bandwidth 80}] |
329 | 298 | :encoding |
330 | | - {:color {:field "species island" :type :nominal :legend false} |
331 | | - :x {:field "value" :type :quantitative :title "body_mass_g"} |
332 | | - :y {:field "density" :type :quantitative :stack :center :axis false} |
| 299 | + {:color {:field "species island" |
| 300 | + :type :nominal |
| 301 | + :legend false} |
| 302 | + :x {:field "value" |
| 303 | + :type :quantitative} |
| 304 | + :y {:field "density" |
| 305 | + :type :quantitative |
| 306 | + :stack :center |
| 307 | + :axis false |
| 308 | + :scale {:range [0 50]}} |
333 | 309 | :opacity {:value 0.7} |
334 | | - } |
335 | | - } |
| 310 | + }} |
336 | 311 |
|
337 | 312 | ;; Points |
338 | 313 | {:mark {:type "point" :tooltip {:content :data}} |
339 | 314 | :transform [{:calculate "random()" :as "jitter"}] |
340 | 315 | :encoding |
341 | | - {:x {:field "body_mass_g" :type :quantitative :scale {:zero false}} |
342 | | - :y {:field "jitter" :type :quantitative :axis false} |
343 | | - } |
344 | | - } |
| 316 | + {:y {:field "jitter" |
| 317 | + :type :quantitative |
| 318 | + :axis false} |
| 319 | + }} |
345 | 320 |
|
346 | 321 | ;; Box |
347 | 322 | {:mark |
348 | 323 | {:type :boxplot |
349 | 324 | :tooltip {:content :data} |
350 | | - :extent :min-max |
351 | 325 | :box {:strokeWidth 1.5 :stroke "gray"}} |
352 | 326 | :transform [{:calculate "random()" :as "jitter"}] |
353 | 327 | :encoding |
354 | | - {:x {:field "body_mass_g" |
355 | | - :type :quantitative |
356 | | - :scale {:zero false}} |
357 | | - :fill {:value "none"} |
| 328 | + {:fill {:value "none"} |
358 | 329 | :stroke {:value "black"} |
359 | | - } |
360 | | - |
361 | | - } |
362 | | - ] |
363 | | - }} |
364 | | - |
365 | | - |
| 330 | + }} |
| 331 | + ] }} |
366 | 332 |
|
367 | | -;;; that's all folks |
| 333 | +;;; There you have it, the datapoints themselves, the coarse statistics, and the probability density, all in one place so you can see how they relate. |
0 commit comments