13 Charts: points and paths
Marks placed at data positions: points (scatter), lines through them (line), straight pieces between two positions (segment) and filled shapes (polygon). Each chart lists the mapping keys it reads, its own options and the shared option groups it takes (documented once in the Shared options chapter).
(ns reference.charts-points-paths
(:require [fastmath.random :as r]
[cljplot2.chart :as chart]
[cljplot2.core :as plot]
[cljplot2.data.levels :as levels]
[cljplot2.describe :as describe]
[notebook-utils :as nu]))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 |
Data: Monthly stock prices for five companies, vega-datasets 3.3.1 stocks.csv (https://cdn.jsdelivr.net/npm/vega-datasets@3.3.1/data/stocks.csv); licence: none listed - see vega-datasets (cite Vega as the source).
(def stocks (nu/dataset :stocks))| symbol | date | price |
|---|---|---|
| MSFT | 2000-01-01 | 39.81 |
| MSFT | 2000-02-01 | 36.35 |
| MSFT | 2000-03-01 | 43.22 |
| MSFT | 2000-04-01 | 28.37 |
| MSFT | 2000-05-01 | 25.45 |
| MSFT | 2000-06-01 | 32.54 |
| MSFT | 2000-07-01 | 28.4 |
| MSFT | 2000-08-01 | 28.4 |
| MSFT | 2000-09-01 | 24.53 |
| MSFT | 2000-10-01 | 28.02 |
| MSFT | 2000-11-01 | 23.34 |
| MSFT | 2000-12-01 | 17.65 |
| MSFT | 2001-01-01 | 24.84 |
| MSFT | 2001-02-01 | 24 |
| MSFT | 2001-03-01 | 22.25 |
| MSFT | 2001-04-01 | 27.56 |
| MSFT | 2001-05-01 | 28.14 |
| MSFT | 2001-06-01 | 29.7 |
| MSFT | 2001-07-01 | 26.93 |
| MSFT | 2001-08-01 | 23.21 |
| MSFT | 2001-09-01 | 20.82 |
| MSFT | 2001-10-01 | 23.65 |
| MSFT | 2001-11-01 | 26.12 |
| MSFT | 2001-12-01 | 26.95 |
| MSFT | 2002-01-01 | 25.92 |
| MSFT | 2002-02-01 | 23.73 |
| MSFT | 2002-03-01 | 24.53 |
| MSFT | 2002-04-01 | 21.26 |
| MSFT | 2002-05-01 | 20.71 |
| MSFT | 2002-06-01 | 22.25 |
| MSFT | 2002-07-01 | 19.52 |
| MSFT | 2002-08-01 | 19.97 |
| MSFT | 2002-09-01 | 17.79 |
| MSFT | 2002-10-01 | 21.75 |
| MSFT | 2002-11-01 | 23.46 |
| MSFT | 2002-12-01 | 21.03 |
| MSFT | 2003-01-01 | 19.31 |
| MSFT | 2003-02-01 | 19.34 |
| MSFT | 2003-03-01 | 19.76 |
| MSFT | 2003-04-01 | 20.87 |
| MSFT | 2003-05-01 | 20.09 |
| MSFT | 2003-06-01 | 20.93 |
| MSFT | 2003-07-01 | 21.56 |
| MSFT | 2003-08-01 | 21.65 |
| MSFT | 2003-09-01 | 22.69 |
| MSFT | 2003-10-01 | 21.45 |
| MSFT | 2003-11-01 | 21.1 |
| MSFT | 2003-12-01 | 22.46 |
| MSFT | 2004-01-01 | 22.69 |
| MSFT | 2004-02-01 | 21.77 |
| MSFT | 2004-03-01 | 20.46 |
| MSFT | 2004-04-01 | 21.45 |
| MSFT | 2004-05-01 | 21.53 |
| MSFT | 2004-06-01 | 23.44 |
| MSFT | 2004-07-01 | 23.38 |
| MSFT | 2004-08-01 | 22.47 |
| MSFT | 2004-09-01 | 22.76 |
| MSFT | 2004-10-01 | 23.02 |
| MSFT | 2004-11-01 | 24.6 |
| MSFT | 2004-12-01 | 24.52 |
| MSFT | 2005-01-01 | 24.11 |
| MSFT | 2005-02-01 | 23.15 |
| MSFT | 2005-03-01 | 22.24 |
| MSFT | 2005-04-01 | 23.28 |
| MSFT | 2005-05-01 | 23.82 |
| MSFT | 2005-06-01 | 22.93 |
| MSFT | 2005-07-01 | 23.64 |
| MSFT | 2005-08-01 | 25.35 |
| MSFT | 2005-09-01 | 23.83 |
| MSFT | 2005-10-01 | 23.8 |
| MSFT | 2005-11-01 | 25.71 |
| MSFT | 2005-12-01 | 24.29 |
| MSFT | 2006-01-01 | 26.14 |
| MSFT | 2006-02-01 | 25.04 |
| MSFT | 2006-03-01 | 25.36 |
| MSFT | 2006-04-01 | 22.5 |
| MSFT | 2006-05-01 | 21.19 |
| MSFT | 2006-06-01 | 21.8 |
| MSFT | 2006-07-01 | 22.51 |
| MSFT | 2006-08-01 | 24.13 |
| MSFT | 2006-09-01 | 25.68 |
| MSFT | 2006-10-01 | 26.96 |
| MSFT | 2006-11-01 | 27.66 |
| MSFT | 2006-12-01 | 28.13 |
| MSFT | 2007-01-01 | 29.07 |
| MSFT | 2007-02-01 | 26.63 |
| MSFT | 2007-03-01 | 26.35 |
| MSFT | 2007-04-01 | 28.3 |
| MSFT | 2007-05-01 | 29.11 |
| MSFT | 2007-06-01 | 27.95 |
| MSFT | 2007-07-01 | 27.5 |
| MSFT | 2007-08-01 | 27.34 |
| MSFT | 2007-09-01 | 28.04 |
| MSFT | 2007-10-01 | 35.03 |
| MSFT | 2007-11-01 | 32.09 |
| MSFT | 2007-12-01 | 34 |
| MSFT | 2008-01-01 | 31.13 |
| MSFT | 2008-02-01 | 26.07 |
| MSFT | 2008-03-01 | 27.21 |
| MSFT | 2008-04-01 | 27.34 |
| … | … | … |
13.1 scatter
A point per row at its x and y; colour, shape and size may come from the data, and :order sets which points are drawn on top.
| Option | Type | Default | Description |
|---|---|---|---|
| :x (required) | a keyword, a function or a categorical marker of one | x position (data units) | |
| :y (required) | a keyword, a function or a categorical marker of one | y position (data units) | |
| :color | a keyword, a function or a categorical marker of one or nil | point colour per row | |
| :shape | a keyword, a function or a categorical marker of one or nil | point shape per row | |
| :size | a keyword, a function or a categorical marker of one or nil | point size per row | |
| :order | a keyword, a function or a categorical marker of one or nil | draw order: points drawn by its values (see :order-direction), else in data order |
(plot/render-image {:data mtcars
:mapping {:x :wt :y :mpg :color (levels/categorical :cyl) :size :hp}
:charts [(chart/scatter)]
:scales {:color {:title "cylinders"} :size {:range [5 18]}}
:labels {:x "weight [1000 lbs]" :y "miles per gallon"}}
{:width 600 :height 380})
Its options: the point style used when nothing is mapped, the draw order of a mapped :order, and jitter - random offsets separating points on a grid of values (seeded here, so the image is the same on every build):
| Option | Type | Default | Description |
|---|---|---|---|
| :size | a non-negative number | 6 | point size in pixels (a mapped :size wins) |
| :color | a clojure2d color or :none | [69.0 117.0 180.0 180.0] | point colour (a mapped :color wins) |
| :stroke-color | a clojure2d color or :none | :none | outline colour: a colour or :none |
| :stroke-width | a non-negative number | 1 | outline width in pixels |
| :shape | a shape character, keyword or shape map | \O | point shape (a mapped :shape wins) |
| :padding | map of one of :x :y to [lo hi], non-negative numbers | 0.05 on every side | padding of the data span per position axis, {:x [lo hi] :y [lo hi]} fractions of the span |
| :order-direction | one of :ascending :descending | :ascending | draw order of the values of a mapped :order (later points on top) |
| :jitter | [a non-negative number, a non-negative number] | [dx dy] - uniform random offsets within ±dx / ±dy data units added to the positions | |
| :rng | a fastmath random generator or nil | an unseeded generator (builds differ) | random number generator for jitter (seeded for reproducible plots) |
(plot/render-image (plot/arrange [{:data mtcars
:mapping {:x :cyl :y :gear}
:charts [(chart/scatter {:size 10 :shape \s :color [200 80 40 150]})]
:labels {:title "overplotted"}}
{:data mtcars
:mapping {:x :cyl :y :gear}
:charts [(chart/scatter {:size 10 :shape \s :color [200 80 40 150]
:stroke-color :black :stroke-width 0.5
:jitter [0.3 0.2] :rng (r/rng :mersenne 7)})]
:labels {:title ":jitter [0.3 0.2]"}}]
{:columns 2})
{:width 900 :height 330})
A mapped :order draws points by its values, later ones on top: the most powerful cars over the others (ascending) or under them (descending); :padding is the room around the data, here none on x:
(plot/render-image (plot/arrange [{:data mtcars
:mapping {:x :wt :y :mpg :color :hp :order :hp}
:charts [(chart/scatter {:size 26 :stroke-color :white})]
:labels {:title ":order :hp, :order-direction :ascending"}
:legend false}
{:data mtcars
:mapping {:x :wt :y :mpg :color :hp :order :hp}
:charts [(chart/scatter {:size 26 :stroke-color :white :order-direction :descending
:padding {:x [0 0] :y [0.05 0.05]}})]
:labels {:title ":order-direction :descending, :padding x 0"}}]
{:columns 2 :widths [1 1.2]})
{:width 1000 :height 360})
Shared options: Data and mapping (:data :mapping); Named theme styles (:theme-style).
13.2 line
A polyline per group through the rows ordered by x; it may fill the area below, stack with other groups, or carry a colour or width along its length. (The dataset’s dates are text: the mapping parses them.)
| Option | Type | Default | Description |
|---|---|---|---|
| :x (required) | a keyword, a function or a categorical marker of one | x position (data units) | |
| :y (required) | a keyword, a function or a categorical marker of one | y position (data units) | |
| :color | a keyword, a function or a categorical marker of one or nil | line colour: per group (ordinal) or along the line (linear or binned) | |
| :size | a keyword, a function or a categorical marker of one or nil | line width: per group, or per point with :trail | |
| :dash | a keyword, a function or a categorical marker of one or nil | dash style per group | |
| :pattern | a keyword, a function or a categorical marker of one or nil | area fill pattern per group | |
| :group | a keyword, a function or a categorical marker of one or nil | groups without a visual aesthetic, each its own line |
(plot/render-image {:data stocks
:mapping {:x #(java.time.LocalDate/parse (:date %)) :y :price :color :symbol}
:charts [(chart/line)]
:labels {:x "date" :y "price [USD]"}}
{:width 700 :height 380})
13.2.1 Style
The area under a line (:fill), its gradient, the stroke’s opacity and the trail - a filled band whose width follows a mapped :size:
| Option | Type | Default | Description |
|---|---|---|---|
| :fill | :color or a clojure2d color or :none | :none | area between the line and y = 0: :color (the line colour at :alpha), a colour or :none |
| :alpha | a number in [0 1] | 0.6 | area fill opacity of :fill :color and of stacked areas |
| :fill-gradient | a clojure2d gradient: a preset keyword, a colour list or a function | clojure2d gradient (preset, colours or function of t) colouring the areas; wins over :fill | |
| :fill-gradient-axis | one of :x :y | :x | axis the :fill-gradient runs along, over that scale's range (needs :fill-gradient) |
| :stroke-alpha | a number in [0 1] | multiplies the stroke colour's opacity of unstacked lines (the legend key stays opaque) | |
| :trail | true or false or nil | false | draw a filled trail whose width follows a linear mapped :size at every point |
(def wave (for [t (range 40)] {:t t :v (+ 1.5 (Math/sin (* 0.25 t))) :w (+ 1 (mod t 10))}))| t | v | w |
|---|---|---|
| 0 | 1.5 | 1 |
| 1 | 1.747403959254523 | 2 |
| 2 | 1.979425538604203 | 3 |
| 3 | 2.181638760023334 | 4 |
| 4 | 2.3414709848078967 | 5 |
| 5 | 2.448984619355586 | 6 |
| 6 | 2.4974949866040546 | 7 |
| 7 | 2.483985946873937 | 8 |
| 8 | 2.409297426825682 | 9 |
| 9 | 2.278073196887921 | 10 |
| 10 | 2.0984721441039564 | 1 |
| 11 | 1.8816609920523317 | 2 |
| 12 | 1.6411200080598671 | 3 |
| 13 | 1.3918048654698916 | 4 |
| 14 | 1.14921677231038 | 5 |
| 15 | 0.9284386812576563 | 6 |
| 16 | 0.7431975046920718 | 7 |
| 17 | 0.6050106417714165 | 8 |
| 18 | 0.522469882334903 | 9 |
| 19 | 0.500707211024622 | 10 |
| 20 | 0.5410757253368615 | 1 |
| 21 | 0.641065506573408 | 2 |
| 22 | 0.7944596744296081 | 3 |
| 23 | 0.9917209225007416 | 4 |
| 24 | 1.2205845018010741 | 5 |
| 25 | 1.4668207834524432 | 6 |
| 26 | 1.7151199880878156 | 7 |
| 27 | 1.9500440737806177 | 8 |
| 28 | 2.156986598718789 | 9 |
| 29 | 2.3230808790115054 | 10 |
| 30 | 2.437999976774739 | 1 |
| 31 | 2.4945987791111763 | 2 |
| 32 | 2.489358246623382 | 3 |
| 33 | 2.4226042102393404 | 4 |
| 34 | 2.2984871126234903 | 5 |
| 35 | 2.1247239537541924 | 6 |
| 36 | 1.9121184852417565 | 7 |
| 37 | 1.6738894853804336 | 8 |
| 38 | 1.4248488795381906 | 9 |
| 39 | 1.1804808063777263 | 10 |
(plot/render-image (plot/arrange [{:data wave :mapping {:x :t :y :v}
:charts [(chart/line {:fill :color :alpha 0.3})]
:labels {:title ":fill :color"}}
{:data wave :mapping {:x :t :y :v}
:charts [(chart/line {:fill-gradient :mpl/viridis :fill-gradient-axis :y})]
:labels {:title ":fill-gradient"}}
{:data wave :mapping {:x :t :y :v :size :w}
:charts [(chart/line {:trail true})]
:labels {:title ":trail true"}
:legend false}]
{:columns 3})
{:width 1000 :height 300})
The gradient along x instead, and :stroke-alpha fading the strokes of overlapping lines:
(plot/render-image (plot/arrange [{:data wave :mapping {:x :t :y :v}
:charts [(chart/line {:fill-gradient :mpl/viridis :fill-gradient-axis :x})]
:labels {:title ":fill-gradient-axis :x"}}
{:data stocks
:mapping {:x #(java.time.LocalDate/parse (:date %)) :y :price :color :symbol}
:charts [(chart/line {:width 3 :stroke-alpha 0.4})]
:labels {:title ":stroke-alpha 0.4"}}]
{:columns 2 :widths [1 1.4]})
{:width 1000 :height 300})
13.2.2 Path
How points are joined: sorted by x or in data order, what a missing value does (a break or a bridge), how groups combine (the Layering and position chapter shows :stack and the streamgraphs), and the padding:
| Option | Type | Default | Description |
|---|---|---|---|
| :bridge | map | {:alpha 0.3} | style of the segments bridging gaps with :gaps :bridge: :alpha (a fraction of the line alpha), optional :width and :dash |
| :bridge :alpha (required) | a number in [0 1] | 0.3 in the theme's :bridge (a given :bridge replaces it whole) | bridge opacity, a fraction of the line's alpha |
| :bridge :width | a non-negative number | the line's width | bridge width in pixels |
| :bridge :dash | :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 nil | the line's dash | dash style of the bridge |
| :sort | true or false or nil | true | order the points by numeric or temporal x (false - data order) |
| :gaps | one of :break :bridge | :break (stacked lines take no :gaps) | an invalid y breaks the line: :break (empty space) or :bridge (a straight segment in the :bridge style) |
| :position | one of :normalize :overlay :silhouette :stack :wiggle | :overlay | how groups combine: :overlay, stacked :stack / :normalize, or streamgraph :silhouette / :wiggle |
| :padding | map of one of :x :y to [lo hi], non-negative numbers | 0.05 on every side; y [0 0.05] when stacked without negative values | padding of the data span per position axis, {:x [lo hi] :y [lo hi]} fractions of the span |
(def spiral (for [t (range 0 12 0.25)] {:x (* t (Math/cos t)) :y (* t (Math/sin t)) :v (when-not (< 5 t 6) t)}))| x | y | v |
|---|---|---|
| 0.0 | 0.0 | 0 |
| 0.24222810542766118 | 0.061850989813630734 | 0.25 |
| 0.4387912809451864 | 0.2397127693021015 | 0.5 |
| 0.5487666516553656 | 0.5112290700175006 | 0.75 |
| 0.5403023058681398 | 0.8414709848078965 | 1.0 |
| 0.3941529529940858 | 1.1862307741944829 | 1.25 |
| 0.10610580250155435 | 1.4962424799060816 | 1.5 |
| -0.31193059738661116 | 1.7219754070293896 | 1.75 |
| -0.8322936730942848 | 1.8185948536513634 | 2.0 |
| -1.4133906511261631 | 1.7506646929978227 | 2.25 |
| -2.002859038867334 | 1.496180360259891 | 2.5 |
| -2.541831541239275 | 1.0495677281439122 | 2.75 |
| -2.9699774898013365 | 0.4233600241796016 | 3.0 |
| -3.2309214472617755 | -0.3516341872228522 | 3.25 |
| -3.277598405517787 | -1.2277412969136694 | 3.5 |
| -3.077097590023353 | -2.143354945283789 | 3.75 |
| -2.6145744834544478 | -3.027209981231713 | 4.0 |
| -1.8958718321336194 | -3.80370477247148 | 4.25 |
| -0.9485810974385086 | -4.3988855294929365 | 4.5 |
| 0.17861022621788863 | -4.746640747633045 | 4.75 |
| 1.4183109273161312 | -4.794621373315692 | 5.0 |
| 2.688448755519664 | -4.509406090489608 | |
| 3.89768375860193 | -3.8804717906371557 | |
| 4.9518563986787445 | -2.922604695620736 | |
| 5.761021719902196 | -1.6764929891935552 | 6.0 |
| 6.246558863903121 | -0.2073701034222301 | 6.25 |
| 6.347819567232152 | 1.398279922570801 | 6.5 |
| 6.027792826651267 | 3.037797498019169 | 6.75 |
| 5.277315780403132 | 4.598906191031523 | 7.0 |
| 4.117450256343037 | 5.967336372833415 | 7.25 |
| 2.5997648837626937 | 7.034999825810542 | 7.5 |
| 0.8044062684492105 | 7.708140538111615 | 7.75 |
| -1.1640002704689083 | 7.914865972987054 | 8.0 |
| -3.1824204839808297 | 7.611484734474557 | 8.25 |
| -5.117101172821001 | 6.787140457299667 | 8.5 |
| -6.832399731550305 | 5.466334595349184 | 8.75 |
| -8.200172356962092 | 3.7090663671758093 | 9.0 |
| -9.109077854572744 | 1.6084777397690104 | 9.25 |
| -9.473135483865596 | -0.7139356443871884 | 9.5 |
| -9.238903088785435 | -3.115312137817168 | 9.75 |
| -8.390715290764524 | -5.440211108893697 | 10.0 |
| -6.953536967356914 | -7.530658911649153 | 10.25 |
| -4.993137743957921 | -9.236805479702536 | 10.5 |
| -2.6134692925310725 | -10.427477080147295 | 10.75 |
| 0.04868267786855864 | -10.999892272057739 | 11.0 |
| 2.831508563307237 | -10.887839972001691 | 11.25 |
| 5.5580047256595675 | -10.067700008916928 | 11.5 |
| 8.047226241006753 | -8.561813465969248 | 11.75 |
(plot/render-image (plot/arrange [{:data spiral :mapping {:x :x :y :y}
:charts [(chart/line)]
:labels {:title ":sort true (default)"}}
{:data spiral :mapping {:x :x :y :y}
:charts [(chart/line {:sort false})]
:labels {:title ":sort false"}}
{:data spiral :mapping {:x :x :y :v}
:charts [(chart/line {:sort false :gaps :break :padding {:x [0 0] :y [0 0]}})]
:labels {:title ":gaps :break, no padding"}}
{:data spiral :mapping {:x :x :y :v}
:charts [(chart/line {:sort false :gaps :bridge :bridge {:alpha 0.5 :dash :dotted}})]
:labels {:title ":gaps :bridge"}}]
{:columns 2})
{:width 900 :height 600})
Groups combined by :position (:overlay, the default, draws each line on its own):
(def streams (for [series ["a" "b" "c"] t (range 25)]
{:series series :t t :v (+ 1.5 (Math/sin (+ (* 0.3 t) (* 2 (count series)) (int (first series)))))}))| series | t | v |
|---|---|---|
| a | 0 | 0.5007931658136463 |
| a | 1 | 0.5571891259213132 |
| a | 2 | 0.6978035734827992 |
| a | 3 | 0.9100758386825973 |
| a | 4 | 1.175044272270521 |
| a | 5 | 1.4690400332167286 |
| a | 6 | 1.7658013557891272 |
| a | 7 | 2.0388194348720936 |
| a | 8 | 2.263706378577581 |
| a | 9 | 2.4203737059941695 |
| a | 10 | 2.494826791358406 |
| a | 11 | 2.4804149622946956 |
| a | 12 | 2.378425584571251 |
| a | 13 | 2.1979690655501094 |
| a | 14 | 1.9551650486306003 |
| a | 15 | 1.6717024935127782 |
| a | 16 | 1.3729022660226193 |
| a | 17 | 1.0854553005796792 |
| a | 18 | 0.8350383785176917 |
| a | 19 | 0.6440204976799755 |
| a | 20 | 0.5294647164625153 |
| a | 21 | 0.5016039616255725 |
| a | 22 | 0.5629269514224042 |
| a | 23 | 0.707955885809737 |
| a | 24 | 0.9237357620113366 |
| b | 0 | 0.9936343588902412 |
| b | 1 | 1.2710830775547932 |
| b | 2 | 1.5689802631292735 |
| b | 3 | 1.8607156472389814 |
| b | 4 | 2.120229376882631 |
| b | 5 | 2.32433986348825 |
| b | 6 | 2.4548145251796623 |
| b | 7 | 2.499998449014294 |
| b | 8 | 2.455855489645069 |
| b | 9 | 2.3263288061636223 |
| b | 10 | 2.122988631442349 |
| b | 11 | 1.8639987376909781 |
| b | 12 | 1.5724939207813549 |
| b | 13 | 1.2745134378334269 |
| b | 14 | 0.9966749979282254 |
| b | 15 | 0.7637970814297974 |
| b | 16 | 0.5966819790500097 |
| b | 17 | 0.5102575851736925 |
| b | 18 | 0.5122439335122633 |
| b | 19 | 0.6024635894844816 |
| b | 20 | 0.7728574999191474 |
| b | 21 | 1.0082048842730023 |
| b | 22 | 1.2874828616253495 |
| b | 23 | 1.5857443620192933 |
| b | 24 | 1.8763465739222898 |
| c | 0 | 1.9520257871783506 |
| c | 1 | 2.1954421919799203 |
| c | 2 | 2.3767368169734757 |
| c | 3 | 2.479715153249283 |
| c | 4 | 2.4951784527231693 |
| c | 5 | 2.4217454249067294 |
| c | 6 | 2.2659756234728015 |
| c | 7 | 2.041783500861879 |
| c | 8 | 1.76919547168632 |
| c | 9 | 1.4725610127567745 |
| c | 10 | 1.178377596837469 |
| c | 11 | 0.9129237523203599 |
| c | 12 | 0.6999116805479259 |
| c | 13 | 0.5583691154881323 |
| c | 14 | 0.5009396329284678 |
| c | 15 | 0.5327532375065523 |
| c | 16 | 0.6509681146743322 |
| c | 17 | 0.8450244815280111 |
| c | 18 | 1.0975878607651728 |
| c | 19 | 1.3860975179157289 |
| c | 20 | 1.6847817445606674 |
| c | 21 | 1.9669599682904564 |
| c | 22 | 2.2074260487769513 |
| c | 23 | 2.3846998672186963 |
| c | 24 | 2.482946081380238 |
(plot/render-image (plot/arrange [{:data streams :mapping {:x :t :y :v :color :series}
:charts [(chart/line {:position :stack})] :labels {:title ":stack"} :legend false}
{:data streams :mapping {:x :t :y :v :color :series}
:charts [(chart/line {:position :normalize})] :labels {:title ":normalize"} :legend false}
{:data streams :mapping {:x :t :y :v :color :series}
:charts [(chart/line {:position :silhouette})] :labels {:title ":silhouette"} :legend false}
{:data streams :mapping {:x :t :y :v :color :series}
:charts [(chart/line {:position :wiggle})] :labels {:title ":wiggle"}}]
{:columns 4 :widths [1 1 1 1.3]})
{:width 1000 :height 280})
13.2.3 Ends
Arrowheads at the ends of each run and gaps shortening it - e.g. to stop short of a marker:
| Option | Type | Default | Description |
|---|---|---|---|
| :arrow | map | arrowheads at the ends of every run of a plain stroked line | |
| :arrow :end | true or false | true | a head at the run's end |
| :arrow :start | true or false | false | a head at the run's start |
| :arrow :size | a positive number | 8.0 | head length in pixels |
| :arrow :angle | number, > 0, < 90 | 20.0 | angle in degrees between each side of the head and the line |
| :arrow :shape | one of :closed :open | :closed | :closed (a filled triangle) or :open (two strokes) |
| :start-gap | a non-negative number | shortens every run at its start, in pixels | |
| :end-gap | a non-negative number | shortens every run at its end, in pixels |
(def path-points [{:x 0 :y 0} {:x 1 :y 2} {:x 2 :y 1} {:x 3 :y 3}])| x | y |
|---|---|
| 0 | 0 |
| 1 | 2 |
| 2 | 1 |
| 3 | 3 |
(plot/render-image (plot/arrange [{:data path-points :mapping {:x :x :y :y}
:charts [(chart/line {:arrow {:end true}})]
:labels {:title ":arrow {:end true}"}}
{:data path-points :mapping {:x :x :y :y}
:charts [(chart/scatter {:size 14})
(chart/line {:arrow {:start true :end true :shape :open :size 12}
:start-gap 9 :end-gap 9})]
:labels {:title "open heads, gaps 9 px"}}]
{:columns 2})
{:width 800 :height 300})
Shared options: Line style (:color :width :dash); Fill patterns (:pattern :pattern-color); Data and mapping (:data :mapping); Named theme styles (:theme-style); Curves (:interpolator :interpolator-params :curve :sample-step).
13.3 segment
A straight piece per row from (x, y) to (xend, yend); the ends may be mapped or given as literals, with arrowheads and gaps as on lines.
| Option | Type | Default | Description |
|---|---|---|---|
| :x (required) | a keyword, a function or a categorical marker of one | start x position (data units) | |
| :y (required) | a keyword, a function or a categorical marker of one | start y position (data units) | |
| :xend | a keyword, a function or a categorical marker of one or nil | end x position (data units) | |
| :yend | a keyword, a function or a categorical marker of one or nil | end y position (data units) | |
| :end-gap | a keyword, a function or a categorical marker of one or nil | end gap per row in pixels (e.g. the radius of the node an edge points at) | |
| :color | a keyword, a function or a categorical marker of one or nil | segment colour: per group (ordinal) or per row (linear or binned) | |
| :size | a keyword, a function or a categorical marker of one or nil | line width: per row (linear) or per group (ordinal) | |
| :dash | a keyword, a function or a categorical marker of one or nil | dash style per group | |
| :group | a keyword, a function or a categorical marker of one or nil | groups without a visual aesthetic (one stroke setup per group) |
(def moves [{:from "a" :x 1 :y 1 :x2 3 :y2 2} {:from "b" :x 2 :y 4 :x2 4 :y2 3} {:from "c" :x 3 :y 0.5 :x2 1.5 :y2 3}])| from | x | y | x2 | y2 |
|---|---|---|---|---|
| a | 1 | 1 | 3 | 2 |
| b | 2 | 4 | 4 | 3 |
| c | 3 | 0.5 | 1.5 | 3 |
(plot/render-image {:data moves
:mapping {:x :x :y :y :xend :x2 :yend :y2 :color :from}
:charts [(chart/segment {:width 2})]}
{:width 500 :height 320})
Its options: literal ends (here every segment starts at x = 0, a lollipop stem), arrows and gaps:
| Option | Type | Default | Description |
|---|---|---|---|
| :xend | a finite number, a java.time value or a level (string / keyword) or nil | the mapped :xend, else the start x | literal end x of every segment: a number, a java.time value or a level (wins over a mapped :xend) |
| :yend | a finite number, a java.time value or a level (string / keyword) or nil | the mapped :yend, else the start y | literal end y of every segment: a number, a java.time value or a level (wins over a mapped :yend) |
| :padding | map of one of :x :y to [lo hi], non-negative numbers | 0.05 on every side; 0 on a side where a literal :xend / :yend is the end of the axis extent | padding of the data span per position axis, {:x [lo hi] :y [lo hi]} fractions of the span |
| :arrow | map | arrowheads at the ends of every segment | |
| :arrow :end | true or false | true | a head at the run's end |
| :arrow :start | true or false | false | a head at the run's start |
| :arrow :size | a positive number | 8.0 | head length in pixels |
| :arrow :angle | number, > 0, < 90 | 20.0 | angle in degrees between each side of the head and the line |
| :arrow :shape | one of :closed :open | :closed | :closed (a filled triangle) or :open (two strokes) |
| :start-gap | a non-negative number | shortens every segment at its start, in pixels | |
| :end-gap | a non-negative number | shortens every segment at its end, in pixels (a mapped :end-gap wins) |
(plot/render-image (plot/arrange [{:data mtcars
:mapping {:x :mpg :y :model}
:charts [(chart/segment {:xend 0}) (chart/scatter)]
:scales {:y {:reverse true}}
:labels {:title ":xend 0"}}
{:data moves
:mapping {:x :x :y :y :xend :x2 :yend :y2}
:charts [(chart/scatter {:size 10})
(chart/segment {:arrow {:end true :size 10} :end-gap 4})]
:labels {:title ":arrow, :end-gap 4"}}]
{:columns 2 :widths [1.3 1]})
{:width 1000 :height 620})
(plot/render-image (plot/arrange [{:data moves
:mapping {:x :x :y :y :xend :x2}
:charts [(chart/segment {:yend 4.5 :dash :dashed}) (chart/scatter)]
:labels {:title ":yend 4.5 (one end for all)"}}
{:data moves
:mapping {:x :x :y :y :xend :x2 :yend :y2}
:charts [(chart/scatter {:size 14})
(chart/segment {:start-gap 10 :end-gap 10 :padding {:x [0.2 0.2] :y [0.2 0.2]}})]
:labels {:title ":start-gap 10, :padding 0.2"}}]
{:columns 2})
{:width 800 :height 320})
Shared options: Line style (:color :width :dash); Data and mapping (:data :mapping); Named theme styles (:theme-style).
13.4 polygon
A filled shape per group through its rows in data order.
| Option | Type | Default | Description |
|---|---|---|---|
| :x (required) | a keyword, a function or a categorical marker of one | x position of a vertex (data units) | |
| :y (required) | a keyword, a function or a categorical marker of one | y position of a vertex (data units) | |
| :color | a keyword, a function or a categorical marker of one or nil | fill colour: per group (ordinal), or by the group's first row (linear or binned) | |
| :group | a keyword, a function or a categorical marker of one or nil | groups without a visual aesthetic, each its own polygon |
(def shapes (concat (for [a (range 0 360 60)]
{:shape "hexagon" :x (Math/cos (Math/toRadians a)) :y (Math/sin (Math/toRadians a))})
(for [[x y] [[1.5 -1] [3 -1] [2.25 1]]] {:shape "triangle" :x x :y y})))| shape | x | y |
|---|---|---|
| hexagon | 1.0 | 0.0 |
| hexagon | 0.5000000000000001 | 0.8660254037844386 |
| hexagon | -0.4999999999999998 | 0.8660254037844387 |
| hexagon | -1.0 | 1.2246467991473532E-16 |
| hexagon | -0.5000000000000004 | -0.8660254037844384 |
| hexagon | 0.5000000000000001 | -0.8660254037844386 |
| triangle | 1.5 | -1 |
| triangle | 3 | -1 |
| triangle | 2.25 | 1 |
(plot/render-image {:data shapes
:mapping {:x :x :y :y :color :shape}
:charts [(chart/polygon)]}
{:width 500 :height 260})
Its options: the fill, its opacity and the outline (:color - the group’s colour, opaque):
| Option | Type | Default | Description |
|---|---|---|---|
| :color | a clojure2d color or :none | [69.0 117.0 180.0 255.0] | fill colour (a mapped :color wins) |
| :alpha | a number in [0 1] | 1.0 | fill opacity |
| :stroke-color | :color or a clojure2d color or :none | :none | outline colour: a colour, :none or :color (the group colour, opaque) |
| :stroke-width | a non-negative number | 1 | outline width in pixels |
| :padding | map of one of :x :y to [lo hi], non-negative numbers | 0.05 on every side | padding of the data span per position axis, {:x [lo hi] :y [lo hi]} fractions of the span |
(plot/render-image {:data shapes
:mapping {:x :x :y :y :color :shape}
:charts [(chart/polygon {:alpha 0.35 :stroke-color :color :stroke-width 2})]}
{:width 500 :height 260})
Without a mapped colour every polygon takes :color; :padding 0 lets the shapes touch the panel’s edges:
(plot/render-image {:data shapes
:mapping {:x :x :y :y :group :shape}
:charts [(chart/polygon {:color [120 160 90] :stroke-color :black :padding {:x [0 0] :y [0 0]}})]}
{:width 500 :height 260})
Shared options: Data and mapping (:data :mapping); Named theme styles (:theme-style).
source: notebooks/reference/charts_points_paths.clj