4  Aesthetics and legends

Five aesthetics style marks from data: :color, :shape, :size, :dash and :pattern. Each mapped aesthetic has a scale (:scales {:color spec}) mapping its domain (levels or a numeric range) onto attributes (colours, shapes …), and every scale draws a legend unless told not to. All five share one spec; the colour scale uses every key, the others differ only in their :range.

(ns reference.aesthetics-legends
  (:require [cljplot2.chart :as chart]
            [cljplot2.core :as plot]
            [cljplot2.describe :as describe]
            [cljplot2.scale.aesthetic :as aesthetic]
            [notebook-utils :as nu])
  (:import [java.time LocalDate]))

Data: Gapminder 2007: life expectancy, population and GDP per capita, data-to-viz example dataset 4_ThreeNum (Yan Holtz; Gapminder via the R gapminder package); licence: MIT (data_to_viz repository).

(def gapminder (nu/dataset :gapminder-2007))
country continent life-exp pop gdp-per-cap
Afghanistan Asia 43.828 31889923 974.5803384
Albania Europe 76.423 3600523 5937.029526
Algeria Africa 72.301 33333216 6223.367465
Angola Africa 42.731 12420476 4797.231267
Argentina Americas 75.32 40301927 12779.37964
Australia Oceania 81.235 20434176 34435.36744
Austria Europe 79.829 8199783 36126.4927
Bahrain Asia 75.635 708573 29796.04834
Bangladesh Asia 64.062 150448339 1391.253792
Belgium Europe 79.441 10392226 33692.60508
Benin Africa 56.728 8078314 1441.284873
Bolivia Americas 65.554 9119152 3822.137084
Bosnia and Herzegovina Europe 74.852 4552198 7446.298803
Botswana Africa 50.728 1639131 12569.85177
Brazil Americas 72.39 190010647 9065.800825
Bulgaria Europe 73.005 7322858 10680.79282
Burkina Faso Africa 52.295 14326203 1217.032994
Burundi Africa 49.58 8390505 430.0706916
Cambodia Asia 59.723 14131858 1713.778686
Cameroon Africa 50.43 17696293 2042.09524
Canada Americas 80.653 33390141 36319.23501
Central African Republic Africa 44.741 4369038 706.016537
Chad Africa 50.651 10238807 1704.063724
Chile Americas 78.553 16284741 13171.63885
China Asia 72.961 1318683096 4959.114854
Colombia Americas 72.889 44227550 7006.580419
Comoros Africa 65.152 710960 986.1478792
Congo, Dem. Rep. Africa 46.462 64606759 277.5518587
Congo, Rep. Africa 55.322 3800610 3632.557798
Costa Rica Americas 78.782 4133884 9645.06142
Cote d'Ivoire Africa 48.328 18013409 1544.750112
Croatia Europe 75.748 4493312 14619.22272
Cuba Americas 78.273 11416987 8948.102923
Czech Republic Europe 76.486 10228744 22833.30851
Denmark Europe 78.332 5468120 35278.41874
Djibouti Africa 54.791 496374 2082.481567
Dominican Republic Americas 72.235 9319622 6025.374752
Ecuador Americas 74.994 13755680 6873.262326
Egypt Africa 71.338 80264543 5581.180998
El Salvador Americas 71.878 6939688 5728.353514
Equatorial Guinea Africa 51.579 551201 12154.08975
Eritrea Africa 58.04 4906585 641.3695236
Ethiopia Africa 52.947 76511887 690.8055759
Finland Europe 79.313 5238460 33207.0844
France Europe 80.657 61083916 30470.0167
Gabon Africa 56.735 1454867 13206.48452
Gambia Africa 59.448 1688359 752.7497265
Germany Europe 79.406 82400996 32170.37442
Ghana Africa 60.022 22873338 1327.60891
Greece Europe 79.483 10706290 27538.41188
Guatemala Americas 70.259 12572928 5186.050003
Guinea Africa 56.007 9947814 942.6542111
Guinea-Bissau Africa 46.388 1472041 579.231743
Haiti Americas 60.916 8502814 1201.637154
Honduras Americas 70.198 7483763 3548.330846
Hong Kong, China Asia 82.208 6980412 39724.97867
Hungary Europe 73.338 9956108 18008.94444
Iceland Europe 81.757 301931 36180.78919
India Asia 64.698 1110396331 2452.210407
Indonesia Asia 70.65 223547000 3540.651564
Iran Asia 70.964 69453570 11605.71449
Iraq Asia 59.545 27499638 4471.061906
Ireland Europe 78.885 4109086 40675.99635
Israel Asia 80.745 6426679 25523.2771
Italy Europe 80.546 58147733 28569.7197
Jamaica Americas 72.567 2780132 7320.880262
Japan Asia 82.603 127467972 31656.06806
Jordan Asia 72.535 6053193 4519.461171
Kenya Africa 54.11 35610177 1463.249282
Korea, Dem. Rep. Asia 67.297 23301725 1593.06548
Korea, Rep. Asia 78.623 49044790 23348.13973
Kuwait Asia 77.588 2505559 47306.98978
Lebanon Asia 71.993 3921278 10461.05868
Lesotho Africa 42.592 2012649 1569.331442
Liberia Africa 45.678 3193942 414.5073415
Libya Africa 73.952 6036914 12057.49928
Madagascar Africa 59.443 19167654 1044.770126
Malawi Africa 48.303 13327079 759.3499101
Malaysia Asia 74.241 24821286 12451.6558
Mali Africa 54.467 12031795 1042.581557
Mauritania Africa 64.164 3270065 1803.151496
Mauritius Africa 72.801 1250882 10956.99112
Mexico Americas 76.195 108700891 11977.57496
Mongolia Asia 66.803 2874127 3095.772271
Montenegro Europe 74.543 684736 9253.896111
Morocco Africa 71.164 33757175 3820.17523
Mozambique Africa 42.082 19951656 823.6856205
Myanmar Asia 62.069 47761980 944
Namibia Africa 52.906 2055080 4811.060429
Nepal Asia 63.785 28901790 1091.359778
Netherlands Europe 79.762 16570613 36797.93332
New Zealand Oceania 80.204 4115771 25185.00911
Nicaragua Americas 72.899 5675356 2749.320965
Niger Africa 56.867 12894865 619.6768924
Nigeria Africa 46.859 135031164 2013.977305
Norway Europe 80.196 4627926 49357.19017
Oman Asia 75.64 3204897 22316.19287
Pakistan Asia 65.483 169270617 2605.94758
Panama Americas 75.537 3242173 9809.185636
Paraguay Americas 71.752 6667147 4172.838464
… … … … …

