12  Change Over Time

Line charts and their variants β€” showing change over a sequence.

(ns napkinsketch-book.change-over-time
  (:require
   ;; Tablecloth β€” dataset manipulation
   [tablecloth.api :as tc]
   ;; Kindly β€” notebook rendering protocol
   [scicloj.kindly.v4.kind :as kind]
   ;; Napkinsketch β€” composable plotting
   [scicloj.napkinsketch.api :as sk]))

Line

Connected line through data points.

(def wave {:x (range 30)
           :y (map #(Math/sin (* % 0.3)) (range 30))})
(-> wave
    (sk/lay-line :x :y))
yx051015202530-1.0-0.8-0.6-0.4-0.20.00.20.40.60.81.0

Grouped Lines

Color separates multiple series.

(def waves {:x (concat (range 30) (range 30))
            :y (concat (map #(Math/sin (* % 0.3)) (range 30))
                       (map #(Math/cos (* % 0.3)) (range 30)))
            :fn (concat (repeat 30 :sin) (repeat 30 :cos))})
(-> waves
    (sk/lay-line :x :y {:color :fn}))
yxfn:sin:cos051015202530-1.0-0.8-0.6-0.4-0.20.00.20.40.60.81.0

Thick Line

Constant stroke width via :size.

(-> wave
    (sk/lay-line :x :y {:size 4}))
yx051015202530-1.0-0.8-0.6-0.4-0.20.00.20.40.60.81.0

Line with Points

Overlay points on a grouped line plot.

(def growth
  {:day [1 2 3 4 5 1 2 3 4 5]
   :value [10 15 13 18 22 8 12 11 16 19]
   :group [:a :a :a :a :a :b :b :b :b :b]})
(-> growth
    (sk/view :day :value {:color :group})
    sk/lay-line
    sk/lay-point)
valuedaygroup:a:b1.01.52.02.53.03.54.04.55.0810121416182022

Step

Horizontal-then-vertical connected points.

(-> {:x [1 2 3 4 5]
     :y [2 4 1 5 3]}
    (sk/lay-step :x :y)
    sk/lay-point)
yx1.01.52.02.53.03.54.04.55.01.01.52.02.53.03.54.04.55.0

Step by Group

Grouped step lines.

(-> growth
    (sk/view :day :value {:color :group})
    sk/lay-step
    sk/lay-point)
valuedaygroup:a:b1.01.52.02.53.03.54.04.55.0810121416182022

Area

Filled area under a line.

(-> {:x (range 30)
     :y (map #(Math/sin (* % 0.3)) (range 30))}
    (sk/lay-area :x :y))
yx0510152025300.00.20.40.60.81.01.2

Stacked Area

Each group fills above the previous.

(-> {:x (concat (range 10) (range 10) (range 10))
     :y (concat [1 2 3 4 5 4 3 2 1 0]
                [2 2 2 3 3 3 2 2 2 2]
                [1 1 1 1 2 2 2 1 1 1])
     :group (concat (repeat 10 "A") (repeat 10 "B") (repeat 10 "C"))}
    (sk/lay-stacked-area :x :y {:color :group}))
yxgroupABC0123456789012345678910

What’s Next

  • Relationships β€” heatmaps, contours, and 2D density
  • Polar β€” radial charts for cyclical data
source: notebooks/napkinsketch_book/change_over_time.clj