32  Evolution

Values over time and other ordered variables: lines, time axes, smoothing and uncertainty, many lines, areas and marks for evolution (the R Graph Gallery evolution pages and the data-to-viz caveats).

Dates are java.time values: a datetime scale places LocalDate, LocalDateTime, Instant and the other temporal types; curves and fits work in epoch milliseconds.

(ns use-cases.evolution
  (:require [fastmath.core :as m]
            [fastmath.kernel :as kernel]
            [fastmath.random :as r]
            [cljplot2.chart :as chart]
            [cljplot2.core :as plot]
            [cljplot2.data.levels :as levels]
            [notebook-utils :as nu])
  (:import [java.time LocalDate]
           [java.time.format DateTimeFormatter]))
(defn- with-dates
  "Rows with the ISO string `:date` parsed to a `LocalDate`."
  [rows]
  (mapv #(update % :date (fn [d] (LocalDate/parse d))) rows))
#'use-cases.evolution/with-dates
(def economics (with-dates (nu/dataset :economics)))
date pce pop psavert uempmed unemploy
1967-07-01 506.7 198712 12.6 4.5 2944
1967-08-01 509.8 198911 12.6 4.7 2945
1967-09-01 515.6 199113 11.9 4.6 2958
1967-10-01 512.2 199311 12.9 4.9 3143
1967-11-01 517.4 199498 12.8 4.7 3066
1967-12-01 525.1 199657 11.8 4.8 3018
1968-01-01 530.9 199808 11.7 5.1 2878
1968-02-01 533.6 199920 12.3 4.5 3001
1968-03-01 544.3 200056 11.7 4.1 2877
1968-04-01 544 200208 12.3 4.6 2709
1968-05-01 549.8 200361 12 4.4 2740
1968-06-01 556.3 200536 11.7 4.4 2938
1968-07-01 563.2 200706 10.7 4.5 2883
1968-08-01 567 200898 10.5 4.2 2768
1968-09-01 568.2 201095 10.6 4.6 2686
1968-10-01 571.6 201290 10.8 4.8 2689
1968-11-01 576.7 201466 10.6 4.4 2715
1968-12-01 576.5 201621 11.1 4.4 2685
1969-01-01 583.5 201760 10.3 4.4 2718
1969-02-01 588.7 201881 9.7 4.9 2692
1969-03-01 588.9 202023 10.2 4 2712
1969-04-01 593.9 202161 9.7 4 2758
1969-05-01 600.3 202331 10.1 4.2 2713
1969-06-01 600.9 202507 11.1 4.4 2816
1969-07-01 602.7 202677 11.8 4.4 2868
1969-08-01 609.9 202877 11.5 4.4 2856
1969-09-01 613.2 203090 11.6 4.7 3040
1969-10-01 618.5 203302 11.4 4.5 3049
1969-11-01 620.5 203500 11.6 4.8 2856
1969-12-01 622.8 203675 11.8 4.6 2884
1970-01-01 628.7 203849 11.8 4.6 3201
1970-02-01 634 204008 11.7 4.5 3453
1970-03-01 632.3 204156 12.4 4.6 3635
1970-04-01 636 204401 13.3 4.1 3797
1970-05-01 642.4 204607 12.4 4.7 3919
1970-06-01 646.3 204830 12.3 4.9 4071
1970-07-01 648.5 205052 13.5 5.1 4175
1970-08-01 652.9 205295 13.4 5.4 4256
1970-09-01 659.1 205540 12.9 5.2 4456
1970-10-01 658.3 205788 13.1 5.2 4591
1970-11-01 656.6 206024 13.6 5.6 4898
1970-12-01 665.6 206238 13.2 5.9 5076
1971-01-01 676.1 206466 13.3 6.2 4986
1971-02-01 679.4 206668 13.3 6.3 4903
1971-03-01 682 206855 13.5 6.4 4987
1971-04-01 688.8 207065 13.2 6.5 4959
1971-05-01 691.1 207260 13.6 6.7 4996
1971-06-01 699.8 207462 14.7 5.7 4949
1971-07-01 698.9 207661 13.8 6.2 5035
1971-08-01 704.9 207881 13.6 6.4 5134
1971-09-01 713 208114 13.3 5.8 5042
1971-10-01 715.8 208345 13.3 6.5 4954
1971-11-01 720.9 208555 13.1 6.4 5161
1971-12-01 728.4 208740 13 6.2 5154
1972-01-01 731.5 208917 12.5 6.2 5019
1972-02-01 736.2 209061 12.8 6.6 4928
1972-03-01 749.2 209212 11.8 6.6 5038
1972-04-01 752.5 209386 11.5 6.7 4959
1972-05-01 758 209545 11.7 6.6 4922
1972-06-01 761.6 209725 11.7 5.4 4923
1972-07-01 769.9 209896 11.7 6.1 4913
1972-08-01 776.3 210075 12 6 4939
1972-09-01 781.1 210278 12.2 5.6 4849
1972-10-01 794.9 210479 13 5.7 4875
1972-11-01 800.5 210656 13.6 5.7 4602
1972-12-01 806.1 210821 13.7 6.1 4543
1973-01-01 816.5 210985 12.4 5.7 4326
1973-02-01 825.8 211120 12.5 5.2 4452
1973-03-01 832.8 211254 12.7 5.5 4394
1973-04-01 835.7 211420 13.2 5 4459
1973-05-01 841.6 211577 13.2 4.9 4329
1973-06-01 844.3 211746 13.6 5 4363
1973-07-01 854.1 211909 13.2 5.2 4305
1973-08-01 853.3 212092 13.9 4.9 4305
1973-09-01 869.2 212289 13.1 5.4 4350
1973-10-01 868.2 212475 14.4 5.5 4144
1973-11-01 876.9 212634 14.4 5.1 4396
1973-12-01 876.6 212785 14.8 4.7 4489
1974-01-01 884.5 212932 14.3 5 4644
1974-02-01 889.7 213074 14.2 5.1 4731
1974-03-01 901.4 213211 13.4 4.8 4634
1974-04-01 910.8 213361 13.1 5 4618
1974-05-01 922.4 213513 12.8 4.6 4705
1974-06-01 928 213686 12.8 5.3 4927
1974-07-01 937.9 213854 12.8 5.7 5063
1974-08-01 954.8 214042 12.1 5 5022
1974-09-01 955.1 214246 12.9 5.3 5437
1974-10-01 959.2 214451 13.4 5.5 5523
1974-11-01 956.2 214625 13.8 5.2 6140
1974-12-01 961.8 214782 14 5.7 6636
1975-01-01 975.6 214931 13.2 6.3 7501
1975-02-01 989.4 215065 12.5 7.1 7520
1975-03-01 990.6 215198 12.7 7.2 7978
1975-04-01 995 215353 14.2 8.7 8210
1975-05-01 1018.9 215523 17.3 9.4 8433
1975-06-01 1026.8 215768 14.3 8.8 8220
1975-07-01 1039.8 215973 12.6 8.6 8127
1975-08-01 1047 216195 13 9.2 7928
1975-09-01 1054.8 216393 13 9.2 7923
1975-10-01 1060.9 216587 13.4 8.6 7897
… … … … … …
(def air (with-dates (nu/dataset :air-passengers)))
date year month passengers
1949-01-01 1949 Jan 112
1949-02-01 1949 Feb 118
1949-03-01 1949 Mar 132
1949-04-01 1949 Apr 129
1949-05-01 1949 May 121
1949-06-01 1949 Jun 135
1949-07-01 1949 Jul 148
1949-08-01 1949 Aug 148
1949-09-01 1949 Sep 136
1949-10-01 1949 Oct 119
1949-11-01 1949 Nov 104
1949-12-01 1949 Dec 118
1950-01-01 1950 Jan 115
1950-02-01 1950 Feb 126
1950-03-01 1950 Mar 141
1950-04-01 1950 Apr 135
1950-05-01 1950 May 125
1950-06-01 1950 Jun 149
1950-07-01 1950 Jul 170
1950-08-01 1950 Aug 170
1950-09-01 1950 Sep 158
1950-10-01 1950 Oct 133
1950-11-01 1950 Nov 114
1950-12-01 1950 Dec 140
1951-01-01 1951 Jan 145
1951-02-01 1951 Feb 150
1951-03-01 1951 Mar 178
1951-04-01 1951 Apr 163
1951-05-01 1951 May 172
1951-06-01 1951 Jun 178
1951-07-01 1951 Jul 199
1951-08-01 1951 Aug 199
1951-09-01 1951 Sep 184
1951-10-01 1951 Oct 162
1951-11-01 1951 Nov 146
1951-12-01 1951 Dec 166
1952-01-01 1952 Jan 171
1952-02-01 1952 Feb 180
1952-03-01 1952 Mar 193
1952-04-01 1952 Apr 181
1952-05-01 1952 May 183
1952-06-01 1952 Jun 218
1952-07-01 1952 Jul 230
1952-08-01 1952 Aug 242
1952-09-01 1952 Sep 209
1952-10-01 1952 Oct 191
1952-11-01 1952 Nov 172
1952-12-01 1952 Dec 194
1953-01-01 1953 Jan 196
1953-02-01 1953 Feb 196
1953-03-01 1953 Mar 236
1953-04-01 1953 Apr 235
1953-05-01 1953 May 229
1953-06-01 1953 Jun 243
1953-07-01 1953 Jul 264
1953-08-01 1953 Aug 272
1953-09-01 1953 Sep 237
1953-10-01 1953 Oct 211
1953-11-01 1953 Nov 180
1953-12-01 1953 Dec 201
1954-01-01 1954 Jan 204
1954-02-01 1954 Feb 188
1954-03-01 1954 Mar 235
1954-04-01 1954 Apr 227
1954-05-01 1954 May 234
1954-06-01 1954 Jun 264
1954-07-01 1954 Jul 302
1954-08-01 1954 Aug 293
1954-09-01 1954 Sep 259
1954-10-01 1954 Oct 229
1954-11-01 1954 Nov 203
1954-12-01 1954 Dec 229
1955-01-01 1955 Jan 242
1955-02-01 1955 Feb 233
1955-03-01 1955 Mar 267
1955-04-01 1955 Apr 269
1955-05-01 1955 May 270
1955-06-01 1955 Jun 315
1955-07-01 1955 Jul 364
1955-08-01 1955 Aug 347
1955-09-01 1955 Sep 312
1955-10-01 1955 Oct 274
1955-11-01 1955 Nov 237
1955-12-01 1955 Dec 278
1956-01-01 1956 Jan 284
1956-02-01 1956 Feb 277
1956-03-01 1956 Mar 317
1956-04-01 1956 Apr 313
1956-05-01 1956 May 318
1956-06-01 1956 Jun 374
1956-07-01 1956 Jul 413
1956-08-01 1956 Aug 405
1956-09-01 1956 Sep 355
1956-10-01 1956 Oct 306
1956-11-01 1956 Nov 271
1956-12-01 1956 Dec 306
1957-01-01 1957 Jan 315
1957-02-01 1957 Feb 301
1957-03-01 1957 Mar 356
1957-04-01 1957 Apr 348
… … … …
(def chicks (nu/dataset :chick-weight))
weight time chick diet
42 0 1 1
51 2 1 1
59 4 1 1
64 6 1 1
76 8 1 1
93 10 1 1
106 12 1 1
125 14 1 1
149 16 1 1
171 18 1 1
199 20 1 1
205 21 1 1
40 0 2 1
49 2 2 1
58 4 2 1
72 6 2 1
84 8 2 1
103 10 2 1
122 12 2 1
138 14 2 1
162 16 2 1
187 18 2 1
209 20 2 1
215 21 2 1
43 0 3 1
39 2 3 1
55 4 3 1
67 6 3 1
84 8 3 1
99 10 3 1
115 12 3 1
138 14 3 1
163 16 3 1
187 18 3 1
198 20 3 1
202 21 3 1
42 0 4 1
49 2 4 1
56 4 4 1
67 6 4 1
74 8 4 1
87 10 4 1
102 12 4 1
108 14 4 1
136 16 4 1
154 18 4 1
160 20 4 1
157 21 4 1
41 0 5 1
42 2 5 1
48 4 5 1
60 6 5 1
79 8 5 1
106 10 5 1
141 12 5 1
164 14 5 1
197 16 5 1
199 18 5 1
220 20 5 1
223 21 5 1
41 0 6 1
49 2 6 1
59 4 6 1
74 6 6 1
97 8 6 1
124 10 6 1
141 12 6 1
148 14 6 1
155 16 6 1
160 18 6 1
160 20 6 1
157 21 6 1
41 0 7 1
49 2 7 1
57 4 7 1
71 6 7 1
89 8 7 1
112 10 7 1
146 12 7 1
174 14 7 1
218 16 7 1
250 18 7 1
288 20 7 1
305 21 7 1
42 0 8 1
50 2 8 1
61 4 8 1
71 6 8 1
84 8 8 1
93 10 8 1
110 12 8 1
116 14 8 1
126 16 8 1
134 18 8 1
125 20 8 1
42 0 9 1
51 2 9 1
59 4 9 1
68 6 9 1
85 8 9 1
… … … …
(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
(def lincoln
  "Lincoln weather with `:date` parsed (`2016-1-1` - not ISO)."
  (let [f (DateTimeFormatter/ofPattern "yyyy-M-d")]
    (mapv #(update % :date (fn [d] (LocalDate/parse d f))) (nu/dataset :lincoln-weather))))
date month mean-temp max-temp min-temp
2016-01-01 January 24 37 11
2016-01-02 January 23 41 5
2016-01-03 January 23 37 8
2016-01-04 January 17 30 4
2016-01-05 January 29 38 19
2016-01-06 January 33 34 32
2016-01-07 January 30 33 27
2016-01-08 January 25 28 22
2016-01-09 January 9 22 -4
2016-01-10 January 11 31 -9
2016-01-11 January 28 37 18
2016-01-12 January 24 34 14
2016-01-13 January 33 47 19
2016-01-14 January 40 51 28
2016-01-15 January 29 35 23
2016-01-16 January 19 30 7
2016-01-17 January 5 15 -5
2016-01-18 January 11 26 -4
2016-01-19 January 22 25 19
2016-01-20 January 28 32 24
2016-01-21 January 25 29 21
2016-01-22 January 22 25 19
2016-01-23 January 28 36 20
2016-01-24 January 30 41 19
2016-01-25 January 26 30 22
2016-01-26 January 29 42 15
2016-01-27 January 33 49 17
2016-01-28 January 41 51 31
2016-01-29 January 41 54 27
2016-01-30 January 39 49 28
2016-01-31 January 35 47 22
2016-02-01 February 31 42 20
2016-02-02 February 27 32 21
2016-02-03 February 17 27 6
2016-02-04 February 18 33 2
2016-02-05 February 28 39 17
2016-02-06 February 24 39 10
2016-02-07 February 36 42 30
2016-02-08 February 31 36 25
2016-02-09 February 26 29 22
2016-02-10 February 37 56 17
2016-02-11 February 32 38 26
2016-02-12 February 23 29 17
2016-02-13 February 19 23 15
2016-02-14 February 35 49 20
2016-02-15 February 36 49 23
2016-02-16 February 41 47 34
2016-02-17 February 41 57 25
2016-02-18 February 48 62 33
2016-02-19 February 52 67 37
2016-02-20 February 42 58 26
2016-02-21 February 40 50 29
2016-02-22 February 35 47 23
2016-02-23 February 36 48 23
2016-02-24 February 35 50 20
2016-02-25 February 31 40 22
2016-02-26 February 39 58 19
2016-02-27 February 50 75 24
2016-02-28 February 44 58 29
2016-02-29 February 34 50 18
2016-03-01 March 32 48 16
2016-03-02 March 39 54 24
2016-03-03 March 38 47 29
2016-03-04 March 45 63 27
2016-03-05 March 45 70 20
2016-03-06 March 56 72 39
2016-03-07 March 55 79 30
2016-03-08 March 45 55 34
2016-03-09 March 43 65 20
2016-03-10 March 46 63 28
2016-03-11 March 50 73 27
2016-03-12 March 46 64 28
2016-03-13 March 55 71 39
2016-03-14 March 53 75 30
2016-03-15 March 45 57 33
2016-03-16 March 40 54 25
2016-03-17 March 37 54 20
2016-03-18 March 31 44 18
2016-03-19 March 30 38 21
2016-03-20 March 33 50 15
2016-03-21 March 55 79 30
2016-03-22 March 58 71 44
2016-03-23 March 40 50 29
2016-03-24 March 30 41 18
2016-03-25 March 38 55 21
2016-03-26 March 31 38 23
2016-03-27 March 37 58 15
2016-03-28 March 45 66 23
2016-03-29 March 50 58 41
2016-03-30 March 47 56 38
2016-03-31 March 39 45 33
2016-04-01 April 35 42 28
2016-04-02 April 48 63 33
2016-04-03 April 60 78 41
2016-04-04 April 49 61 37
2016-04-05 April 56 70 42
2016-04-06 April 52 65 39
2016-04-07 April 45 58 31
2016-04-08 April 42 59 25
2016-04-09 April 46 65 26
… … … … …
(def economics-long
  "Economics in long form - `:variable`, `:value` and `:value01` (the value rescaled to 0..1 per variable), as
  ggplot2's `economics_long`."
  (vec (for [k [:pce :pop :psavert :uempmed :unemploy]
             :let [vs (map k economics) lo (double (reduce min vs)) hi (double (reduce max vs))]
             row economics]
         {:date (:date row) :variable (name k) :value (k row) :value01 (/ (- (double (k row)) lo) (- hi lo))})))
date variable value value01
1967-07-01 pce 506.7 0.0
1967-08-01 pce 509.8 2.652497197765077E-4
1967-09-01 pce 515.6 7.615233890357775E-4
1967-10-01 pce 512.2 4.7060434153896666E-4
1967-11-01 pce 517.4 9.155393553576157E-4
1967-12-01 pce 525.1 0.001574385433512166
1968-01-01 pce 530.9 0.002070659102771431
1968-02-01 pce 533.6 0.002301683052254198
1968-03-01 pce 544.3 0.003217222407611809
1968-04-01 pce 544 0.0031915530798915055
1968-05-01 pce 549.8 0.0036878267491507704
1968-06-01 pce 556.3 0.004243995516424089
1968-07-01 pce 563.2 0.004834390053991158
1968-08-01 pce 567 0.00515953487178171
1968-09-01 pce 568.2 0.005262212182662942
1968-10-01 pce 571.6 0.005553131230159753
1968-11-01 pce 576.7 0.005989509801404973
1968-12-01 pce 576.5 0.0059723969162580985
1969-01-01 pce 583.5 0.006571347896398595
1969-02-01 pce 588.7 0.0070162829102172536
1969-03-01 pce 588.9 0.00703339579536412
1969-04-01 pce 593.9 0.0074612179240359025
1969-05-01 pce 600.3 0.008008830248735783
1969-06-01 pce 600.9 0.0080601689041764
1969-07-01 pce 602.7 0.008214184870498248
1969-08-01 pce 609.9 0.00883024873578561
1969-09-01 pce 613.2 0.009112611340708994
1969-10-01 pce 618.5 0.00956610279710108
1969-11-01 pce 620.5 0.009737231648569792
1969-12-01 pce 622.8 0.00993402982775881
1970-01-01 pce 628.7 0.010438859939591521
1970-02-01 pce 634 0.010892351395983609
1970-03-01 pce 632.3 0.010746891872235199
1970-04-01 pce 636 0.011063480247452321
1970-05-01 pce 642.4 0.011611092572152202
1970-06-01 pce 646.3 0.011944793832516192
1970-07-01 pce 648.5 0.01213303556913178
1970-08-01 pce 652.9 0.012509519042362948
1970-09-01 pce 659.1 0.013040018481915962
1970-10-01 pce 658.3 0.012971566941328472
1970-11-01 pce 656.6 0.012826107417580071
1970-12-01 pce 665.6 0.013596187249189281
1971-01-01 pce 676.1 0.014494613719400027
1971-02-01 pce 679.4 0.0147769763243234
1971-03-01 pce 682 0.01499944383123273
1971-04-01 pce 688.8 0.015581281926226351
1971-05-01 pce 691.1 0.015778080105415378
1971-06-01 pce 699.8 0.016522490609304274
1971-07-01 pce 698.9 0.016445482626143357
1971-08-01 pce 704.9 0.016958869180549497
1971-09-01 pce 713 0.017651941028997788
1971-10-01 pce 715.8 0.01789152142105398
1971-11-01 pce 720.9 0.018327899992299203
1971-12-01 pce 728.4 0.018969633185306877
1972-01-01 pce 731.5 0.019234882905083385
1972-02-01 pce 736.2 0.019637035706034867
1972-03-01 pce 749.2 0.020749373240581504
1972-04-01 pce 752.5 0.021031735845504877
1972-05-01 pce 758 0.021502340187043837
1972-06-01 pce 761.6 0.021810372119687525
1972-07-01 pce 769.9 0.02252055685328268
1972-08-01 pce 776.3 0.023068169177982562
1972-09-01 pce 781.1 0.02347887842150748
1972-10-01 pce 794.9 0.0246596674966416
1972-11-01 pce 800.5 0.025138828280754
1972-12-01 pce 806.1 0.025617989064866398
1973-01-01 pce 816.5 0.026507859092503704
1973-02-01 pce 825.8 0.027303608251833218
1973-03-01 pce 832.8 0.027902559231973715
1973-04-01 pce 835.7 0.028150696066603358
1973-05-01 pce 841.6 0.028655526178436058
1973-06-01 pce 844.3 0.02888655012791882
1973-07-01 pce 854.1 0.02972508150011552
1973-08-01 pce 853.3 0.029656629959528027
1973-09-01 pce 869.2 0.031017104328704307
1973-10-01 pce 868.2 0.03093153990296995
1973-11-01 pce 876.9 0.03167595040685885
1973-12-01 pce 876.6 0.031650281079138545
1974-01-01 pce 884.5 0.03232624004243996
1974-02-01 pce 889.7 0.032771175056258615
1974-03-01 pce 901.4 0.03377227883735059
1974-04-01 pce 910.8 0.03457658443925354
1974-05-01 pce 922.4 0.03556913177777208
1974-06-01 pce 928 0.03604829256188448
1974-07-01 pce 937.9 0.03689538037665461
1974-08-01 pce 954.8 0.03834141917156523
1974-09-01 pce 955.1 0.038367088499285544
1974-10-01 pce 959.2 0.03871790264479641
1974-11-01 pce 956.2 0.03846120936759334
1974-12-01 pce 961.8 0.038940370151705726
1975-01-01 pce 975.6 0.040121159226839856
1975-02-01 pce 989.4 0.04130194830197398
1975-03-01 pce 990.6 0.041404625612855205
1975-04-01 pce 995 0.04178110908608638
1975-05-01 pce 1018.9 0.0438260988611375
1975-06-01 pce 1026.8 0.04450205782443891
1975-07-01 pce 1039.8 0.045614395358985545
1975-08-01 pce 1047 0.04623045922427292
1975-09-01 pce 1054.8 0.04689786174500089
1975-10-01 pce 1060.9 0.04741980474198049
… … … …

32.1 Lines

A line joins points in x order; dates on x give a datetime axis.

32.1.1 Basic line with dates

Source: R Graph Gallery - Line chart with ggplot2; Time series (1, 2). Data: US economic time series, 1967-2015, R ggplot2 package economics (Federal Reserve Bank of St. Louis, FRED); licence: MIT (ggplot2).

(plot/render-image
 {:data economics
  :mapping {:x :date :y :unemploy}
  :charts [(chart/line)]
  :labels {:title "Unemployed persons in the US" :x nil :y "thousands"}}
 {:width 800 :height 400})

32.1.2 Line styling

Width, colour and points are chart options; :dash mapped to a variable takes the named dash styles in order (:solid, :dashed, :dotted, …), so lines read apart without colour.

Source: R Graph Gallery - Line chart with ggplot2 (customization). Data: US economic time series, 1967-2015, R ggplot2 package economics (Federal Reserve Bank of St. Louis, FRED); licence: MIT (ggplot2).

(let [recent (filter #(.isAfter ^LocalDate (:date %) (LocalDate/of 2010 1 1)) economics)
      recent-long (filter #(and (.isAfter ^LocalDate (:date %) (LocalDate/of 2010 1 1))
                                (#{"psavert" "uempmed" "unemploy"} (:variable %)))
                          economics-long)]
  (plot/render-image
   (plot/arrange
    [{:data recent :mapping {:x :date :y :psavert}
      :charts [(chart/line {:width 2 :color [215 48 39]}) (chart/scatter {:size 5 :color [215 48 39]})]
      :labels {:title "width, colour, points" :x nil :y "savings rate, %"}}
     {:data recent-long :mapping {:x :date :y :value01 :dash :variable}
      :charts [(chart/line {:width 1.5 :color :black})]
      :labels {:title ":dash by variable" :x nil :y "rescaled"}}]
    {:columns 2})
   {:width 1000 :height 400}))

32.1.3 Log scale

Growth at a steady rate is a straight line on a log axis; :sqrt, :pow and :symlog are the other continuous transforms (wadogo scales).

Source: R Graph Gallery - Line plot with log scale. Data: US economic time series, 1967-2015, R ggplot2 package economics (Federal Reserve Bank of St. Louis, FRED); licence: MIT (ggplot2).

(plot/render-image
 (plot/arrange
  [{:data economics :mapping {:x :date :y :pce} :charts [(chart/line)]
    :labels {:title "linear" :x nil :y "consumption, billions USD"}}
   {:data economics :mapping {:x :date :y :pce} :scales {:y {:kind :log}} :charts [(chart/line)]
    :labels {:title ":log" :x nil :y "consumption, billions USD"}}]
  {:columns 2})
 {:width 1000 :height 400})

32.1.4 Several groups

One line per level of :color; values on different units are rescaled to 0..1 to share an axis.

Source: R Graph Gallery - Line chart with several groups. Data: US economic time series, 1967-2015, R ggplot2 package economics (Federal Reserve Bank of St. Louis, FRED); licence: MIT (ggplot2).

(plot/render-image
 {:data economics-long
  :mapping {:x :date :y :value01 :color :variable}
  :charts [(chart/line)]
  :labels {:title "Five economic series, rescaled" :x nil :y "value (0..1)"}}
 {:width 900 :height 450})

32.1.5 Steps and interpolated lines

:interpolator takes any fastmath one-dimensional method: steps, monotone (no overshoot) or cubic curves through the points.

Source: R Graph Gallery - Line plot; data-to-viz - Line plot (1, 2). Data: Monthly airline passengers, 1949-1960, R datasets package AirPassengers (Box & Jenkins 1976); licence: Part of R (GPL-2 | GPL-3).

(let [year (filter #(= 1955 (:year %)) air)
      panel (fn [title opts] {:data year :mapping {:x :date :y :passengers}
                              :charts [(chart/line opts) (chart/scatter {:size 5})]
                              :labels {:title title :x nil :y "passengers, thousands"}})]
  (plot/render-image
   (plot/arrange [(panel ":step-after" {:interpolator :step-after})
                  (panel ":monotone" {:interpolator :monotone})
                  (panel ":cubic" {:interpolator :cubic})]
                 {:columns 3})
   {:width 1000 :height 350}))

32.1.6 Sampling step

An interpolated line is evaluated every :sample-step pixels (default 1) or at {:count n} positions per run - a coarse count shows the polyline behind a curve.

Source: cljplot2 - line :sample-step. Data: Monthly airline passengers, 1949-1960, R datasets package AirPassengers (Box & Jenkins 1976); licence: Part of R (GPL-2 | GPL-3).

(let [years (filter #(<= 1958 (:year %)) air)
      panel (fn [title step] {:data years :mapping {:x :date :y :passengers}
                              :charts [(chart/line {:interpolator :cubic :sample-step step}) (chart/scatter {:size 4})]
                              :labels {:title title :x nil :y "passengers, thousands"}})]
  (plot/render-image
   (plot/arrange [(panel "every pixel (default)" 1) (panel "{:count 12} per run" {:count 12})] {:columns 2})
   {:width 1000 :height 380}))

32.1.7 Missing values

A missing (nil) value breaks the line (:gaps :break, default); :gaps :bridge joins the ends with a dimmed segment, so the gap stays visible.

Source: cljplot2 - line :gaps. Data: US economic time series, 1967-2015, R ggplot2 package economics (Federal Reserve Bank of St. Louis, FRED); licence: MIT (ggplot2).

(let [gappy (map (fn [row] (if (<= 1990 (.getYear ^LocalDate (:date row)) 1992) (assoc row :psavert nil) row))
                 (filter #(<= 1985 (.getYear ^LocalDate (:date %)) 1997) economics))
      panel (fn [title gaps] {:data gappy :mapping {:x :date :y :psavert}
                              :charts [(chart/line {:gaps gaps :width 1.5})]
                              :labels {:title title :x nil :y "savings rate, %"}})]
  (plot/render-image
   (plot/arrange [(panel ":gaps :break" :break) (panel ":gaps :bridge" :bridge)] {:columns 2})
   {:width 1000 :height 380}))

32.2 Time axes

Datetime ticks fall on calendar units; the formatter is a function or a java.time pattern string.

32.2.1 Date format and breaks

Source: R Graph Gallery - Time series with ggplot2; Manage dates (1, 2). Data: US economic time series, 1967-2015, R ggplot2 package economics (Federal Reserve Bank of St. Louis, FRED); licence: MIT (ggplot2).

(plot/render-image
 (plot/arrange
  [{:data economics :mapping {:x :date :y :uempmed}
    :scales {:x {:ticks-params {:unit :years :step 5} :formatter "yyyy"}}
    :charts [(chart/line)] :labels {:title "every 5 years, \"yyyy\"" :x nil :y "weeks"}}
   {:data (filter #(= 2008 (.getYear ^LocalDate (:date %))) economics) :mapping {:x :date :y :uempmed}
    :scales {:x {:ticks-params {:unit :months :step 2} :formatter "MMM"}}
    :charts [(chart/line) (chart/scatter {:size 5})] :labels {:title "2008, every 2 months, \"MMM\"" :x nil :y "weeks"}}]
  {:columns 2})
 {:width 1000 :height 400})

32.2.2 Time window

An explicit :domain of dates shows one window of a long series; the data outside is clipped by the panel.

Source: R Graph Gallery - Time series with ggplot2 (select a time frame). Data: US economic time series, 1967-2015, R ggplot2 package economics (Federal Reserve Bank of St. Louis, FRED); licence: MIT (ggplot2).

(plot/render-image
 {:data economics
  :mapping {:x :date :y :unemploy}
  :scales {:x {:domain [(LocalDate/of 2000 1 1) (LocalDate/of 2010 1 1)]} :y {:domain [4000 16000]}}
  :charts [(chart/line {:width 1.5})]
  :labels {:title "Unemployment, 2000-2010" :x nil :y "thousands"}}
 {:width 800 :height 400})

32.2.3 Annotations

Literal rects, rules and texts take dates as positions: recessions shaded behind the line, a rule and a label.

Source: R Graph Gallery - Line chart annotation. Data: US economic time series, 1967-2015, R ggplot2 package economics (Federal Reserve Bank of St. Louis, FRED); licence: MIT (ggplot2).

(let [d (fn [y mo] (LocalDate/of (int y) (int mo) 1))
      recessions [[(d 1969 12) (d 1970 11)] [(d 1973 11) (d 1975 3)] [(d 1980 1) (d 1980 7)] [(d 1981 7) (d 1982 11)]
                  [(d 1990 7) (d 1991 3)] [(d 2001 3) (d 2001 11)] [(d 2007 12) (d 2009 6)]]]
  (plot/render-image
   {:data economics
    :mapping {:x :date :y :unemploy}
    :charts [(chart/rect {:x recessions :fill [0 0 0 35]})
             (chart/line {:width 1.5})
             (chart/rule {:x (d 2008 9) :dash :dashed :color [215 48 39]})
             (chart/text {:x (d 2008 9) :y 14500 :text "Lehman Brothers" :anchor [:right :middle] :offset [-5 0]
                          :color [215 48 39]})]
    :labels {:title "Unemployment and US recessions (NBER)" :x nil :y "thousands"}}
   {:width 900 :height 450}))

32.2.4 Time heatmap

Year × month as two categorical axes, the value as a linear colour: seasonality and growth at once. A rect whose ends are levels covers the level’s band.

Source: R Graph Gallery - The hourly heatmap. Data: Monthly airline passengers, 1949-1960, R datasets package AirPassengers (Box & Jenkins 1976); licence: Part of R (GPL-2 | GPL-3).

(let [months ["Jan" "Feb" "Mar" "Apr" "May" "Jun" "Jul" "Aug" "Sep" "Oct" "Nov" "Dec"]
      rows (map #(assoc % :year-level (str (:year %))) air)]
  (plot/render-image
   {:data rows
    :scales {:x {:domain months} :color {:range :viridis/viridis :title "passengers"}}
    :charts [(chart/rect {:mapping {:x0 :month :x1 :month :y0 :year-level :y1 :year-level :color :passengers}
                          :stroke-color :white})]
    :labels {:title "Airline passengers by month (thousands)" :x nil :y nil}}
   {:width 800 :height 500}))

32.3 Smoothing and uncertainty

chart/smooth fits a curve per group with a band; chart/ribbon draws any band given as :ymin / :ymax.

32.3.1 Smoothers with confidence bands

Loess (default) with nested 50% / 95% bands; a linear model per diet; a Gaussian process (mean ± z·σ). The loess defaults are ggplot2’s geom_smooth ones - 75% of the points, local quadratic fits - with fastmath’s two robustness iterations (:method-params {:iters 0} is ggplot2’s plain fit; the robust bands are a little wider). The bands of the plain fit (:iters 0) equal R’s predict(se = TRUE); with 144 or 220 points they are narrow. On a date axis the fit works in epoch milliseconds - a kernel’s scale too (here three months).

Source: R Graph Gallery - Scatterplot with ggplot2 (smoothing); Polynomial curve fitting (1, 2). Data: Monthly airline passengers, 1949-1960, R datasets package AirPassengers (Box & Jenkins 1976); licence: Part of R (GPL-2 | GPL-3).

(let [year-ms (* 365.25 86400000.0)]
  (plot/render-image
   (plot/arrange
    [{:data air :mapping {:x :date :y :passengers}
      :charts [(chart/scatter {:size 3}) (chart/smooth {:level [0.5 0.95]})]
      :labels {:title "loess, 50% / 95%" :x nil :y "passengers"}}
     {:data chicks :mapping {:x :time :y :weight :color :diet}
      :charts [(chart/scatter {:size 3}) (chart/smooth {:method :lm})]
      :labels {:title "lm per diet" :x "days" :y "weight, g"}}
     {:data (filter #(<= 1956 (:year %)) air) :mapping {:x :date :y :passengers}
      :charts [(chart/smooth {:method :gp :level [0.5 0.95]
                              :method-params {:kernel (kernel/kernel :gaussian {:sigma (* 0.25 year-ms)})
                                              :noise 0.05 :normalize? true}})
               (chart/scatter {:size 4})]
      :labels {:title "Gaussian process" :x nil :y "passengers"}}]
    {:columns 3})
   {:width 1000 :height 380}))

32.3.2 Ribbon

Daily minimum and maximum temperatures as a band, the mean as a line.

Source: R Graph Gallery - Lines with error envelopes. Data: Weather in Lincoln, Nebraska, 2016, R ggridges package lincoln_weather (Weather Underground, via Wilke); licence: GPL-2 (ggridges).

(plot/render-image
 {:data lincoln
  :mapping {:x :date}
  :scales {:x {:formatter "MMM"}}
  :charts [(chart/ribbon {:mapping {:ymin :min-temp :ymax :max-temp} :color [215 48 39]})
           (chart/line {:mapping {:y :mean-temp} :color [150 30 20]})]
  :labels {:title "Lincoln, Nebraska, 2016: daily min-max and mean" :x nil :y "temperature, F"}}
 {:width 900 :height 420})

32.4 Many lines

Fifty chicks are a spaghetti plot; highlighting, small multiples and labels make it readable.

32.4.1 Highlighting one line

All lines in grey (:group, no colour), then the one of interest from its own :data in colour.

Source: data-to-viz - Spaghetti plot; R Graph Gallery - Highlight elements in a line chart (1, 2). Data: Weight versus age of chicks on four diets, R datasets package ChickWeight (Crowder & Hand 1990); licence: Part of R (GPL-2 | GPL-3).

(plot/render-image
 {:data chicks
  :mapping {:x :time :y :weight}
  :charts [(chart/line {:mapping {:group :chick} :color [190 190 190]})
           (chart/line {:data (filter #(= "21" (:chick %)) chicks) :width 2.5 :color [215 48 39]})
           (chart/text {:data (filter #(= "21" (:chick %)) chicks) :mapping {:x :time :y :weight :label :chick}
                        :select :last :color [215 48 39]})]
  :labels {:title "Chick 21 among all fifty" :x "days" :y "weight, g"}}
 {:width 800 :height 450})

32.4.2 Small multiples with the others greyed

A chart whose mapping sets the facet key to nil is drawn in every panel: all chicks in grey behind each diet’s own.

Source: R Graph Gallery - Small multiple line chart, all groups greyed out. Data: Weight versus age of chicks on four diets, R datasets package ChickWeight (Crowder & Hand 1990); licence: Part of R (GPL-2 | GPL-3).

(plot/render-image
 {:data chicks
  :mapping {:x :time :y :weight :group :chick :facet :diet}
  :charts [(chart/line {:mapping {:facet nil} :color [175 175 175]})
           (chart/line {:color [33 102 172]})]
  :labels {:title "Chicks per diet, all chicks in grey" :x "days" :y "weight, g"}}
 {:width 900 :height 600})

32.4.3 Labels at line ends

chart/text with :select :last keeps one label per series at its last point and makes room for it.

Source: R Graph Gallery - Line chart with labels at end of lines. Data: US economic time series, 1967-2015, R ggplot2 package economics (Federal Reserve Bank of St. Louis, FRED); licence: MIT (ggplot2).

(let [recent (filter #(and (.isAfter ^LocalDate (:date %) (LocalDate/of 2005 1 1))
                            (#{"pce" "psavert" "unemploy"} (:variable %)))
                      economics-long)]
  (plot/render-image
   {:data recent
    :mapping {:x :date :y :value01 :color :variable}
    :charts [(chart/line {:width 1.5})
             (chart/text {:mapping {:x :date :y :value01 :label :variable :color :variable} :select :last})]
    :legend false
    :labels {:title "Three economic series since 2005, labelled at the end" :x nil :y "value (0..1)"}}
   {:width 900 :height 450}))

32.4.4 Bump chart

Ranks instead of values: levels/rank-rows ranks the chicks at every weighing (1 - the heaviest; ties by data order with :ties :first), a monotone line joins the ranks on a reversed axis.

Source: R Graph Gallery - Basic bump plot. Data: Weight versus age of chicks on four diets, R datasets package ChickWeight (Crowder & Hand 1990); licence: Part of R (GPL-2 | GPL-3).

(let [eight (set (map str (range 1 9)))
      ranked (levels/rank-rows (filter #(and (eight (:chick %)) (zero? (mod (:time %) 4))) chicks) :time :weight
                              {:ties :first})]
  (plot/render-image
   {:data ranked
    :mapping {:x :time :y :rank :color :chick}
    :scales {:y {:domain [8.5 0.5] :nice false :ticks (vec (range 1 9))}}
    :charts [(chart/line {:interpolator :monotone :width 3})
             (chart/scatter {:size 9})
             (chart/text {:mapping {:x :time :y :rank :label :chick :color :chick} :select :last})]
    :legend false
    :labels {:title "Weight rank of chicks 1-8 on diet 1" :x "days" :y "rank"}}
   {:width 800 :height 450}))

32.5 Areas

An area fills between a line and zero, or between stacked lines.

32.5.1 Area chart

Source: R Graph Gallery - Area chart with ggplot2; data-to-viz - Area chart (1, 2). Data: US economic time series, 1967-2015, R ggplot2 package economics (Federal Reserve Bank of St. Louis, FRED); licence: MIT (ggplot2).

(plot/render-image
 {:data economics
  :mapping {:x :date :y :psavert}
  :charts [(chart/line {:fill :color :alpha 0.4 :width 1.5})]
  :labels {:title "Personal savings rate" :x nil :y "%"}}
 {:width 800 :height 400})

32.5.2 Gradient area

:fill-gradient colours the area along the y (or x) scale’s range.

Source: R Graph Gallery - Area chart with gradient. Data: US economic time series, 1967-2015, R ggplot2 package economics (Federal Reserve Bank of St. Louis, FRED); licence: MIT (ggplot2).

(plot/render-image
 {:data economics
  :mapping {:x :date :y :psavert}
  :charts [(chart/line {:fill-gradient [[255 255 255 0] [33 102 172 200]] :fill-gradient-axis :y :width 1.5
                        :color [33 102 172]})]
  :labels {:title "Savings rate, gradient along y" :x nil :y "%"}}
 {:width 800 :height 400})

(def diet-means
  "Mean chick weight per diet and day."
  (vec (for [[[diet time] rows] (sort-by key (group-by (juxt :diet :time) chicks))]
         {:diet diet :time time :weight (/ (double (reduce + (map :weight rows))) (count rows))})))
diet time weight
1 0 41.4
1 2 47.25
1 4 56.473684210526315
1 6 66.78947368421052
1 8 79.6842105263158
1 10 93.05263157894737
1 12 108.52631578947368
1 14 123.38888888888889
1 16 144.64705882352942
1 18 158.94117647058823
1 20 170.41176470588235
1 21 177.75
2 0 40.7
2 2 49.4
2 4 59.8
2 6 75.4
2 8 91.7
2 10 108.5
2 12 131.3
2 14 141.9
2 16 164.7
2 18 187.7
2 20 205.6
2 21 214.7
3 0 40.8
3 2 50.4
3 4 62.2
3 6 77.9
3 8 98.4
3 10 117.1
3 12 144.4
3 14 164.5
3 16 197.4
3 18 233.1
3 20 258.9
3 21 270.3
4 0 41.0
4 2 51.8
4 4 64.5
4 6 83.9
4 8 105.6
4 10 126.0
4 12 151.4
4 14 161.8
4 16 182.0
4 18 202.9
4 20 233.88888888888889
4 21 238.55555555555554

32.5.3 Stacked area

:position :stack aligns the groups on the union of their x values and stacks them; every band is filled.

Source: R Graph Gallery - Stacked area chart; data-to-viz - Stacked area (1, 2). Data: Weight versus age of chicks on four diets, R datasets package ChickWeight (Crowder & Hand 1990); licence: Part of R (GPL-2 | GPL-3).

(plot/render-image
 {:data diet-means
  :mapping {:x :time :y :weight :color :diet}
  :charts [(chart/line {:position :stack})]
  :labels {:title "Mean chick weight per diet, stacked" :x "days" :y "weight, g"}}
 {:width 800 :height 420})

32.5.4 Percent stacked area

Source: R Graph Gallery - Stacked area chart (proportion). Data: Weight versus age of chicks on four diets, R datasets package ChickWeight (Crowder & Hand 1990); licence: Part of R (GPL-2 | GPL-3).

(plot/render-image
 {:data diet-means
  :mapping {:x :time :y :weight :color :diet}
  :charts [(chart/line {:position :normalize})]
  :labels {:title "Share of each diet in the summed mean weight" :x "days"}}
 {:width 800 :height 420})

32.5.5 Stacking order

Groups stack in the colour scale’s order: an explicit :domain reorders the bands (and the legend).

Source: data-to-viz - Stacking caveat; R Graph Gallery - Reorder a variable (1, 2). Data: Weight versus age of chicks on four diets, R datasets package ChickWeight (Crowder & Hand 1990); licence: Part of R (GPL-2 | GPL-3).

(plot/render-image
 (plot/arrange
  [{:data diet-means :mapping {:x :time :y :weight :color :diet}
    :charts [(chart/line {:position :stack})] :labels {:title "diets 1, 2, 3, 4" :x "days" :y "weight, g"}}
   {:data diet-means :mapping {:x :time :y :weight :color :diet} :scales {:color {:domain ["3" "4" "2" "1"]}}
    :charts [(chart/line {:position :stack})] :labels {:title "diets 3, 4, 2, 1" :x "days" :y "weight, g"}}]
  {:columns 2})
 {:width 1000 :height 400})

32.5.6 Streamgraph

Stacked around a moving baseline: :silhouette (symmetric around zero) and :wiggle (least slope); an :interpolator smooths the bands.

Source: R Graph Gallery - Streamgraph; data-to-viz - Streamgraph (1, 2). Data: Weight versus age of chicks on four diets, R datasets package ChickWeight (Crowder & Hand 1990); licence: Part of R (GPL-2 | GPL-3).

(plot/render-image
 (plot/arrange
  [{:data diet-means :mapping {:x :time :y :weight :color :diet}
    :charts [(chart/line {:position :silhouette :interpolator :monotone})]
    :labels {:title ":silhouette" :x "days" :y nil}}
   {:data diet-means :mapping {:x :time :y :weight :color :diet}
    :charts [(chart/line {:position :wiggle :interpolator :monotone})]
    :labels {:title ":wiggle" :x "days" :y nil}}]
  {:columns 2})
 {:width 1000 :height 400})

32.6 Other marks

Segments, candlesticks, colour along a line and paths through time.

32.6.1 Lollipop

A segment from zero to each value ((chart/segment {:yend 0})) under a point, on any x: car models (flipped for readable labels) or dates.

Source: R Graph Gallery - Basic lollipop plot; data-to-viz - Lollipop (1, 2). Data: Motor Trend car road tests (1974), R datasets package mtcars (Henderson & Velleman 1981); licence: Part of R (GPL-2 | GPL-3).

(let [ordered (sort-by :mpg mtcars)]
  (plot/render-image
   (plot/arrange
    [{:data ordered
      :mapping {:x :model :y :mpg}
      :scales {:x {:domain (mapv :model ordered)}}
      :flip true
      :charts [(chart/segment {:yend 0 :color [150 150 150]}) (chart/scatter {:size 7 :color [33 102 172]})]
      :labels {:title "Fuel economy of 32 cars" :x nil :y "miles per gallon"}}
     {:data (filter #(= 1959 (:year %)) air)
      :mapping {:x :date :y :passengers}
      :charts [(chart/segment {:yend 0 :color [150 150 150]}) (chart/scatter {:size 7 :color [33 102 172]})]
      :labels {:title "Passengers in 1959" :x nil :y "thousands"}}]
    {:columns 2})
   {:width 1000 :height 700}))

32.6.2 Candlestick

Open-high-low-close per day: the wick from low to high, the body from open to close, green up and red down; OHLC bars as the alternative. The prices are a seeded random walk.

Source: Wikipedia - Candlestick chart.

(def prices
  "Daily open / high / low / close of a random walk over 40 trading days."
  (let [rng (r/rng :mersenne 21)
        start (LocalDate/of 2024 3 1)]
    (vec (map-indexed (fn [i close]
                        (let [open (+ close (* 1.2 (r/grandom rng)))
                              spread (m/abs (* 0.8 (double (r/grandom rng))))]
                          {:date (.plusDays start i) :open open :close close
                           :high (+ (max open close) spread) :low (- (min open close) spread)}))
                      (take 40 (reductions + 50.0 (repeatedly #(r/grandom rng))))))))
date open close high low
2024-03-01 47.632548030053506 50.0 50.3286977642804 47.30385026577311
2024-03-02 49.59853547913083 50.13074905926286 50.849707159246314 48.87957737914738
2024-03-03 49.85964964035673 50.164925774380706 50.279031427100506 49.74554398763693
2024-03-04 50.113320837937856 50.349991952731706 50.58741357428397 49.87589921638559
2024-03-05 52.25208184068009 51.52423795803585 52.704142448207214 51.072177350508724
2024-03-06 48.62515032006433 49.926852284301525 50.1456935307793 48.406309073586556
2024-03-07 51.41949072012316 50.810355354606294 51.561443697255 50.66840237747445
2024-03-08 50.83730269968272 50.897941387702105 51.04705044932875 50.68819363805608
2024-03-09 49.79245191479805 49.844583588212785 50.457704237195586 49.17933126581525
2024-03-10 52.11272318746326 50.354760452323745 52.51758165578023 49.94990198400677
2024-03-11 50.78777657220132 50.04923547053897 51.8152982462539 49.02171379648638
2024-03-12 50.871536659373426 50.185507073661874 51.89775874581844 49.15928498721686
2024-03-13 50.897790041859025 50.51837339991901 52.0779266255956 49.33823681618243
2024-03-14 49.903006876881335 51.17463629759976 52.52318359859172 48.554459575889375
2024-03-15 47.576601017504466 50.01826907744722 50.43658287583983 47.158287219111855
2024-03-16 50.05479355601661 49.903536289257374 51.42156434025841 48.53676550501557
2024-03-17 51.367209395295305 50.61400136594374 52.55248818394688 49.42872257729217
2024-03-18 52.10509462728763 52.304673004646936 53.38289468630413 51.02687294563044
2024-03-19 53.05672973292597 52.181533109987356 53.353207165520104 51.885055677393225
2024-03-20 53.42799936197863 51.65871212739008 53.71266436220612 51.37404712716259
2024-03-21 56.49851254967236 53.39748128958453 57.189633568274125 52.70636027098277
2024-03-22 52.544694099577484 53.737660836453024 53.75791021304482 52.524444722985685
2024-03-23 52.6969152281411 52.89226687364923 53.671817155594404 51.917364946195924
2024-03-24 52.875851207803926 53.05708637368157 53.391090799734236 52.54184678175126
2024-03-25 49.93578283424558 51.106243521310944 52.50392093089916 48.53810542465736
2024-03-26 49.014635230837115 51.09317848320589 52.65203438806887 47.455779325974135
2024-03-27 52.535220062486324 51.96895173211941 52.95975564483152 51.544416149774214
2024-03-28 51.18977618228914 51.19727609700252 51.65773077350881 50.72932150578285
2024-03-29 52.17032047684884 52.21839198373194 53.239600530855526 51.14911192972526
2024-03-30 52.06274248419599 51.36402226409783 53.16079893770136 50.265965810592455
2024-03-31 53.163607037448976 53.10427153844901 53.50413439691346 52.76374417898453
2024-04-01 54.67267052712853 53.86913314270853 55.55133055074569 52.99047311909137
2024-04-02 54.6609377300407 53.15120173078838 55.76366272761407 52.04847673321501
2024-04-03 54.761404104331106 53.08579096970526 54.90249888847253 52.944696185563835
2024-04-04 53.895327056650466 51.4048111816092 54.949352734076214 50.35078550418345
2024-04-05 53.08471033063695 52.91400580583511 53.09317800271896 52.905538133753105
2024-04-06 54.0340638360606 53.880941616549315 54.56854803674665 53.34645741586326
2024-04-07 53.66050127708517 53.05887978767572 54.15584080856096 52.56354025619993
2024-04-08 53.80372874961422 53.38908282814445 54.375145674105184 52.81766590365349
2024-04-09 53.13514114153314 52.48026246690437 53.82882939728739 51.78657421115012
(plot/render-image
 (plot/arrange
  [{:data prices :mapping {:x :date :open :open :high :high :low :low :close :close}
    :charts [(chart/candlestick)] :labels {:title "candlestick" :x nil :y "price"}}
   {:data prices :mapping {:x :date :open :open :high :high :low :low :close :close}
    :charts [(chart/candlestick {:style :ohlc})] :labels {:title ":style :ohlc" :x nil :y "price"}}]
  {:columns 2})
 {:width 1000 :height 400})

32.6.3 Line coloured by its value

A linear :color colours the line along its length; binned, it shows ranges (see also the Colors chapter).

Source: R Graph Gallery - Line plot colour following y value. Data: Weather in Lincoln, Nebraska, 2016, R ggridges package lincoln_weather (Weather Underground, via Wilke); licence: GPL-2 (ggridges).

(plot/render-image
 (plot/arrange
  [{:data lincoln :mapping {:x :date :y :mean-temp :color :mean-temp} :scales {:color {:range :RColorBrewer/RdYlBu :reverse true :title "F"}}
    :charts [(chart/line {:width 2})] :labels {:title "linear" :x nil :y "mean temperature, F"}}
   {:data lincoln :mapping {:x :date :y :mean-temp :color :mean-temp}
    :scales {:color {:range :RColorBrewer/RdYlBu :kind :binned :reverse true :bins [20 40 60 80] :title "F"}}
    :charts [(chart/line {:width 2})] :labels {:title "binned" :x nil :y "mean temperature, F"}}]
  {:columns 2})
 {:width 1000 :height 400})

32.6.4 Connected scatter

Two variables against each other, joined in time order (:sort false); :curve :bezier passes a smooth path through the points, labels mark some years.

Source: R Graph Gallery - Connected scatterplot with ggplot2; data-to-viz - Connected scatter (1, 2). Data: US economic time series, 1967-2015, R ggplot2 package economics (Federal Reserve Bank of St. Louis, FRED); licence: MIT (ggplot2).

(let [yearly (vec (for [[year rows] (sort-by key (group-by #(.getYear ^LocalDate (:date %)) economics))
                        :when (<= 2000 year 2014)]
                    {:year year
                     :uempmed (/ (double (reduce + (map :uempmed rows))) (count rows))
                     :psavert (/ (double (reduce + (map :psavert rows))) (count rows))}))]
  (plot/render-image
   {:data yearly
    :mapping {:x :uempmed :y :psavert}
    :charts [(chart/line {:sort false :curve :bezier :color [33 102 172]})
             (chart/scatter {:size 6 :color [33 102 172]})
             (chart/text {:data (filter #(#{2000 2003 2006 2009 2012 2014} (:year %)) yearly) :mapping {:x :uempmed :y :psavert :label :year}
                          :anchor [:left :bottom] :offset [4 4]})]
    :labels {:title "Unemployment duration vs savings rate, 2000-2014" :x "median unemployment, weeks" :y "savings rate, %"}}
   {:width 800 :height 550}))

32.7 Functions

chart/function samples functions across the panel. Several functions - a vector, [label f] pairs or a map - are one group each: {:mapping {:color :function}} colours them and gives the legend, labels in the given order. :y-limits keeps asymptotes from flattening the curve (segments across a limit are cut).

32.7.1 Many functions with a legend

Source: cljplot2 - chart/function.

(plot/render-image
 (plot/arrange
  [{:charts [(chart/function {:f (mapv (fn [k] [(str "sin " k "x") (fn [x] (m/sin (* k (double x))))]) (range 1 11))
                              :domain [0 m/PI] :mapping {:color :function}})]
    :scales {:color {:title nil}}
    :labels {:title "ten [label f] pairs" :x "x" :y "y"}}
   {:charts [(chart/function {:f m/tan :domain [(- m/PI) m/PI] :y-limits [-5 5]})]
    :labels {:title "tan with :y-limits [-5 5]" :x "x" :y "y"}}]
  {:columns 2})
 {:width 1000 :height 400})

32.7.2 Parametric curves

:parametric - (fn [t] [x y]) over :t [lo hi], sampled by count; Lissajous figures on an isometric aspect.

Source: Wikipedia - Lissajous curve.

(plot/render-image
 (plot/arrange
  (vec (for [[a b] [[1 2] [3 2] [3 4] [5 4]]]
         {:aspect :iso
          :charts [(chart/function {:parametric (fn [t] [(m/sin (* a (double t))) (m/sin (* b (double t)))])
                                    :t [0 m/TWO_PI] :sample-step {:count 1000}})]
          :labels {:title (str "a = " a ", b = " b) :x nil :y nil}}))
  {:columns 4})
 {:width 1000 :height 300})

32.8 Caveats

32.8.1 Cutting the y axis; aspect ratio

Starting y at zero shows changes against the whole; a cut axis magnifies them. A wide panel flattens slopes, a narrow one steepens them - the same data.

Source: data-to-viz - Cut y axis; Aspect ratio (1, 2). Data: US economic time series, 1967-2015, R ggplot2 package economics (Federal Reserve Bank of St. Louis, FRED); licence: MIT (ggplot2).

(let [recent (filter #(.isAfter ^LocalDate (:date %) (LocalDate/of 2012 1 1)) economics)
      plot-of (fn [title y] {:data recent :mapping {:x :date :y :pop} :scales {:y y}
                             :charts [(chart/line {:width 1.5})] :labels {:title title :x nil :y "population, thousands"}})]
  (plot/render-image
   (plot/arrange [(plot/arrange [(plot-of "y from 0" {:domain [0 330000]}) (plot-of "y cut" {})] {:columns 2})
                  (plot/arrange [(plot-of "wide" {}) (plot-of "narrow" {})] {:columns 2 :widths [4 1]})]
                 {:columns 1})
   {:width 1000 :height 700}))

source: notebooks/use_cases/evolution.clj