4.1 Levels or numbers

An aesthetic scale has its own kinds - not those of the position scales (:log, :datetime … in the examples below are the x axis’s, Scales chapter). The kind follows the data: levels (strings, keywords) get an :ordinal scale - one attribute per level, in :domain order - numbers a :linear one - a gradient or a size range; :binned cuts numbers into classes; :type :categorical treats numbers or dates as levels; :identity reads the attributes from the data.

Option Type Default Description
:kind one of :binned :identity :linear :ordinal :linear for numeric :color / :size data, else :ordinal scale kind: :ordinal (levels), :linear (a gradient or size range), :binned (:color by bins) or :identity (values are the attributes)
:type one of :categorical :numeric declare numbers or dates categorical (ordinal levels) or force a numeric scale
:domain sequential of any value or nil the data's levels (sorted) or [min max] ordinal: the levels in order; linear / binned: [lo hi]
(plot/render-image (plot/arrange [{:data gapminder
                                   :mapping {:x :gdp-per-cap :y :life-exp :color :continent}
                                   :charts [(chart/scatter)]
                                   :scales {:x {:kind :log :formatter :power}
                                            :color {:domain ["Europe" "Asia" "Africa" "Americas" "Oceania"]}}}
                                  {:data gapminder
                                   :mapping {:x :gdp-per-cap :y :life-exp :color :life-exp}
                                   :charts [(chart/scatter)]
                                   :scales {:x {:kind :log :formatter :power}}}]
                                 {:columns 2})
                   {:width 900 :height 350})

Dates (java.time values) mapped to :color get a linear gradient over time, the colour bar ticked at dates (a Year / YearMonth reads as the date its period starts, on every scale):

(def readings (for [i (range 40)]
                {:day (.plusDays (LocalDate/of 2024 1 1) (* 9 i)) :x (mod (* i 7) 13) :y (mod (* i 5) 11)}))
day x y
2024-01-01 0 0
2024-01-10 7 5
2024-01-19 1 10
2024-01-28 8 4
2024-02-06 2 9
2024-02-15 9 3
2024-02-24 3 8
2024-03-04 10 2
2024-03-13 4 7
2024-03-22 11 1
2024-03-31 5 6
2024-04-09 12 0
2024-04-18 6 5
2024-04-27 0 10
2024-05-06 7 4
2024-05-15 1 9
2024-05-24 8 3
2024-06-02 2 8
2024-06-11 9 2
2024-06-20 3 7
2024-06-29 10 1
2024-07-08 4 6
2024-07-17 11 0
2024-07-26 5 5
2024-08-04 12 10
2024-08-13 6 4
2024-08-22 0 9
2024-08-31 7 3
2024-09-09 1 8
2024-09-18 8 2
2024-09-27 2 7
2024-10-06 9 1
2024-10-15 3 6
2024-10-24 10 0
2024-11-02 4 5
2024-11-11 11 10
2024-11-20 5 4
2024-11-29 12 9
2024-12-08 6 3
2024-12-17 0 8
(plot/render-image {:data readings
                    :mapping {:x :x :y :y :color :day}
                    :charts [(chart/scatter {:size 12})]}
                   {:width 600 :height 320})

Its :ticks take dates, and :formatter gets ticks in the data’s type (here LocalDates):

