19  Charts: grids and fields

Charts colouring a plane: a cell per row (tile), a 2D histogram (bin2d), a scalar field drawn as a raster (field) or as level lines (contour), and the correlation of two variables as one glyph (correlation).

(ns reference.charts-grids-fields
  (:require [cljplot2.chart :as chart]
            [cljplot2.core :as plot]
            [cljplot2.describe :as describe]
            [notebook-utils :as nu]))

Data: Old Faithful geyser eruptions, R datasets package faithful (Härdle 1991; Azzalini & Bowman 1990); licence: Part of R (GPL-2 | GPL-3).

(def faithful (nu/dataset :faithful))
eruptions waiting
3.6 79
1.8 54
3.333 74
2.283 62
4.533 85
2.883 55
4.7 88
3.6 85
1.95 51
4.35 85
1.833 54
3.917 84
4.2 78
1.75 47
4.7 83
2.167 52
1.75 62
4.8 84
1.6 52
4.25 79
1.8 51
1.75 47
3.45 78
3.067 69
4.533 74
3.6 83
1.967 55
4.083 76
3.85 78
4.433 79
4.3 73
4.467 77
3.367 66
4.033 80
3.833 74
2.017 52
1.867 48
4.833 80
1.833 59
4.783 90
4.35 80
1.883 58
4.567 84
1.75 58
4.533 73
3.317 83
3.833 64
2.1 53
4.633 82
2 59
4.8 75
4.716 90
1.833 54
4.833 80
1.733 54
4.883 83
3.717 71
1.667 64
4.567 77
4.317 81
2.233 59
4.5 84
1.75 48
4.8 82
1.817 60
4.4 92
4.167 78
4.7 78
2.067 65
4.7 73
4.033 82
1.967 56
4.5 79
4 71
1.983 62
5.067 76
2.017 60
4.567 78
3.883 76
3.6 83
4.133 75
4.333 82
4.1 70
2.633 65
4.067 73
4.933 88
3.95 76
4.517 80
2.167 48
4 86
2.2 60
4.333 90
1.867 50
4.817 78
1.833 63
4.3 72
4.667 84
3.75 75
1.867 51
4.9 82
… …

Data: Maunga Whau volcano topography, R datasets package volcano (Ross Ihaka); licence: Part of R (GPL-2 | GPL-3).

(def volcano (nu/dataset :volcano))
row column height
1 1 100
2 1 101
3 1 102
4 1 103
5 1 104
6 1 105
7 1 105
8 1 106
9 1 107
10 1 108
11 1 109
12 1 110
13 1 110
14 1 111
15 1 114
16 1 116
17 1 118
18 1 120
19 1 120
20 1 121
21 1 122
22 1 122
23 1 123
24 1 124
25 1 123
26 1 123
27 1 120
28 1 118
29 1 117
30 1 115
31 1 114
32 1 115
33 1 113
34 1 111
35 1 110
36 1 109
37 1 108
38 1 108
39 1 107
40 1 107
41 1 107
42 1 108
43 1 109
44 1 110
45 1 111
46 1 111
47 1 112
48 1 113
49 1 113
50 1 114
51 1 115
52 1 115
53 1 114
54 1 113
55 1 112
56 1 111
57 1 111
58 1 112
59 1 112
60 1 112
61 1 113
62 1 114
63 1 114
64 1 115
65 1 115
66 1 116
67 1 116
68 1 117
69 1 117
70 1 116
71 1 114
72 1 112
73 1 109
74 1 106
75 1 104
76 1 102
77 1 101
78 1 100
79 1 100
80 1 99
81 1 99
82 1 99
83 1 99
84 1 98
85 1 98
86 1 97
87 1 97
1 2 100
2 2 101
3 2 102
4 2 103
5 2 104
6 2 105
7 2 106
8 2 107
9 2 108
10 2 109
11 2 110
12 2 110
13 2 111
… … …

Data: Edgar Anderson’s iris measurements, R datasets package iris (Anderson 1935; Fisher 1936); licence: Part of R (GPL-2 | GPL-3).

