9 Visualization (experimental 🛠)
author: Daniel Slutsky
ns noj-book.visualization
(:require [aerial.hanami.templates :as ht]
(:as datasets]
[noj-book.datasets :as kind]
[scicloj.kindly.v4.kind :as vis.hanami]
[scicloj.noj.v1.vis.hanami :as tc])) [tablecloth.api
9.1 Visualizing datases with Hanami
Noj offers a few convenience functions to make Hanami plotting work smoothly with Tablecloth and Kindly.
def random-walk
(let [n 20]
(-> {:x (range n)
(:y (->> (repeatedly n #(- (rand) 0.5))
+))}
(reductions tc/dataset)))
9.1.1 A simple plot
We can plot a Tablecloth datasete using a Hanami template:
-> random-walk
(
(vis.hanami/plot ht/point-chart:MSIZE 200})) {
Let us look inside the resulting vega-lite space. We can see the dataset is included as CSV:
-> random-walk
(
(vis.hanami/plot ht/point-chart:MSIZE 200})
{ kind/pprint)
:encoding
{:y {:field "y", :type "quantitative"},
{:x {:field "x", :type "quantitative"}},
:usermeta {:embedOptions {:renderer :svg}},
:mark {:type "circle", :size 200, :tooltip true},
:width 400,
:background "floralwhite",
:height 300,
:data
:values
{"x,y\n0,-0.43189740812782884\n1,-0.17599285192851577\n2,0.17377593405744984\n3,-0.05659623341790221\n4,0.34748985985708125\n5,-0.12194689179521723\n6,-0.620485099291358\n7,-0.9923264989212358\n8,-1.3929741814805254\n9,-1.6678174703849014\n10,-1.579591288613389\n11,-1.978142736662846\n12,-2.2805871186713085\n13,-2.2620781267594783\n14,-2.414411048913178\n15,-2.7736259778099255\n16,-2.399259632735366\n17,-2.653120757658206\n18,-2.475807597667098\n19,-2.904404788269629\n",
:format {:type "csv"}}}
9.1.2 More examples
-> datasets/mtcars
(
(vis.hanami/plot ht/boxplot-chart:X :gear
{:XTYPE :nominal
:Y :mpg}))
-> datasets/iris
(
(vis.hanami/plot ht/rule-chart:X :sepal-width
{:Y :sepal-length
:X2 :petal-width
:Y2 :petal-length
:OPACITY 0.2
:SIZE 3
:COLOR "species"}))
9.1.3 Grouped datasets
Grouped datasets are handled automatically with a table view.
-> datasets/iris
(:species])
(tc/group-by [
(vis.hanami/plot ht/rule-chart:X :sepal-width
{:Y :sepal-length
:X2 :petal-width
:Y2 :petal-length
:OPACITY 0.2
:SIZE 3}))
species | plot |
---|---|
setosa | |
versicolor | |
virginica |
9.1.4 Layers
-> random-walk
(
(vis.hanami/layers:TITLE "points and a line"}
{nil
[(vis.hanami/plot
ht/point-chart:MSIZE 400})
{nil
(vis.hanami/plot
ht/line-chart:MSIZE 4
{:MCOLOR "brown"})]))
Alternatively:
-> random-walk
(
(vis.hanami/combined-plot
ht/layer-chart:TITLE "points and a line"}
{:LAYER [[ht/point-chart
:MSIZE 400}]
{
[ht/line-chart:MSIZE 4
{:MCOLOR "brown"}]]))
9.1.5 Concatenation
Vertical
-> random-walk
(
(vis.hanami/vconcat
{}nil
[(vis.hanami/plot
ht/point-chart:MSIZE 400
{:HEIGHT 100
:WIDTH 100})
nil
(vis.hanami/plot
ht/line-chart:MSIZE 4
{:MCOLOR "brown"
:HEIGHT 100
:WIDTH 100})]))
Alternatively:
-> random-walk
(
(vis.hanami/combined-plot
ht/vconcat-chart:HEIGHT 100
{:WIDTH 100}
:VCONCAT [[ht/point-chart
:MSIZE 400}]
{
[ht/line-chart:MSIZE 4
{:MCOLOR "brown"}]]))
Horizontal
-> random-walk
(
(vis.hanami/hconcat
{}nil
[(vis.hanami/plot
ht/point-chart:MSIZE 400
{:HEIGHT 100
:WIDTH 100})
nil
(vis.hanami/plot
ht/line-chart:MSIZE 4
{:MCOLOR "brown"
:HEIGHT 100
:WIDTH 100})]))
Alternatively:
-> random-walk
(
(vis.hanami/combined-plot
ht/hconcat-chart:HEIGHT 100
{:WIDTH 100}
:HCONCAT [[ht/point-chart
:MSIZE 400}]
{
[ht/line-chart:MSIZE 4
{:MCOLOR "brown"}]]))
:bye
:bye