9  Visualization (too be deprecated 🛠)

author: Daniel Slutsky

This functionality will soon be replated by Hanamicloth.

(ns noj-book.visualization
  (:require [aerial.hanami.templates :as ht]
            [noj-book.datasets :as datasets]
            [scicloj.kindly.v4.kind :as kind]
            [scicloj.noj.v1.vis.hanami :as vis.hanami]
            [tablecloth.api :as tc]))

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.12870137072265364\n1,0.5778825574493295\n2,0.4781125550996277\n3,0.00920959411099409\n4,0.4869390101573958\n5,0.6188674639033069\n6,0.471197386398169\n7,0.6499358705030457\n8,0.8455331619305582\n9,0.5333215708340313\n10,0.5944274512024917\n11,0.7363293789114881\n12,0.2790621657210287\n13,0.2781204572782676\n14,0.6541500345400689\n15,1.1251569515666144\n16,1.4917864735639952\n17,1.8120306050405643\n18,1.7835314948950487\n19,1.7443206608088042\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
    (tc/group-by [:species])
    (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"}
     [(vis.hanami/plot nil
                       ht/point-chart
                       {:MSIZE 400})
      (vis.hanami/plot nil
                       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
     {}
     [(vis.hanami/plot nil
                       ht/point-chart
                       {:MSIZE 400
                        :HEIGHT 100
                        :WIDTH 100})
      (vis.hanami/plot nil
                       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
     {}
     [(vis.hanami/plot nil
                       ht/point-chart
                       {:MSIZE 400
                        :HEIGHT 100
                        :WIDTH 100})
      (vis.hanami/plot nil
                       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
source: notebooks/noj_book/visualization.clj