(def iris (nu/dataset :iris))
sepal-length sepal-width petal-length petal-width species
5.1 3.5 1.4 0.2 setosa
4.9 3 1.4 0.2 setosa
4.7 3.2 1.3 0.2 setosa
4.6 3.1 1.5 0.2 setosa
5 3.6 1.4 0.2 setosa
5.4 3.9 1.7 0.4 setosa
4.6 3.4 1.4 0.3 setosa
5 3.4 1.5 0.2 setosa
4.4 2.9 1.4 0.2 setosa
4.9 3.1 1.5 0.1 setosa
5.4 3.7 1.5 0.2 setosa
4.8 3.4 1.6 0.2 setosa
4.8 3 1.4 0.1 setosa
4.3 3 1.1 0.1 setosa
5.8 4 1.2 0.2 setosa
5.7 4.4 1.5 0.4 setosa
5.4 3.9 1.3 0.4 setosa
5.1 3.5 1.4 0.3 setosa
5.7 3.8 1.7 0.3 setosa
5.1 3.8 1.5 0.3 setosa
5.4 3.4 1.7 0.2 setosa
5.1 3.7 1.5 0.4 setosa
4.6 3.6 1 0.2 setosa
5.1 3.3 1.7 0.5 setosa
4.8 3.4 1.9 0.2 setosa
5 3 1.6 0.2 setosa
5 3.4 1.6 0.4 setosa
5.2 3.5 1.5 0.2 setosa
5.2 3.4 1.4 0.2 setosa
4.7 3.2 1.6 0.2 setosa
4.8 3.1 1.6 0.2 setosa
5.4 3.4 1.5 0.4 setosa
5.2 4.1 1.5 0.1 setosa
5.5 4.2 1.4 0.2 setosa
4.9 3.1 1.5 0.2 setosa
5 3.2 1.2 0.2 setosa
5.5 3.5 1.3 0.2 setosa
4.9 3.6 1.4 0.1 setosa
4.4 3 1.3 0.2 setosa
5.1 3.4 1.5 0.2 setosa
5 3.5 1.3 0.3 setosa
4.5 2.3 1.3 0.3 setosa
4.4 3.2 1.3 0.2 setosa
5 3.5 1.6 0.6 setosa
5.1 3.8 1.9 0.4 setosa
4.8 3 1.4 0.3 setosa
5.1 3.8 1.6 0.2 setosa
4.6 3.2 1.4 0.2 setosa
5.3 3.7 1.5 0.2 setosa
5 3.3 1.4 0.2 setosa
7 3.2 4.7 1.4 versicolor
6.4 3.2 4.5 1.5 versicolor
6.9 3.1 4.9 1.5 versicolor
5.5 2.3 4 1.3 versicolor
6.5 2.8 4.6 1.5 versicolor
5.7 2.8 4.5 1.3 versicolor
6.3 3.3 4.7 1.6 versicolor
4.9 2.4 3.3 1 versicolor
6.6 2.9 4.6 1.3 versicolor
5.2 2.7 3.9 1.4 versicolor
5 2 3.5 1 versicolor
5.9 3 4.2 1.5 versicolor
6 2.2 4 1 versicolor
6.1 2.9 4.7 1.4 versicolor
5.6 2.9 3.6 1.3 versicolor
6.7 3.1 4.4 1.4 versicolor
5.6 3 4.5 1.5 versicolor
5.8 2.7 4.1 1 versicolor
6.2 2.2 4.5 1.5 versicolor
5.6 2.5 3.9 1.1 versicolor
5.9 3.2 4.8 1.8 versicolor
6.1 2.8 4 1.3 versicolor
6.3 2.5 4.9 1.5 versicolor
6.1 2.8 4.7 1.2 versicolor
6.4 2.9 4.3 1.3 versicolor
6.6 3 4.4 1.4 versicolor
6.8 2.8 4.8 1.4 versicolor
6.7 3 5 1.7 versicolor
6 2.9 4.5 1.5 versicolor
5.7 2.6 3.5 1 versicolor
5.5 2.4 3.8 1.1 versicolor
5.5 2.4 3.7 1 versicolor
5.8 2.7 3.9 1.2 versicolor
6 2.7 5.1 1.6 versicolor
5.4 3 4.5 1.5 versicolor
6 3.4 4.5 1.6 versicolor
6.7 3.1 4.7 1.5 versicolor
6.3 2.3 4.4 1.3 versicolor
5.6 3 4.1 1.3 versicolor
5.5 2.5 4 1.3 versicolor
5.5 2.6 4.4 1.2 versicolor
6.1 3 4.6 1.4 versicolor
5.8 2.6 4 1.2 versicolor
5 2.3 3.3 1 versicolor
5.6 2.7 4.2 1.3 versicolor
5.7 3 4.2 1.2 versicolor
5.7 2.9 4.2 1.3 versicolor
6.2 2.9 4.3 1.3 versicolor
5.1 2.5 3 1.1 versicolor
5.7 2.8 4.1 1.3 versicolor
… … … … …

