11 More visualization examples (experimental π )
author: Daniel Slutsky
ns noj-book.more-visualization
(:require [aerial.hanami.templates :as ht]
(:as color]
[clojure2d.color :as datasets]
[noj-book.datasets :as kind]
[scicloj.kindly.v4.kind :as stats]
[scicloj.noj.v1.stats :as vis.hanami]
[scicloj.noj.v1.vis.hanami :as vis.stats]
[scicloj.noj.v1.vis.stats :as tc])) [tablecloth.api
11.1 Combining a few things together
The following is inspired by the example at Plotnineβs main page. Note how we add regression lines here. We take care of layout and colouring on our side, not using Vega-Lite for that.
let [pallete (->> :accent
(
color/palettemapv color/format-hex))]
(-> datasets/mtcars
(:gear {:result-type :as-map})
(tc/group-by ->> (sort-by key)
(
(map-indexedfn [i [group-name ds]]
(-> ds
(
(vis.stats/linear-regression-plot:mpg :wt
:TITLE (str "grear=" group-name)
{:X :wt
:MCOLOR (pallete i)
:HEIGHT 200
:WIDTH 200
:point-options {:MSIZE 200}
:line-options {:MSIZE 5}}))))
nil {})))) (vis.hanami/vconcat
Alternatively, using a grouped dataset:
let [pallete (->> :accent
(
color/palettemapv color/format-hex))]
(-> datasets/mtcars
(:color [:gear] #(-> % (- 3) pallete))
(tc/map-columns :gear])
(tc/group-by [
(vis.stats/linear-regression-plot:mpg :wt
:X :wt
{:MCOLOR {:expr "datum.color"}
:HEIGHT 200
:WIDTH 200
:point-options {:MSIZE 200}
:line-options {:MSIZE 5}})
:gear]))) (tc/order-by [
gear | plot |
---|---|
3 | |
4 | |
5 |
A similar example with histograms:
let [pallete (->> :accent
(
color/palettemapv color/format-hex))]
(-> datasets/iris
(:species {:result-type :as-map})
(tc/group-by ->> (sort-by key)
(
(map-indexedfn [i [group-name ds]]
(-> ds
(:sepal-width
(vis.stats/histogram :nbins 10}))))
{nil {})))) (vis.hanami/vconcat
Scatterplots and regression lines again, this time using Vega-Lite for layout and coloring (using its βfacetβ option).
-> datasets/mtcars
(:gear])
(tc/group-by [:mpg [:wt]
(stats/add-predictions :model-type :smile.regression/ordinary-least-square})
{
(tc/ungroup):gear :wt :mpg :mpg-prediction])
(tc/select-columns [
(vis.hanami/combined-plot
ht/layer-chart
{}:LAYER [[ht/point-chart
:X :wt
{:Y :mpg
:MSIZE 200
:COLOR "gear"
:HEIGHT 100
:WIDTH 200}]
[ht/line-chart:X :wt
{:Y :mpg-prediction
:MSIZE 5
:COLOR "gear"
:YTITLE :mpg}]])
fn [spec]
((:facet {:row {:field "gear"}}
{:spec (dissoc spec :data)
:data (:data spec)}))
kind/vega-lite)