(plot/render-image {:data readings
                    :mapping {:x :x :y :y :color :day}
                    :charts [(chart/scatter {:size 12})]
                    :scales {:color {:ticks [(LocalDate/of 2024 3 1) (LocalDate/of 2024 6 1) (LocalDate/of 2024 9 1)]
                                     :formatter #(str "Q" (inc (quot (dec (.getMonthValue ^LocalDate %)) 3)) " " (.getYear ^LocalDate %))}}}
                   {:width 600 :height 320})

4.2 Colour range

The attributes the domain maps to: a sequence of colours, a level → colour map, or a clojure2d palette or gradient name - 7339 palettes and 5009 gradients. Without :range the theme :palette supplies it.

Option Type Default Description
:range map of any value to a clojure2d color or a function or a palette or gradient name, or a non-empty sequence of colors theme :palette attributes the domain maps to: palette, gradient, shapes, sizes [min max], dashes or patterns, or a level map

Search palettes and gradients by name with plot/find-palette and plot/find-gradient (a regular expression, or a namespace and one); plot/palette gives a palette’s colours, plot/gradient a gradient function:

(plot/find-palette #"tableau")
(:tableau-10
 :tableau-10-2
 :tableau-10-light
 :tableau-10-medium
 :tableau-20
 :tableau-20-2
 :tableau-classic-20
 :tableau-classic-medium)

Swatches of a few palettes (an :identity colour scale reads the colours from the data):

(def palettes (plot/find-palette #"^:(rcartocolor/(Bold|Pastel|Vivid|Prism)|tableau-10)$"))
(def palette-swatches (for [p palettes [i col] (map-indexed vector (plot/palette p))]
                        {:palette (str p) :i i :color col}))
palette i color
:tableau-10 0 [31.0 119.0 180.0 255.0]
:tableau-10 1 [255.0 127.0 14.0 255.0]
:tableau-10 2 [44.0 160.0 44.0 255.0]
:tableau-10 3 [214.0 39.0 40.0 255.0]
:tableau-10 4 [148.0 103.0 189.0 255.0]
:tableau-10 5 [140.0 86.0 75.0 255.0]
:tableau-10 6 [227.0 119.0 194.0 255.0]
:tableau-10 7 [127.0 127.0 127.0 255.0]
:tableau-10 8 [188.0 189.0 34.0 255.0]
:tableau-10 9 [23.0 190.0 207.0 255.0]
:rcartocolor/Bold 0 [127.0 60.0 141.0 255.0]
:rcartocolor/Bold 1 [17.0 165.0 121.0 255.0]
:rcartocolor/Bold 2 [57.0 105.0 172.0 255.0]
:rcartocolor/Bold 3 [242.0 183.0 1.0 255.0]
:rcartocolor/Bold 4 [231.0 63.0 116.0 255.0]
:rcartocolor/Bold 5 [128.0 186.0 90.0 255.0]
:rcartocolor/Bold 6 [230.0 131.0 16.0 255.0]
:rcartocolor/Bold 7 [0.0 134.0 149.0 255.0]
:rcartocolor/Bold 8 [207.0 28.0 144.0 255.0]
:rcartocolor/Bold 9 [249.0 123.0 114.0 255.0]
:rcartocolor/Bold 10 [75.0 75.0 143.0 255.0]
:rcartocolor/Bold 11 [165.0 170.0 153.0 255.0]
:rcartocolor/Pastel 0 [102.0 197.0 204.0 255.0]
:rcartocolor/Pastel 1 [246.0 207.0 113.0 255.0]
:rcartocolor/Pastel 2 [248.0 156.0 116.0 255.0]
:rcartocolor/Pastel 3 [220.0 176.0 242.0 255.0]
:rcartocolor/Pastel 4 [135.0 197.0 95.0 255.0]
:rcartocolor/Pastel 5 [158.0 185.0 243.0 255.0]
:rcartocolor/Pastel 6 [254.0 136.0 177.0 255.0]
:rcartocolor/Pastel 7 [201.0 219.0 116.0 255.0]
:rcartocolor/Pastel 8 [139.0 224.0 164.0 255.0]
:rcartocolor/Pastel 9 [180.0 151.0 231.0 255.0]
:rcartocolor/Pastel 10 [211.0 180.0 132.0 255.0]
:rcartocolor/Pastel 11 [179.0 179.0 179.0 255.0]
:rcartocolor/Prism 0 [95.0 70.0 144.0 255.0]
:rcartocolor/Prism 1 [29.0 105.0 150.0 255.0]
:rcartocolor/Prism 2 [56.0 166.0 165.0 255.0]
:rcartocolor/Prism 3 [15.0 133.0 84.0 255.0]
:rcartocolor/Prism 4 [115.0 175.0 72.0 255.0]
:rcartocolor/Prism 5 [237.0 173.0 8.0 255.0]
:rcartocolor/Prism 6 [225.0 124.0 5.0 255.0]
:rcartocolor/Prism 7 [204.0 80.0 62.0 255.0]
:rcartocolor/Prism 8 [148.0 52.0 110.0 255.0]
:rcartocolor/Prism 9 [111.0 64.0 112.0 255.0]
:rcartocolor/Prism 10 [153.0 78.0 149.0 255.0]
:rcartocolor/Prism 11 [102.0 102.0 102.0 255.0]
:rcartocolor/Vivid 0 [229.0 134.0 6.0 255.0]
:rcartocolor/Vivid 1 [93.0 105.0 177.0 255.0]
:rcartocolor/Vivid 2 [82.0 188.0 163.0 255.0]
:rcartocolor/Vivid 3 [153.0 201.0 69.0 255.0]
:rcartocolor/Vivid 4 [204.0 97.0 176.0 255.0]
:rcartocolor/Vivid 5 [36.0 121.0 108.0 255.0]
:rcartocolor/Vivid 6 [218.0 165.0 27.0 255.0]
:rcartocolor/Vivid 7 [47.0 138.0 196.0 255.0]
:rcartocolor/Vivid 8 [118.0 78.0 159.0 255.0]
:rcartocolor/Vivid 9 [237.0 100.0 90.0 255.0]
:rcartocolor/Vivid 10 [204.0 58.0 142.0 255.0]
:rcartocolor/Vivid 11 [165.0 170.0 153.0 255.0]
(plot/render-image {:data palette-swatches
                    :mapping {:x :i :y :palette :color :color}
                    :charts [(chart/tile)]
                    :scales {:color {:kind :identity :legend false}
                             :y {:reverse true}
                             :x {:ticks 12}}}
                   {:width 700 :height 280})

Gradients, each sampled at 32 points:

(def gradients (plot/find-gradient "viridis" #".*"))
(def gradient-swatches (for [g gradients
                             :let [gradient (plot/gradient g)]
                             i (range 32)]
                         {:gradient (str g) :i i :color (gradient (/ i 31.0))}))
gradient i color
:viridis/cividis 0 [0.0 32.0 77.0 255.0]
:viridis/cividis 1 [0.0 38.096774193548384 90.38709677419355 255.0]
:viridis/cividis 2 [0.0 44.193548387096776 105.58064516129032 255.0]
:viridis/cividis 3 [0.0 48.58064516129032 111.0 255.0]
:viridis/cividis 4 [10.09677419354838 54.774193548387096 110.0 255.0]
:viridis/cividis 5 [32.93548387096774 60.96774193548387 108.51612903225806 255.0]
:viridis/cividis 6 [46.74193548387096 66.58064516129032 108.0 255.0]
:viridis/cividis 7 [57.354838709677416 72.6774193548387 107.0 255.0]
:viridis/cividis 8 [66.54838709677419 78.54838709677419 107.0 255.0]
:viridis/cividis 9 [75.74193548387098 83.87096774193549 107.87096774193549 255.0]
:viridis/cividis 10 [83.93548387096774 89.96774193548387 108.0 255.0]
:viridis/cividis 11 [91.12903225806453 96.06451612903226 110.0 255.0]
:viridis/cividis 12 [99.16129032258064 102.16129032258064 111.16129032258064 255.0]
:viridis/cividis 13 [106.51612903225806 108.51612903225806 113.25806451612904 255.0]
:viridis/cividis 14 [114.0 114.06451612903226 116.0 255.0]
:viridis/cividis 15 [120.90322580645162 120.35483870967742 119.0 255.0]
:viridis/cividis 16 [129.09677419354838 126.64516129032259 121.0 255.0]
:viridis/cividis 17 [136.29032258064515 132.93548387096772 121.0 255.0]
:viridis/cividis 18 [144.48387096774195 139.48387096774195 120.25806451612902 255.0]
:viridis/cividis 19 [152.67741935483872 145.83870967741936 120.0 255.0]
:viridis/cividis 20 [160.8709677419355 152.8709677419355 118.06451612903226 255.0]
:viridis/cividis 21 [170.06451612903223 159.06451612903223 116.96774193548389 255.0]
:viridis/cividis 22 [178.25806451612905 166.25806451612905 114.0 255.0]
:viridis/cividis 23 [186.4516129032258 173.4516129032258 111.7741935483871 255.0]
:viridis/cividis 24 [195.6451612903226 180.32258064516128 108.6774193548387 255.0]
:viridis/cividis 25 [204.83870967741933 187.41935483870967 104.58064516129033 255.0]
:viridis/cividis 26 [213.54838709677423 195.03225806451616 99.48387096774192 255.0]
:viridis/cividis 27 [222.83870967741936 202.2258064516129 94.38709677419355 255.0]
:viridis/cividis 28 [232.1290322580645 210.4193548387097 88.29032258064515 255.0]
:viridis/cividis 29 [241.61290322580643 217.61290322580643 80.38709677419357 255.0]
:viridis/cividis 30 [250.80645161290326 225.80645161290326 72.19354838709675 255.0]
:viridis/cividis 31 [255.0 234.0 70.0 255.0]
:viridis/inferno 0 [0.0 0.0 4.0 255.0]
:viridis/inferno 1 [4.096774193548387 3.096774193548387 18.483870967741936 255.0]
:viridis/inferno 2 [11.387096774193548 7.193548387096774 36.96774193548387 255.0]
:viridis/inferno 3 [21.870967741935484 11.290322580645162 56.45161290322581 255.0]
:viridis/inferno 4 [34.16129032258064 12.0 75.93548387096774 255.0]
:viridis/inferno 5 [48.4516129032258 10.0 92.45161290322581 255.0]
:viridis/inferno 6 [62.74193548387096 9.58064516129032 102.16129032258064 255.0]
:viridis/inferno 7 [76.03225806451613 12.677419354838708 107.6774193548387 255.0]
:viridis/inferno 8 [89.54838709677419 16.774193548387096 110.0 255.0]
:viridis/inferno 9 [103.48387096774195 21.870967741935488 110.0 255.0]
:viridis/inferno 10 [115.90322580645162 25.967741935483872 110.0 255.0]
:viridis/inferno 11 [129.1935483870968 31.064516129032263 107.93548387096774 255.0]
:viridis/inferno 12 [142.48387096774195 35.32258064516129 104.83870967741936 255.0]
:viridis/inferno 13 [155.03225806451613 41.0 100.0 255.0]
:viridis/inferno 14 [168.06451612903226 46.0 95.29032258064517 255.0]
:viridis/inferno 15 [180.35483870967744 51.35483870967742 89.09677419354838 255.0]
:viridis/inferno 16 [193.09677419354838 58.0 79.90322580645162 255.0]
:viridis/inferno 17 [204.29032258064515 66.29032258064514 71.70967741935486 255.0]
:viridis/inferno 18 [214.9677419354839 75.22580645161293 63.03225806451609 255.0]
:viridis/inferno 19 [224.67741935483872 85.51612903225806 53.322580645161295 255.0]
:viridis/inferno 20 [232.8709677419355 96.74193548387098 44.12903225806451 255.0]
:viridis/inferno 21 [239.06451612903223 109.09677419354836 33.90322580645164 255.0]
:viridis/inferno 22 [245.1290322580645 122.5161290322581 22.612903225806424 255.0]
:viridis/inferno 23 [248.2258064516129 136.90322580645162 11.548387096774196 255.0]
:viridis/inferno 24 [251.0 152.29032258064518 6.0 255.0]
:viridis/inferno 25 [252.0 167.6774193548387 13.258064516129009 255.0]
:viridis/inferno 26 [251.0 184.06451612903228 28.580645161290374 255.0]
:viridis/inferno 27 [249.38709677419354 199.83870967741936 48.064516129032256 255.0]
:viridis/inferno 28 [245.29032258064515 215.83870967741936 71.25806451612905 255.0]
:viridis/inferno 29 [242.19354838709677 231.41935483870967 99.45161290322575 255.0]
:viridis/inferno 30 [242.90322580645162 244.70967741935488 133.1290322580646 255.0]
:viridis/inferno 31 [252.0 255.0 164.0 255.0]
:viridis/magma 0 [0.0 0.0 4.0 255.0]
:viridis/magma 1 [3.193548387096774 3.096774193548387 18.387096774193548 255.0]
:viridis/magma 2 [10.387096774193548 8.193548387096774 34.774193548387096 255.0]
:viridis/magma 3 [19.580645161290324 13.290322580645162 53.16129032258065 255.0]
:viridis/magma 4 [30.16129032258064 17.0 72.54838709677419 255.0]
:viridis/magma 5 [42.4516129032258 17.0 92.41935483870967 255.0]
:viridis/magma 6 [55.74193548387096 15.41935483870968 108.74193548387096 255.0]
:viridis/magma 7 [70.70967741935483 15.677419354838708 119.35483870967741 255.0]
:viridis/magma 8 [84.09677419354838 18.774193548387096 124.7741935483871 255.0]
:viridis/magma 9 [95.74193548387098 23.870967741935488 127.87096774193549 255.0]
:viridis/magma 10 [108.90322580645162 28.967741935483872 129.0 255.0]
:viridis/magma 11 [122.19354838709678 34.06451612903226 130.0 255.0]
:viridis/magma 12 [135.48387096774195 39.32258064516129 129.0 255.0]
:viridis/magma 13 [148.03225806451613 43.25806451612903 128.0 255.0]
:viridis/magma 14 [161.41935483870967 47.354838709677416 126.64516129032258 255.0]
:viridis/magma 15 [175.35483870967744 52.45161290322581 123.54838709677419 255.0]
:viridis/magma 16 [188.6451612903226 57.54838709677419 119.45161290322581 255.0]
:viridis/magma 17 [201.93548387096772 62.0 113.70967741935486 255.0]
:viridis/magma 18 [214.22580645161293 68.74193548387098 108.25806451612902 255.0]
:viridis/magma 19 [226.3548387096774 77.51612903225806 101.3225806451613 255.0]
:viridis/magma 20 [235.8709677419355 87.80645161290323 96.06451612903226 255.0]
:viridis/magma 21 [243.03225806451613 101.09677419354836 92.0 255.0]
:viridis/magma 22 [248.1290322580645 115.64516129032263 92.12903225806453 255.0]
:viridis/magma 23 [251.0 130.90322580645162 95.45161290322581 255.0]
:viridis/magma 24 [253.0 147.29032258064518 102.64516129032259 255.0]
:viridis/magma 25 [254.0 162.6774193548387 111.25806451612901 255.0]
:viridis/magma 26 [254.0 178.06451612903228 121.54838709677422 255.0]
:viridis/magma 27 [254.0 192.83870967741936 133.83870967741936 255.0]
:viridis/magma 28 [254.0 207.83870967741936 146.83870967741936 255.0]
:viridis/magma 29 [253.0 223.22580645161287 160.41935483870967 255.0]
:viridis/magma 30 [252.09677419354838 237.70967741935488 175.6129032258065 255.0]
:viridis/magma 31 [252.0 253.0 191.0 255.0]
:viridis/mako 0 [11.0 4.0 5.0 255.0]
:viridis/mako 1 [20.193548387096776 9.193548387096774 16.29032258064516 255.0]
:viridis/mako 2 [28.387096774193548 16.193548387096776 28.580645161290324 255.0]
:viridis/mako 3 [36.58064516129032 22.29032258064516 40.87096774193548 255.0]
… … …
(plot/render-image {:data gradient-swatches
                    :mapping {:x :i :y :gradient :color :color}
                    :charts [(chart/tile {:stroke-color :none})]
                    :scales {:color {:kind :identity :legend false}
                             :y {:reverse true}
                             :x {:axis false}}}
                   {:width 700 :height 300})

4.3 Placing values on the range

How values spread over the attributes: a diverging gradient around :midpoint, values placed by a transform, the range reversed, size by area.

Option Type Default Description
:spread true or false false ordinal :color: the range resampled to one colour per level
:midpoint a finite number linear / binned :color: the value at the gradient's middle (a diverging scale)
:transform one of :exp :log :sq :sqrt or a function linear :color: values placed along the gradient by their square root, logarithm, square, exponential or a monotonic function
:reverse true or false false the attributes in reverse order
:area true or false false :size: the mark's area, not its size, grows linearly with the value
(plot/render-image (plot/arrange [{:data gapminder
                                   :mapping {:x :gdp-per-cap :y :life-exp :color :life-exp}
                                   :charts [(chart/scatter)]
                                   :scales {:x {:kind :log :formatter :power}
                                            :color {:range [:steelblue :white :firebrick] :midpoint 65
                                                    :title "life-exp, midpoint 65"}}}
                                  {:data gapminder
                                   :mapping {:x :gdp-per-cap :y :life-exp :color :pop}
                                   :charts [(chart/scatter)]
                                   :scales {:x {:kind :log :formatter :power}
                                            :color {:transform :log :range :viridis/viridis :reverse true
                                                    :title "pop, log, reversed"}}}]
                                 {:columns 2})
                   {:width 900 :height 350})

:transform places values along the gradient by :sqrt, :log, :sq, :exp or a function of the value; the function must be monotonic over the domain (the colour bar inverts it numerically) and finite at its ends. The values 1 to 20 under each (:exp - the values 0.1 to 2, the exponential of 20 dwarfs everything):

(def ramp (for [i (range 1 21)] {:i i :tenth (* 0.1 i) :row "value"}))
i tenth row
1 0.1 value
2 0.2 value
3 0.30000000000000004 value
4 0.4 value
5 0.5 value
6 0.6000000000000001 value
7 0.7000000000000001 value
8 0.8 value
9 0.9 value
10 1.0 value
11 1.1 value
12 1.2000000000000002 value
13 1.3 value
14 1.4000000000000001 value
15 1.5 value
16 1.6 value
17 1.7000000000000002 value
18 1.8 value
19 1.9000000000000001 value
20 2.0 value
(plot/render-image (plot/arrange (for [[title transform] [["none" nil] [":sqrt" :sqrt] [":log" :log]
                                                         [":sq" :sq] [":exp" :exp] ["cube root (fn)" #(Math/cbrt %)]]]
                                   {:data ramp
                                    :mapping {:x :i :y :row :color (if (= :exp transform) :tenth :i)}
                                    :charts [(chart/tile)]
                                    :scales (cond-> {:color {:title nil}} transform (assoc-in [:color :transform] transform))
                                    :labels {:title title :y nil}})
                                 {:columns 3})
                   {:width 900 :height 330})

:area true grows a size scale’s marks by area, not diameter - the right one reads as proportional:

(plot/render-image (plot/arrange (for [area [false true]]
                                   {:data ramp
                                    :mapping {:x :i :y :row :size :i}
                                    :charts [(chart/scatter)]
                                    :scales {:size {:range [2 26] :area area :legend false}}
                                    :labels {:title (str ":area " area) :y nil}})
                                 {:columns 2})
                   {:width 900 :height 200})

4.4 Bins

A :binned colour scale cuts numbers into classes and draws a key per class. :bins takes a count of equal bins, the breaks themselves, a binning method (:sturges, :sqrt, :rice, :doane, :scott …) or quantile bins:

Option Type Default Description
:bins keyword or vector of a finite number (at least 2) or quantile bins {:quantiles q :estimation e}: q a positive count or a vector of increasing probabilities strictly between 0 and 1, e one of [:legacy :r1 :r2 :r3 :r4 :r5 :r6 :r7 :r8 :r9] or a positive integer 5 equal bins binned :color: a bin count, breaks, a bin method or quantile bins
(plot/render-image (plot/arrange (for [[title bins] [["3 equal bins" 3] ["breaks [39 50 60 70 83]" [39 50 60 70 83]]
                                                    ["method :sturges" :sturges] ["{:quantiles 4}" {:quantiles 4}]]]
                                   {:data gapminder
                                    :mapping {:x :gdp-per-cap :y :life-exp :color :life-exp}
                                    :charts [(chart/scatter)]
                                    :scales {:x {:kind :log :formatter :power}
                                             :color {:kind :binned :bins bins :range :viridis/viridis :title nil}}
                                    :labels {:title title}})
                                 {:columns 2})
                   {:width 900 :height 600})

4.5 Legend

One legend per mapped aesthetic, titled by the mapped column’s name: :title renames it, :formatter writes its labels, :ticks picks the entries of a numeric scale (:integer-ticks keeps counted ones at whole numbers), :legend false hides it (the plot’s :legend false hides them all). Aesthetics mapped to the same column with equal levels share one legend. Legends sit right of the panels; their sizes and frame are in the theme :legend section (Theme chapter).

Option Type Default Description
:formatter a function legend labels, a function of the level or value
:ticks sequential of a finite number or a java.time value or a positive integer or nil linear :size / :color: the legend entries or colour bar ticks - a count or the values (dates for a :color scale over dates)
:integer-ticks true or false false linear :size / :color: counted legend entries or colour bar ticks only at whole numbers (an explicit :ticks vector or :midpoint tick is drawn as given)
:title string or nil the mapped column's name legend title (nil - none)
:legend true or false or nil true show the scale's legend
(plot/render-image {:data gapminder
                    :mapping {:x :gdp-per-cap :y :life-exp :color :continent :shape :continent :size :pop}
                    :charts [(chart/scatter)]
                    :scales {:x {:kind :log :formatter :power}
                             :color {:title "continent"}
                             :size {:title "population" :ticks [1e7 1e8 1e9]
                                    :formatter #(format "%.0f M" (/ (double %) 1e6))}}}
                   {:width 700 :height 380})

A colour bar takes :ticks and :formatter too:

(plot/render-image {:data gapminder
                    :mapping {:x :gdp-per-cap :y :life-exp :color :life-exp}
                    :charts [(chart/scatter)]
                    :scales {:x {:kind :log :formatter :power}
                             :color {:ticks [45 55 65 75] :formatter #(str % " years") :title "life expectancy"}}}
                   {:width 600 :height 350})

Data: Motor Trend car road tests (1974), R datasets package mtcars (Henderson & Velleman 1981); licence: Part of R (GPL-2 | GPL-3).

(def mtcars (nu/dataset :mtcars))
model mpg cyl disp hp drat wt qsec vs am gear carb
Mazda RX4 21 6 160 110 3.9 2.62 16.46 0 1 4 4
Mazda RX4 Wag 21 6 160 110 3.9 2.875 17.02 0 1 4 4
Datsun 710 22.8 4 108 93 3.85 2.32 18.61 1 1 4 1
Hornet 4 Drive 21.4 6 258 110 3.08 3.215 19.44 1 0 3 1
Hornet Sportabout 18.7 8 360 175 3.15 3.44 17.02 0 0 3 2
Valiant 18.1 6 225 105 2.76 3.46 20.22 1 0 3 1
Duster 360 14.3 8 360 245 3.21 3.57 15.84 0 0 3 4
Merc 240D 24.4 4 146.7 62 3.69 3.19 20 1 0 4 2
Merc 230 22.8 4 140.8 95 3.92 3.15 22.9 1 0 4 2
Merc 280 19.2 6 167.6 123 3.92 3.44 18.3 1 0 4 4
Merc 280C 17.8 6 167.6 123 3.92 3.44 18.9 1 0 4 4
Merc 450SE 16.4 8 275.8 180 3.07 4.07 17.4 0 0 3 3
Merc 450SL 17.3 8 275.8 180 3.07 3.73 17.6 0 0 3 3
Merc 450SLC 15.2 8 275.8 180 3.07 3.78 18 0 0 3 3
Cadillac Fleetwood 10.4 8 472 205 2.93 5.25 17.98 0 0 3 4
Lincoln Continental 10.4 8 460 215 3 5.424 17.82 0 0 3 4
Chrysler Imperial 14.7 8 440 230 3.23 5.345 17.42 0 0 3 4
Fiat 128 32.4 4 78.7 66 4.08 2.2 19.47 1 1 4 1
Honda Civic 30.4 4 75.7 52 4.93 1.615 18.52 1 1 4 2
Toyota Corolla 33.9 4 71.1 65 4.22 1.835 19.9 1 1 4 1
Toyota Corona 21.5 4 120.1 97 3.7 2.465 20.01 1 0 3 1
Dodge Challenger 15.5 8 318 150 2.76 3.52 16.87 0 0 3 2
AMC Javelin 15.2 8 304 150 3.15 3.435 17.3 0 0 3 2
Camaro Z28 13.3 8 350 245 3.73 3.84 15.41 0 0 3 4
Pontiac Firebird 19.2 8 400 175 3.08 3.845 17.05 0 0 3 2
Fiat X1-9 27.3 4 79 66 4.08 1.935 18.9 1 1 4 1
Porsche 914-2 26 4 120.3 91 4.43 2.14 16.7 0 1 5 2
Lotus Europa 30.4 4 95.1 113 3.77 1.513 16.9 1 1 5 2
Ford Pantera L 15.8 8 351 264 4.22 3.17 14.5 0 1 5 4
Ferrari Dino 19.7 6 145 175 3.62 2.77 15.5 0 1 5 6
Maserati Bora 15 8 301 335 3.54 3.57 14.6 0 1 5 8
Volvo 142E 21.4 4 121 109 4.11 2.78 18.6 1 1 4 2

Whole-number data over a short range - here 3 to 5 gears (size) and the 0 / 1 engine shape vs (colour) - gets fractional counted entries (left); :integer-ticks true keeps only whole ones, on a size legend and a colour bar alike (right):

(plot/render-image (plot/arrange [{:data mtcars
                                   :mapping {:x :wt :y :mpg :size :gear :color :vs}
                                   :charts [(chart/scatter)]
                                   :scales {:color {:range [[160 200 235] [8 48 107]]}}
                                   :labels {:title "counted ticks"}}
                                  {:data mtcars
                                   :mapping {:x :wt :y :mpg :size :gear :color :vs}
                                   :charts [(chart/scatter)]
                                   :scales {:size {:integer-ticks true} :color {:integer-ticks true :range [[160 200 235] [8 48 107]]}}
                                   :labels {:title ":integer-ticks true"}}]
                                 {:columns 2})
                   {:width 1000 :height 360})

4.6 Shape

Point shapes: characters - filled and outlined circles, squares, triangles, strokes, plus and cross - or shape maps (an SVG path, a text, an SVG file, an image).

Option Type Default Description
:range map of any value to a shape character, keyword or shape map or sequential of a shape character, keyword or shape map (at least 1) theme :palette attributes the domain maps to: palette, gradient, shapes, sizes [min max], dashes or patterns, or a level map

Every built-in shape (describe/attribute-values lists the built-in shapes, dashes, patterns and cells; an :identity scale draws the data’s shapes):

(def shapes (map-indexed (fn [i s] {:i i :shape s :name (str s)}) (:shape describe/attribute-values)))
i shape name
0 \+ +
1 \- -
2 \/ /
3 \< <
4 \> >
5 \A A
6 \O O
7 \S S
8 \V V
9 \\ \
10 \^ ^
11 \o o
12 \s s
13 \v v
14 \x x
15 \{ {
16 \| |
17 \} }
(plot/render-image {:data shapes
                    :mapping {:x :i :y (constantly 1) :shape :shape}
                    :charts [(chart/scatter {:size 18 :color [69 117 180]})
                             (chart/text {:mapping {:x :i :y (constantly 0.4) :label :name}})]
                    :scales {:shape {:kind :identity :legend false} :y {:domain [0 1.5] :axis false}}}
                   {:width 800 :height 200})

A shape range of characters and shape maps:

(def groups (for [[gi g] (map-indexed vector ["a" "b" "c"]) i (range 8)]
              {:g g :x i :y (+ (* 3 gi) (mod (* i 7) 3))}))
g x y
a 0 0
a 1 1
a 2 2
a 3 0
a 4 1
a 5 2
a 6 0
a 7 1
b 0 3
b 1 4
b 2 5
b 3 3
b 4 4
b 5 5
b 6 3
b 7 4
c 0 6
c 1 7
c 2 8
c 3 6
c 4 7
c 5 8
c 6 6
c 7 7
(plot/render-image {:data groups
                    :mapping {:x :x :y :y :shape :g}
                    :charts [(chart/scatter {:size 14})]
                    :scales {:shape {:range [\s {:path "M0 0 L10 0 L5 9 Z"} {:text "★"}]}}}
                   {:width 500 :height 280})

4.7 Size

Sizes [min max] in pixels for numbers (a level map or a list for levels).

Option Type Default Description
:range map of any value to a non-negative number or [min max], non-negative numbers, min <= max theme :palette attributes the domain maps to: palette, gradient, shapes, sizes [min max], dashes or patterns, or a level map
(plot/render-image {:data gapminder
                    :mapping {:x :gdp-per-cap :y :life-exp :size :pop}
                    :charts [(chart/scatter)]
                    :scales {:x {:kind :log :formatter :power}
                             :size {:range [4 50] :area true :legend false}}}
                   {:width 600 :height 350})

4.8 Dash

Line dashes: a named style or a dash array - lengths in pixels of a dash, a gap, a dash, … repeated along the line.

Option Type Default Description
:range map of any value to :none, a dash style (:dashed :dot-dash :dot-dot-dash :dotted :long-dash :loose-dot :solid :two-dash) or a sequence of positive numbers or sequential of :none, a dash style (:dashed :dot-dash :dot-dot-dash :dotted :long-dash :loose-dot :solid :two-dash) or a sequence of positive numbers (at least 1) theme :palette attributes the domain maps to: palette, gradient, shapes, sizes [min max], dashes or patterns, or a level map

Every named style, and two dash arrays (an :identity scale draws the data’s dashes):

(def dashes (concat (for [[i d] (map-indexed vector (:dash describe/attribute-values)) x [0 10]]
                      {:i i :x x :dash d :name (str d)})
                    (for [[i d] [[8 [12 4]] [9 [2 2 8 2]]] x [0 10]]
                      {:i i :x x :dash d :name (pr-str d)})))
i x dash name
0 0 :dashed :dashed
0 10 :dashed :dashed
1 0 :dot-dash :dot-dash
1 10 :dot-dash :dot-dash
2 0 :dot-dot-dash :dot-dot-dash
2 10 :dot-dot-dash :dot-dot-dash
3 0 :dotted :dotted
3 10 :dotted :dotted
4 0 :long-dash :long-dash
4 10 :long-dash :long-dash
5 0 :loose-dot :loose-dot
5 10 :loose-dot :loose-dot
6 0 :solid :solid
6 10 :solid :solid
7 0 :two-dash :two-dash
7 10 :two-dash :two-dash
8 0 [12 4] [12 4]
8 10 [12 4] [12 4]
9 0 [2 2 8 2] [2 2 8 2]
9 10 [2 2 8 2] [2 2 8 2]
(plot/render-image {:data dashes
                    :mapping {:x :x :y :i :dash :dash :group :i}
                    :charts [(chart/line {:width 2 :color :black})
                             (chart/text {:data (filter #(zero? (long (:x %))) dashes)
                                          :mapping {:x :x :y :i :label :name} :anchor [:right :middle] :offset [-8 0]})]
                    :scales {:dash {:kind :identity :legend false}
                             :x {:domain [-6 10] :axis false} :y {:axis false :reverse true}}}
                   {:width 600 :height 380})

A dash range by group:

(plot/render-image {:data groups
                    :mapping {:x :x :y :y :dash :g}
                    :charts [(chart/line {:width 2})]
                    :scales {:dash {:range [:solid :dotted [8 3 2 3]]}}}
                   {:width 500 :height 280})

4.9 Pattern

Fill patterns of areas and bars: a named pattern or your own as a pattern map - lines or crosses {:kind :lines :angle a :spacing s :width w} (a 0, 45, 90 or 135) or dots {:kind :dots :spacing s :size d}.

Option Type Default Description
:range map of any value to a pattern name (:back-hatch :cross :dense-dots :diagonal-cross :dots :hatch :horizontal :solid :vertical) or a pattern map or sequential of a pattern name (:back-hatch :cross :dense-dots :diagonal-cross :dots :hatch :horizontal :solid :vertical) or a pattern map (at least 1) theme :palette attributes the domain maps to: palette, gradient, shapes, sizes [min max], dashes or patterns, or a level map

Every named pattern and two pattern maps:

(def patterns (concat (for [p (:pattern describe/attribute-values)] {:name (name p) :pattern p :v 1})
                      [{:name "lines 135, spacing 8" :pattern {:kind :lines :angle 135 :spacing 8 :width 2} :v 1}
                       {:name "dots, spacing 5" :pattern {:kind :dots :spacing 5 :size 3} :v 1}]))
name pattern v
back-hatch :back-hatch 1
cross :cross 1
dense-dots :dense-dots 1
diagonal-cross :diagonal-cross 1
dots :dots 1
hatch :hatch 1
horizontal :horizontal 1
solid :solid 1
vertical :vertical 1
lines 135, spacing 8 {:kind :lines, :angle 135, :spacing 8, :width 2} 1
dots, spacing 5 {:kind :dots, :spacing 5, :size 3} 1
(plot/render-image {:data patterns
                    :mapping {:x :name :y :v :pattern :pattern}
                    :charts [(chart/bar {:color :white :stroke-color :black :pattern-color :black :width 0.9})]
                    :scales {:pattern {:kind :identity :legend false} :y {:axis false}}
                    :theme {:axis {:x {:label-rotation 45}}}}
                   {:width 800 :height 320})

source: notebooks/reference/aesthetics_legends.clj