19.1 tile

A cell per row at its x and y, filled by a mapped colour: numeric (a gradient) or categorical (a palette).

Option Type Default Description
:x (required) a keyword, a function or a categorical marker of one cell x position: categorical (a cell per level) or numeric (cells centred on the values, sized by the smallest gap)
:y (required) a keyword, a function or a categorical marker of one cell y position: categorical (a cell per level) or numeric (cells centred on the values, sized by the smallest gap)
:color (required) a keyword, a function or a categorical marker of one cell fill: numeric (a gradient and colour bar) or categorical (a palette and legend); nil - the NA colour
:size a keyword, a function or a categorical marker of one or nil cell size: each cell shrinks by :cell-scale times its size over the size scale's largest
(def week-hours (for [[i day] (map-indexed vector ["Mon" "Tue" "Wed" "Thu" "Fri"]) hour (range 8 18)]
                  {:day day :hour hour
                   :visits (+ 10 (* 3 (mod (* (inc i) hour) 7)) (if (< 11 hour 14) 15 0))}))
day hour visits
Mon 8 13
Mon 9 16
Mon 10 19
Mon 11 22
Mon 12 40
Mon 13 43
Mon 14 10
Mon 15 13
Mon 16 16
Mon 17 19
Tue 8 16
Tue 9 22
Tue 10 28
Tue 11 13
Tue 12 34
Tue 13 40
Tue 14 10
Tue 15 16
Tue 16 22
Tue 17 28
Wed 8 19
Wed 9 28
Wed 10 16
Wed 11 25
Wed 12 28
Wed 13 37
Wed 14 10
Wed 15 19
Wed 16 28
Wed 17 16
Thu 8 22
Thu 9 13
Thu 10 25
Thu 11 16
Thu 12 43
Thu 13 34
Thu 14 10
Thu 15 22
Thu 16 13
Thu 17 25
Fri 8 25
Fri 9 19
Fri 10 13
Fri 11 28
Fri 12 37
Fri 13 31
Fri 14 10
Fri 15 25
Fri 16 19
Fri 17 13
(plot/render-image {:data week-hours
                    :mapping {:x :hour :y :day :color :visits}
                    :charts [(chart/tile)]
                    :scales {:y {:reverse true}}}
                   {:width 600 :height 300})

Its own option writes the value in each cell (black or white by the fill, the :tile-value text role); the cell shape and outline are the Cell style group:

Option Type Default Description
:value-label true or false or a function false label in each cell: true (the colour value, numbers to 2 decimals) or a function of {:value :x :y} returning the text (nil - none), drawn in black or white by the fill under it
(plot/render-image (plot/arrange [{:data week-hours
                                   :mapping {:x :hour :y :day :color :visits}
                                   :charts [(chart/tile {:value-label true})]
                                   :scales {:y {:reverse true}}
                                   :labels {:title ":value-label true"}
                                   :legend false}
                                  {:data week-hours
                                   :mapping {:x :hour :y :day :color :visits}
                                   :charts [(chart/tile {:value-label #(when (< 30 (:value %)) "busy")})]
                                   :scales {:y {:reverse true}}
                                   :labels {:title "a function: \"busy\" above 30"}}]
                                 {:columns 2 :widths [1 1.15]})
                   {:width 1000 :height 300})

A mapped :size shrinks each cell by its value; with the colour mapped to another variable the size legend’s keys have no colour of their own - their fill is a theme key of [:chart :tile], not an option:

Option Type Default Description
:size-key-fill a clojure2d color or :none [110.0 110.0 110.0 255.0] fill of a size legend key without a colour (theme only, not an option)
(plot/render-image {:data week-hours
                    :mapping {:x :hour :y :day :color :day :size :visits}
                    :charts [(chart/tile)]
                    :scales {:y {:reverse true}}
                    :theme {:chart {:tile {:size-key-fill [200 120 60]}}}}
                   {:width 600 :height 300})

Shared options: Cell style (:cell :cell-scale :stroke-color :stroke-width :alpha); Data and mapping (:data :mapping); Named theme styles (:theme-style).

19.2 bin2d

A 2D histogram: points counted in rectangular cells, the count (or another measure) as the cell colour.

Option Type Default Description
:x (required) a keyword, a function or a categorical marker of one point x positions (numbers)
:y (required) a keyword, a function or a categorical marker of one point y positions (numbers)
:color nil must be nil (the cell colour is the measure): set it so in the chart's mapping when the plot maps :color
(plot/render-image {:data faithful
                    :mapping {:x :eruptions :y :waiting}
                    :charts [(chart/bin2d)]
                    :labels {:x "eruption [min]" :y "waiting [min]"}}
                   {:width 600 :height 400})

Its options: the number of cells (or a fastmath estimate), a cell width in data units, the cells’ height / width ratio and what the colour measures:

Option Type Default Description
:bins one of :scott-correlated :scott :freedman-diaconis :cross-validation :shimazaki-shinomoto or a positive integer :scott-correlated cells across the x range (a positive integer), or a fastmath estimate-grid-size method giving a cell area (:binwidth wins)
:binwidth a positive finite number cell size in x data units (log10 units on a :log axis); wins over :bins
:shape a positive finite number the panel's height / width (y span / x span with :aspect :iso), estimated from the plot size and facet layout height / width ratio of the normalized binning space (cells are regular in pixels when it matches the panel)
:measure one of :count :percent :pmf :density :count the cell colour: :count, :percent (of the panel's points), :pmf (share of the panel's points) or :density (count / (n * cell area in data units))
(plot/render-image (plot/arrange [{:data faithful
                                   :mapping {:x :eruptions :y :waiting}
                                   :charts [(chart/bin2d {:bins 10})]
                                   :labels {:title ":bins 10"}
                                   :legend false}
                                  {:data faithful
                                   :mapping {:x :eruptions :y :waiting}
                                   :charts [(chart/bin2d {:binwidth 0.25 :shape 0.5})]
                                   :labels {:title ":binwidth 0.25 :shape 0.5"}
                                   :legend false}
                                  {:data faithful
                                   :mapping {:x :eruptions :y :waiting}
                                   :charts [(chart/bin2d {:measure :percent})]
                                   :labels {:title ":measure :percent"}}]
                                 {:columns 3 :widths [1 1 1.3]})
                   {:width 1000 :height 320})

The fastmath estimates of the grid (the default :scott-correlated above) - each picks its own cell count:

(plot/render-image (plot/arrange [{:data faithful
                                   :mapping {:x :eruptions :y :waiting}
                                   :charts [(chart/bin2d {:bins :scott})]
                                   :labels {:title ":bins :scott"}
                                   :legend false}
                                  {:data faithful
                                   :mapping {:x :eruptions :y :waiting}
                                   :charts [(chart/bin2d {:bins :freedman-diaconis})]
                                   :labels {:title ":bins :freedman-diaconis"}
                                   :legend false}
                                  {:data faithful
                                   :mapping {:x :eruptions :y :waiting}
                                   :charts [(chart/bin2d {:bins :cross-validation})]
                                   :labels {:title ":bins :cross-validation"}
                                   :legend false}
                                  {:data faithful
                                   :mapping {:x :eruptions :y :waiting}
                                   :charts [(chart/bin2d {:bins :shimazaki-shinomoto})]
                                   :labels {:title ":bins :shimazaki-shinomoto"}
                                   :legend false}]
                                 {:columns 2})
                   {:width 900 :height 600})

The other measures: the share of the panel’s points (:pmf) and the density - the count over n times the cell area (the legend shows the scale):

(plot/render-image (plot/arrange [{:data faithful
                                   :mapping {:x :eruptions :y :waiting}
                                   :charts [(chart/bin2d {:measure :pmf})]
                                   :labels {:title ":measure :pmf"}}
                                  {:data faithful
                                   :mapping {:x :eruptions :y :waiting}
                                   :charts [(chart/bin2d {:measure :density})]
                                   :labels {:title ":measure :density"}}]
                                 {:columns 2})
                   {:width 900 :height 340})

Shared options: Cell style (:cell :cell-scale :stroke-color :stroke-width :alpha); Data and mapping (:data :mapping); Named theme styles (:theme-style).

19.3 field

A raster of a scalar field: the density of mapped points, gridded :values interpolated, or a function of a point (the Fields group).

Option Type Default Description
:x a keyword, a function or a categorical marker of one or nil x of the points (density) or of the gridded data
:y a keyword, a function or a categorical marker of one or nil y of the points (density) or of the gridded data
:value a keyword, a function or a categorical marker of one or nil the field's value at gridded (x, y) on a full lattice, interpolated (absent - a density of the points)
:color nil must be nil (the colour is the field's value): set it so in the chart's mapping when the plot maps :color
(plot/render-image (plot/arrange [{:data faithful
                                   :mapping {:x :eruptions :y :waiting}
                                   :charts [(chart/field)]
                                   :labels {:title "density of the points"}
                                   :legend false}
                                  {:data volcano
                                   :mapping {:x :column :y :row :value :height}
                                   :charts [(chart/field)]
                                   :labels {:title "gridded :value"}}]
                                 {:columns 2 :widths [1 1.2]})
                   {:width 1000 :height 380})

Its options: the raster’s opacity, or a colour per pixel computed by a function (no field, no colour scale):

Option Type Default Description
:alpha a number in [0 1] 1.0 opacity multiplying the colours' alpha
:pixel-color a function a function of a point [x y] returning each pixel's colour (nil - transparent): a raster over :domain, no grid, colour scale or legend; not with :f
(plot/render-image (plot/arrange [{:data faithful
                                   :mapping {:x :eruptions :y :waiting}
                                   :charts [(chart/field {:alpha 0.5}) (chart/scatter {:size 3 :color :black})]
                                   :labels {:title ":alpha 0.5, points over it"}
                                   :legend false}
                                  {:charts [(chart/field {:domain {:x [-2 2] :y [-2 2]}
                                                          :pixel-color (fn [[x y]]
                                                                         (when (< (+ (* x x) (* y y)) 3)
                                                                           [(int (* 60 (+ x 2))) 80 (int (* 60 (+ y 2)))]))})]
                                   :labels {:title ":pixel-color - a colour per point"}}]
                                 {:columns 2})
                   {:width 900 :height 380})

19.3.1 Field sources

The Fields group gives the field. A function of a point [x y] sampled over a :domain on a grid of :n points per axis (a coarse grid shows its cells):

(plot/render-image (plot/arrange [{:charts [(chart/field {:f (fn [[x y]] (Math/sin (+ (* x x) (* y y))))
                                                          :domain {:x [-2 2] :y [-2 2]}})]
                                   :labels {:title ":f over :domain"}
                                   :legend false}
                                  {:charts [(chart/field {:f (fn [[x y]] (Math/sin (+ (* x x) (* y y))))
                                                          :domain {:x [-2 2] :y [-2 2]} :n 12})]
                                   :labels {:title ":n 12"}}]
                                 {:columns 2 :widths [1 1.2]})
                   {:width 900 :height 380})

Gridded values are interpolated between the lattice points - here a 5 x 5 lattice:

(def lattice (for [x (range 5) y (range 5)] {:x x :y y :z (* (Math/sin x) (Math/cos (* 0.8 y)))}))
x y z
0 0 0.0
0 1 0.0
0 2 -0.0
0 3 -0.0
0 4 -0.0
1 0 0.8414709848078965
1 1 0.5862584808366281
1 2 -0.024570550786785637
1 3 -0.620495416007646
1 4 -0.8400360881165891
2 0 0.9092974268256817
2 1 0.6335136180615656
2 2 -0.02655105049310103
2 3 -0.6705102080990835
2 4 -0.9077468708436899
3 0 0.1411200080598672
3 1 0.09831925643843555
3 2 -0.00412063682250215
3 3 -0.10406100708047604
3 4 -0.14087936680627888
4 0 -0.7568024953079282
4 1 -0.5272693761317102
4 2 0.022098271339414874
4 3 0.5580614039459993
4 4 0.7555119773743378
(plot/render-image (plot/arrange [{:data lattice
                                   :mapping {:x :x :y :y :value :z}
                                   :charts [(chart/field {:interpolation :bilinear})]
                                   :labels {:title ":interpolation :bilinear"}
                                   :legend false}
                                  {:data lattice
                                   :mapping {:x :x :y :y :value :z}
                                   :charts [(chart/field {:interpolation :bicubic})]
                                   :labels {:title ":interpolation :bicubic"}
                                   :legend false}
                                  {:data lattice
                                   :mapping {:x :x :y :y :value :z}
                                   :charts [(chart/field {:interpolation :cubic-2d})]
                                   :labels {:title ":interpolation :cubic-2d"}}]
                                 {:columns 3 :widths [1 1 1.25]})
                   {:width 1000 :height 320})

The density of mapped points: its kernel and bandwidth, a binned estimate (faster on many points), and how far the domain reaches past the points (:cut, in bandwidths):

(plot/render-image (plot/arrange [{:data faithful
                                   :mapping {:x :eruptions :y :waiting}
                                   :charts [(chart/field {:kernel :epanechnikov :bandwidth [0.2 4]})]
                                   :labels {:title "epanechnikov, bandwidth [0.2 4]"}
                                   :legend false}
                                  {:data faithful
                                   :mapping {:x :eruptions :y :waiting}
                                   :charts [(chart/field {:binned? true})]
                                   :labels {:title ":binned? true"}
                                   :legend false}
                                  {:data faithful
                                   :mapping {:x :eruptions :y :waiting}
                                   :charts [(chart/field {:cut 0})]
                                   :labels {:title ":cut 0"}
                                   :legend false}]
                                 {:columns 3})
                   {:width 1000 :height 320})

Shared options: Fields (:f :domain :kernel :bandwidth :binned? :cut :interpolation :n); Data and mapping (:data :mapping); Named theme styles (:theme-style).

19.4 contour

Level lines (or filled bands) of a scalar field - the same sources as field; categorical groups give a field each.

Option Type Default Description
:x a keyword, a function or a categorical marker of one or nil x of the points (density) or of the gridded data
:y a keyword, a function or a categorical marker of one or nil y of the points (density) or of the gridded data
:value a keyword, a function or a categorical marker of one or nil the field's value at gridded (x, y) on a full lattice, interpolated (absent - a density of the points)
:color a keyword, a function or a categorical marker of one or nil categorical groups, each its own field with lines in its colour (else the lines are coloured by level)
:dash a keyword, a function or a categorical marker of one or nil groups, each its own field with lines in its dash style
:group a keyword, a function or a categorical marker of one or nil groups without a visual aesthetic, each its own field
(plot/render-image (plot/arrange [{:data volcano
                                   :mapping {:x :column :y :row :value :height}
                                   :charts [(chart/contour)]
                                   :labels {:title "gridded :value"}
                                   :legend false}
                                  {:data iris
                                   :mapping {:x :sepal-length :y :sepal-width :color :species}
                                   :charts [(chart/contour {:levels 6})]
                                   :labels {:title "a density per :color group"}}]
                                 {:columns 2 :widths [1 1.25]})
                   {:width 1000 :height 380})

Its options: the levels (a count or the values), filled bands instead of lines, labels on the lines, one line colour and the opacity:

Option Type Default Description
:color a clojure2d color or :none [69.0 117.0 180.0 255.0]; the theme's, for grouped lines without a mapped colour; ungrouped lines are coloured by level, lines grouped by :color take their group's colour one colour for every line instead of colouring by level (contour's own :color, not the line style's)
:alpha a number in [0 1] 1.0; the theme's; grouped filled contours take half of it opacity multiplying the colours' alpha
:levels integer (1..1000) or vector of a finite number (at least 1) 10 contour levels: a count evenly spread over the range of every sampled value (the ends left out), or a vector of level values
:fill true or false false fill the region at or above each level, each band coloured by its middle value, instead of drawing lines
:labels true or false or a function or nil false line contours only: label each line with its level - true (the number) or a function of {:level v} returning the text (nil - no label)
(plot/render-image (plot/arrange [{:data volcano
                                   :mapping {:x :column :y :row :value :height}
                                   :charts [(chart/contour {:levels [100 120 140 160 180] :labels true
                                                            :color [40 40 40]})]
                                   :labels {:title ":levels vector, :labels true"}}
                                  {:data volcano
                                   :mapping {:x :column :y :row :value :height}
                                   :charts [(chart/contour {:fill true :levels 8 :alpha 0.8})]
                                   :labels {:title ":fill true :levels 8"}}]
                                 {:columns 2 :widths [1 1.25]})
                   {:width 1000 :height 380})

19.4.1 Contour sources

The same sources as field: a function over a domain (lines on a coarse grid are angular), interpolated gridded values and a density with its own bandwidth:

(plot/render-image (plot/arrange [{:charts [(chart/contour {:f (fn [[x y]] (+ (* x x) (* 2 y y) (* x y)))
                                                            :domain {:x [-2 2] :y [-2 2]} :labels true})]
                                   :labels {:title ":f over :domain"}
                                   :legend false}
                                  {:charts [(chart/contour {:f (fn [[x y]] (+ (* x x) (* 2 y y) (* x y)))
                                                            :domain {:x [-2 2] :y [-2 2]} :n 6})]
                                   :labels {:title ":n 6"}
                                   :legend false}
                                  {:data lattice
                                   :mapping {:x :x :y :y :value :z}
                                   :charts [(chart/contour {:interpolation :bicubic :levels 8})]
                                   :labels {:title "5 x 5 lattice, :bicubic"}
                                   :legend false}
                                  {:data faithful
                                   :mapping {:x :eruptions :y :waiting}
                                   :charts [(chart/scatter {:size 3 :color [0 0 0 80]})
                                            (chart/contour {:bandwidth [0.15 3] :cut 1})]
                                   :labels {:title ":bandwidth [0.15 3] :cut 1"}}]
                                 {:columns 2 :widths [1 1.25]})
                   {:width 900 :height 640})

Shared options: Line style (:width :dash); Fields (:f :domain :kernel :bandwidth :binned? :cut :interpolation :n); Data and mapping (:data :mapping); Named theme styles (:theme-style).

19.5 correlation

The correlation coefficient r of two variables drawn as one glyph filling the panel - a cell of a correlogram (plot/pairs). Alone it writes r, one line per group.

Option Type Default Description
:x (required) a keyword, a function or a categorical marker of one first variable (numbers)
:y (required) a keyword, a function or a categorical marker of one second variable (numbers)
:color a keyword, a function or a categorical marker of one or nil categorical groups, each a line of the :number glyph in its colour
:group a keyword, a function or a categorical marker of one or nil groups without a visual aesthetic, each a line of the :number glyph
(plot/render-image {:data iris
                    :mapping {:x :sepal-length :y :petal-length :color :species}
                    :charts [(chart/correlation)]}
                   {:width 450 :height 300})

Its options: the coefficient, the glyph and its colours (by default r on a diverging scale):

Option Type Default Description
:color a clojure2d color or :none [69.0 117.0 180.0 255.0]; grouped shape glyphs: the theme's; ungrouped: r on a diverging scale; :number: the :correlation text role's colour, group lines mapped or black one fixed glyph colour, the :number text's too
:stroke-color :darker or a clojure2d color or :none :darker outline colour: a colour, :none or :darker (the darkened fill)
:stroke-width a non-negative number 1 outline width in pixels
:method one of :pearson :spearman :kendall :pearson correlation coefficient: :pearson, :spearman or :kendall
:glyph one of :number :shade :square :circle :ellipse :pie :number how r is drawn: :number (text, a line per group), :shade (the panel in r's colour), :square / :circle (area ~ |r|), :ellipse (normal contour) or :pie (a disk filled |r|)
:glyph-size a number in [0 1] 0.9 glyph side (:square, :circle, :ellipse, :pie) as a fraction of the panel's shorter side

Every glyph, for one pair of variables (r about 0.82):

(plot/render-image (plot/arrange [{:data iris :mapping {:x :sepal-length :y :petal-width}
                                   :charts [(chart/correlation {:glyph :number})] :labels {:title ":glyph :number"}}
                                  {:data iris :mapping {:x :sepal-length :y :petal-width}
                                   :charts [(chart/correlation {:glyph :shade})] :labels {:title ":glyph :shade"}}
                                  {:data iris :mapping {:x :sepal-length :y :petal-width}
                                   :charts [(chart/correlation {:glyph :square})] :labels {:title ":glyph :square"}}
                                  {:data iris :mapping {:x :sepal-length :y :petal-width}
                                   :charts [(chart/correlation {:glyph :circle})] :labels {:title ":glyph :circle"}}
                                  {:data iris :mapping {:x :sepal-length :y :petal-width}
                                   :charts [(chart/correlation {:glyph :ellipse})] :labels {:title ":glyph :ellipse"}}
                                  {:data iris :mapping {:x :sepal-length :y :petal-width}
                                   :charts [(chart/correlation {:glyph :pie})] :labels {:title ":glyph :pie"}}]
                                 {:columns 3})
                   {:width 900 :height 560})

The three coefficients of the same pair (Kendall’s tau, counting concordant pairs, is the lowest), and a smaller glyph in one fixed colour with a thick outline:

(plot/render-image (plot/arrange [{:data iris :mapping {:x :sepal-length :y :petal-width}
                                   :charts [(chart/correlation {:method :pearson})] :labels {:title ":method :pearson"}}
                                  {:data iris :mapping {:x :sepal-length :y :petal-width}
                                   :charts [(chart/correlation {:method :spearman})] :labels {:title ":method :spearman"}}
                                  {:data iris :mapping {:x :sepal-length :y :petal-width}
                                   :charts [(chart/correlation {:method :kendall})] :labels {:title ":method :kendall"}}
                                  {:data iris :mapping {:x :sepal-length :y :petal-width}
                                   :charts [(chart/correlation {:glyph :circle :glyph-size 0.6 :color [120 160 90]
                                                                :stroke-color :black :stroke-width 3})]
                                   :labels {:title ":glyph-size 0.6, :stroke-width 3"}}]
                                 {:columns 4})
                   {:width 1000 :height 280})

In a correlogram:

(plot/render-image (plot/pairs iris {:variables [:sepal-length :sepal-width :petal-length :petal-width]
                                     :lower (chart/correlation {:glyph :ellipse :stroke-color :none})
                                     :upper (chart/correlation {:glyph :square :method :spearman})
                                     :diagonal :names
                                     :title "ellipses (Pearson) below, squares (Spearman) above"})
                   {:width 700 :height 620})

Shared options: Data and mapping (:data :mapping); Named theme styles (:theme-style).

source: notebooks/reference/charts_grids_fields.clj