18  Charts: distribution statistics

Charts computing a statistic of the values on one axis: binned counts (histogram), kernel density curves (density), the empirical distribution function (ecdf) and the comparison of two distributions by quantiles (qq) or probabilities (pp).

(ns reference.charts-distribution-stats
  (:require [fastmath.random :as r]
            [cljplot2.chart :as chart]
            [cljplot2.core :as plot]
            [cljplot2.describe :as describe]
            [notebook-utils :as nu]))

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

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

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

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

Two species only - for the charts drawing two groups against each other:

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

18.1 histogram

Bars counting the values falling into each bin; groups overlay, dodge, stack or mirror, and a categorical :y gives one histogram per level (ridgelines).

Option Type Default Description
:x (required) a keyword, a function or a categorical marker of one values to bin (data units)
:y a keyword, a function or a categorical marker of one or nil ridgeline levels: categorical or on a :bands scale, one histogram per level (a numeric :y otherwise is ignored)
:color a keyword, a function or a categorical marker of one or nil groups, each with its own bar fill colour
:pattern a keyword, a function or a categorical marker of one or nil groups, each with its own fill pattern
:group a keyword, a function or a categorical marker of one or nil groups without a visual aesthetic
(plot/render-image {:data iris
                    :mapping {:x :petal-length :color :species}
                    :charts [(chart/histogram)]
                    :labels {:x "petal length [cm]"}}
                   {:width 600 :height 350})

Its options: the bins (a method, a count or breaks), what a bar’s height measures, how groups combine, the bar style and the zero line of mirrored groups:

Option Type Default Description
:color a clojure2d color [69.0 117.0 180.0 255.0] bar fill colour (a mapped :color wins)
:stroke-color :darker or a clojure2d color or :none :darker outline colour: a colour, :none or :darker (the darkened fill)
:stroke-width a non-negative number 1 outline width in pixels
:alpha a number in [0 1] 0.6 fill opacity of grouped bars with :position :overlay (other bars are opaque)
:zero-line-color a clojure2d color or :none :black colour of the y = 0 line drawn with :position :mirror
:zero-line-width a non-negative number 1 width in pixels of the y = 0 line drawn with :position :mirror
: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 :sturges bins shared by all panels and groups: a fastmath estimation method (:sturges :sqrt :rice :doane :scott :freedman-diaconis), a bin count or increasing breaks (values outside dropped)
:y one of :cumulative :percent :count :pmf :density :percent bar height: :percent (of the panel's or group's total), :count, :pmf (shares summing to 1), :density (area 1) or :cumulative (count of the bin and every bin before it)
:position one of :dodge :mirror :normalize :overlay :stack :overlay grouped bars: :overlay (translucent), :dodge, :stack, :normalize (stacked to 1) or :mirror (two groups, the second downward); ridgelines: :overlay or :mirror
:symmetric true or false or nil false with :position :mirror, tie both halves to one y range [-M M], M the tallest bar over all panels
:zero-line true or false or nil true with :position :mirror, draw the y = 0 line over the bars
:padding map of one of :x :y to [lo hi], non-negative numbers {:x [0.05 0.05] :y [0 0.05]} (bars stand on the panel's bottom edge); y [0.05 0.05] with :position :mirror padding of the data span per position axis, {:x [lo hi] :y [lo hi]} fractions of the span
(plot/render-image (plot/arrange [{:data faithful
                                   :mapping {:x :eruptions}
                                   :charts [(chart/histogram {:bins 30 :y :count :stroke-color :none})]
                                   :labels {:title ":bins 30 :y :count"}}
                                  {:data faithful
                                   :mapping {:x :eruptions}
                                   :charts [(chart/histogram {:bins [1.5 2 2.5 3 3.5 4 4.5 5 5.5] :y :density
                                                              :color [120 160 90] :stroke-color :white})]
                                   :labels {:title "breaks, :y :density"}}
                                  {:data two-species
                                   :mapping {:x :sepal-length :color :species}
                                   :charts [(chart/histogram {:position :mirror :bins 12 :zero-line-color :red
                                                              :zero-line-width 2})]
                                   :labels {:title ":position :mirror"}
                                   :legend false}]
                                 {:columns 3})
                   {:width 1000 :height 320})

What a bar’s height measures - the same bins as shares summing to 1 (:pmf, the default :percent scaled to 100), and the running count (:cumulative):

(plot/render-image (plot/arrange [{:data faithful
                                   :mapping {:x :eruptions}
                                   :charts [(chart/histogram {:bins 20 :y :pmf})]
                                   :labels {:title ":y :pmf"}}
                                  {:data faithful
                                   :mapping {:x :eruptions}
                                   :charts [(chart/histogram {:bins 20 :y :cumulative})]
                                   :labels {:title ":y :cumulative"}}]
                                 {:columns 2})
                   {:width 900 :height 300})

How groups share the bins: translucent over each other (:alpha of :overlay), side by side, stacked, or stacked to 1 - the share of each species in every bin:

(plot/render-image (plot/arrange [{:data iris
                                   :mapping {:x :petal-length :color :species}
                                   :charts [(chart/histogram {:position :overlay :alpha 0.4 :bins 20})]
                                   :labels {:title ":position :overlay :alpha 0.4"}
                                   :legend false}
                                  {:data iris
                                   :mapping {:x :petal-length :color :species}
                                   :charts [(chart/histogram {:position :dodge :bins 12})]
                                   :labels {:title ":position :dodge"}
                                   :legend false}
                                  {:data iris
                                   :mapping {:x :petal-length :color :species}
                                   :charts [(chart/histogram {:position :stack :bins 20})]
                                   :labels {:title ":position :stack"}
                                   :legend false}
                                  {:data iris
                                   :mapping {:x :petal-length :color :species}
                                   :charts [(chart/histogram {:position :normalize :bins 20})]
                                   :labels {:title ":position :normalize"}
                                   :legend false}]
                                 {:columns 2})
                   {:width 900 :height 560})

Mirrored groups: halves tied to one y range [-M M] (:symmetric) or each fitted to its own tallest bar, the zero line off; thick outlines and no padding above the bars:

(plot/render-image (plot/arrange [{:data two-species
                                   :mapping {:x :sepal-length :color :species}
                                   :charts [(chart/histogram {:position :mirror :bins 12 :symmetric true})]
                                   :labels {:title ":symmetric true"}
                                   :legend false}
                                  {:data two-species
                                   :mapping {:x :sepal-length :color :species}
                                   :charts [(chart/histogram {:position :mirror :bins 12 :symmetric false :zero-line false})]
                                   :labels {:title ":symmetric false :zero-line false"}
                                   :legend false}
                                  {:data faithful
                                   :mapping {:x :eruptions}
                                   :charts [(chart/histogram {:bins 15 :stroke-color :black :stroke-width 2
                                                              :padding {:y [0 0]}})]
                                   :labels {:title ":stroke-width 2 :padding {:y [0 0]}"}}]
                                 {:columns 3})
                   {:width 1000 :height 320})

Shared options: Fill patterns (:pattern :pattern-color); Data and mapping (:data :mapping); Named theme styles (:theme-style); Rounded corners (:corner-radius :corner-radius-end); Ridgelines (:overlap).

18.2 density

A kernel density estimate per group drawn as a curve, optionally filled; groups overlay, stack or mirror, and a categorical :y gives ridgelines.

Option Type Default Description
:x (required) a keyword, a function or a categorical marker of one values whose density is estimated (data units)
:y a keyword, a function or a categorical marker of one or nil ridgeline levels: categorical or on a :bands scale, one curve per level (a numeric :y otherwise is ignored)
:color a keyword, a function or a categorical marker of one or nil groups, each a curve in its own colour
:dash a keyword, a function or a categorical marker of one or nil groups, each a curve with its own dash style
:pattern a keyword, a function or a categorical marker of one or nil groups, each area with its own fill pattern
:group a keyword, a function or a categorical marker of one or nil groups without a visual aesthetic, each its own curve
(plot/render-image {:data iris
                    :mapping {:x :petal-length :color :species}
                    :charts [(chart/density)]
                    :labels {:x "petal length [cm]"}}
                   {:width 600 :height 350})

18.2.1 Style

The area under a curve, its opacity, a gradient fill of ridgelines and the zero line of mirrored curves:

Option Type Default Description
:fill :color or a clojure2d color or :none :none area under the curve: a colour, :color (the curve's colour with opacity :alpha) or :none; stacked positions and ridgelines fill with the curve's colour unless set
:alpha a number in [0 1] 0.6 fill opacity of areas in the curve's colour
:zero-line-color a clojure2d color or :none :black colour of the y = 0 line drawn with :position :mirror
:zero-line-width a non-negative number 1 width in pixels of the y = 0 line drawn with :position :mirror
:fill-gradient a clojure2d gradient: a preset keyword, a colour list or a function ridgelines only: fill colour along :fill-gradient-axis, a clojure2d gradient (preset keyword, colour list or function of t in [0 1]) (wins over :fill)
:fill-gradient-axis one of :x :y :x; only with ridgelines and :fill-gradient axis of the :fill-gradient: :x (over the x scale's domain) or :y (from each ridge's baseline to the top of the tallest curve)
(plot/render-image (plot/arrange [{:data iris
                                   :mapping {:x :petal-length :color :species}
                                   :charts [(chart/density {:fill :color :alpha 0.3})]
                                   :labels {:title ":fill :color :alpha 0.3"}
                                   :legend false}
                                  {:data iris
                                   :mapping {:x :sepal-length :y :species}
                                   :charts [(chart/density {:fill-gradient :mpl/viridis})]
                                   :scales {:y {:reverse true}}
                                   :labels {:title "ridgelines, :fill-gradient"}}
                                  {:data two-species
                                   :mapping {:x :sepal-length :color :species}
                                   :charts [(chart/density {:position :mirror :fill :color
                                                            :zero-line-color :red :zero-line-width 2})]
                                   :labels {:title ":zero-line-color :red"}
                                   :legend false}]
                                 {:columns 3})
                   {:width 1000 :height 320})

The ridgelines’ gradient along x (over the x domain) or along y (from each ridge’s baseline up):

(plot/render-image (plot/arrange [{:data iris
                                   :mapping {:x :sepal-length :y :species}
                                   :charts [(chart/density {:fill-gradient :mpl/plasma :fill-gradient-axis :x})]
                                   :scales {:y {:reverse true}}
                                   :labels {:title ":fill-gradient-axis :x"}}
                                  {:data iris
                                   :mapping {:x :sepal-length :y :species}
                                   :charts [(chart/density {:fill-gradient :mpl/plasma :fill-gradient-axis :y})]
                                   :scales {:y {:reverse true}}
                                   :labels {:title ":fill-gradient-axis :y"}}]
                                 {:columns 2})
                   {:width 900 :height 320})

18.2.2 Estimate

The kernel, its bandwidth (in data units or a selector), the number of evaluation points and how far a curve reaches past the data:

Option Type Default Description
:kernel keyword :gaussian fastmath KDE kernel
:bandwidth a positive number or keyword :nrd0 kernel bandwidth in data units, or a fastmath bandwidth selector
:n integer, >= 2 or nil 512 number of evaluation points of each curve
:cut a non-negative number or nil 3 how many bandwidths the curve extends past the extreme values on each side
(plot/render-image (plot/arrange [{:data faithful
                                   :mapping {:x :eruptions}
                                   :charts [(chart/density)]
                                   :labels {:title "default (:gaussian, :nrd0)"}}
                                  {:data faithful
                                   :mapping {:x :eruptions}
                                   :charts [(chart/density {:bandwidth 0.1})]
                                   :labels {:title ":bandwidth 0.1"}}
                                  {:data faithful
                                   :mapping {:x :eruptions}
                                   :charts [(chart/density {:kernel :uniform :cut 0})]
                                   :labels {:title ":kernel :uniform :cut 0"}}]
                                 {:columns 3})
                   {:width 1000 :height 300})

A bandwidth selector instead of a number, few evaluation points (the curve drawn through 8 points), and a curve reaching 3 bandwidths past the data:

(plot/render-image (plot/arrange [{:data faithful
                                   :mapping {:x :eruptions}
                                   :charts [(chart/density {:bandwidth :sj})]
                                   :labels {:title ":bandwidth :sj"}}
                                  {:data faithful
                                   :mapping {:x :eruptions}
                                   :charts [(chart/density {:n 8})]
                                   :labels {:title ":n 8"}}
                                  {:data faithful
                                   :mapping {:x :eruptions}
                                   :charts [(chart/density {:cut 3})]
                                   :labels {:title ":cut 3"}}]
                                 {:columns 3})
                   {:width 1000 :height 300})

18.2.3 Position

How groups combine (the Layering and position chapter shows the streamgraphs), a common y range of the mirrored halves and the padding:

Option Type Default Description
:position one of :mirror :normalize :overlay :silhouette :stack :wiggle :overlay grouped curves: :overlay, :stack, :normalize (stacked, summing to 1), streamgraphs :silhouette and :wiggle, or :mirror (two groups, the second drawn downward); ridgelines take :overlay or :mirror
:symmetric true or false or nil false with :position :mirror, tie both halves to one y range [-M M], M the tallest curve over all panels
:zero-line true or false or nil true with :position :mirror, draw the y = 0 line over the curves
:padding map of one of :x :y to [lo hi], non-negative numbers {:x [0.05 0.05] :y [0 0.05]} (curves stand on the panel's bottom edge); y [0.05 0.05] with :mirror, :silhouette and :wiggle padding of the data span per position axis, {:x [lo hi] :y [lo hi]} fractions of the span
(plot/render-image (plot/arrange [{:data iris
                                   :mapping {:x :petal-length :color :species}
                                   :charts [(chart/density {:position :stack})]
                                   :labels {:title ":position :stack"}
                                   :legend false}
                                  {:data two-species
                                   :mapping {:x :petal-length :color :species}
                                   :charts [(chart/density {:position :mirror :fill :color})]
                                   :labels {:title ":position :mirror"}
                                   :legend false}
                                  {:data two-species
                                   :mapping {:x :petal-length :color :species}
                                   :charts [(chart/density {:position :mirror :fill :color :symmetric true})]
                                   :labels {:title ":symmetric true"}}]
                                 {:columns 3 :widths [1 1 1.4]})
                   {:width 1000 :height 320})

Stacked to 1 (each species’ share of the density at every x), the streamgraphs, and a mirrored pair without the zero line and with no padding:

(plot/render-image (plot/arrange [{:data iris
                                   :mapping {:x :petal-length :color :species}
                                   :charts [(chart/density {:position :normalize})]
                                   :labels {:title ":position :normalize"}
                                   :legend false}
                                  {:data iris
                                   :mapping {:x :petal-length :color :species}
                                   :charts [(chart/density {:position :silhouette})]
                                   :labels {:title ":position :silhouette"}
                                   :legend false}
                                  {:data iris
                                   :mapping {:x :petal-length :color :species}
                                   :charts [(chart/density {:position :wiggle})]
                                   :labels {:title ":position :wiggle"}
                                   :legend false}
                                  {:data two-species
                                   :mapping {:x :petal-length :color :species}
                                   :charts [(chart/density {:position :mirror :fill :color :zero-line false
                                                            :padding {:x [0 0] :y [0 0]}})]
                                   :labels {:title ":zero-line false, no padding"}
                                   :legend false}]
                                 {:columns 2})
                   {:width 900 :height 560})

Shared options: Line style (:color :width :dash); Fill patterns (:pattern :pattern-color); Data and mapping (:data :mapping); Named theme styles (:theme-style); Ridgelines (:overlap).

18.3 ecdf

A step line per group: the share of values at or below each x.

Option Type Default Description
:x (required) a keyword, a function or a categorical marker of one values whose empirical distribution is drawn (data units)
:color a keyword, a function or a categorical marker of one or nil groups, each a step line in its own colour
:dash a keyword, a function or a categorical marker of one or nil groups, each a step line with its own dash style
:group a keyword, a function or a categorical marker of one or nil groups without a visual aesthetic, each its own step line
(plot/render-image {:data iris
                    :mapping {:x :petal-length :color :species}
                    :charts [(chart/ecdf)]
                    :labels {:x "petal length [cm]" :y "share at or below"}}
                   {:width 600 :height 350})

Its only own option is the padding - here none on y, so the steps start and end at the panel’s edges:

Option Type Default Description
:padding map of one of :x :y to [lo hi], non-negative numbers {:x [0.05 0.05] :y [0.05 0.05]} padding of the data span per position axis, {:x [lo hi] :y [lo hi]} fractions of the span
(plot/render-image {:data faithful
                    :mapping {:x :eruptions}
                    :charts [(chart/ecdf {:padding {:y [0 0]}})]
                    :labels {:title ":padding {:y [0 0]}"}}
                   {:width 500 :height 300})

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

18.4 qq

Quantiles of one side against the other’s: two data columns, or data against a theoretical distribution. Points on the reference line mean the same shape of distribution.

Option Type Default Description
:x a keyword, a function or a categorical marker of one or nil data of the x side (unless :x-distribution is given)
:y a keyword, a function or a categorical marker of one or nil data of the y side (unless :y-distribution is given)
:color a keyword, a function or a categorical marker of one or nil groups, each with its own point and line colour
:shape a keyword, a function or a categorical marker of one or nil groups, each with its own point shape
:size a keyword, a function or a categorical marker of one or nil groups, each with its own point size
:group a keyword, a function or a categorical marker of one or nil groups without a visual aesthetic
(def samples (let [g (r/rng :mersenne 11)
                   t3 (r/distribution :t {:degrees-of-freedom 3 :rng g})]
               (vec (concat (for [_ (range 200)] {:v (r/grandom g) :sample "normal"})
                            (for [_ (range 200)] {:v (r/sample t3) :sample "t(3)"})))))
v sample
-1.9354135624386053 normal
-0.9842798332756787 normal
0.7692425297241978 normal
0.47520762766422164 normal
-0.07460401433466404 normal
0.386576340050147 normal
0.5740037801583956 normal
0.6304795444116987 normal
0.8658518672112061 normal
-0.6587155372039402 normal
1.1977369488812335 normal
1.3496814267379298 normal
0.33415045817805766 normal
-0.09540563325605536 normal
0.4202488095487268 normal
1.2157194832517149 normal
-0.06003547257265243 normal
0.31875404416652725 normal
-1.2696556154795149 normal
0.22783203772564486 normal
0.7252478683121171 normal
-0.6244515436177719 normal
-0.5975761062902751 normal
-0.3623485346184586 normal
-0.31550127476485473 normal
1.9095980213212904 normal
0.41098996078625705 normal
-0.8061841406740187 normal
2.1071608232867507 normal
-0.7256677343646285 normal
1.6865979632494321 normal
-2.1551951631319257 normal
-0.8129352068886067 normal
-0.06082656934917857 normal
-1.3525320628523652 normal
0.22749695176256018 normal
2.00040094394776 normal
0.8023225700821351 normal
-0.7187867660832096 normal
-1.3666058217681498 normal
1.1569046470792448 normal
-0.35688823167648287 normal
0.8400673754079033 normal
0.3079360059649594 normal
-0.6085750438713416 normal
-0.54198387035825 normal
3.0378337780508096 normal
0.1224340917841133 normal
-0.438621351500724 normal
0.3338767636927164 normal
-2.65714989483138 normal
-2.1546086692916107 normal
-0.12521182140433756 normal
0.5655133775234155 normal
-1.2286343497814154 normal
-1.6875138589081766 normal
-1.3000745391077166 normal
0.47600748113197855 normal
-1.3952400596571857 normal
1.222893544881492 normal
-1.564668551993953 normal
0.38596082112326074 normal
0.3026726977215286 normal
2.6917350088128495 normal
0.47346375499232296 normal
0.9564010031630503 normal
-0.28400848879695406 normal
0.12087876478748225 normal
-0.39749999013144105 normal
-1.0528216283209995 normal
0.21158371484421143 normal
-0.9837375530978842 normal
1.6831486569359688 normal
-0.473300154181772 normal
1.1796989962144864 normal
1.1764595433604714 normal
0.4810565023449482 normal
0.23256596526418555 normal
-0.8687361114901867 normal
-0.6054534100157256 normal
0.3447467365944367 normal
0.5529152521718022 normal
1.5715512150048214 normal
-0.08980591289398993 normal
-0.0868956668739789 normal
0.8117764588472456 normal
-0.41100479532618145 normal
0.338499013268407 normal
0.5543343363973507 normal
0.2077980844271687 normal
-2.102473149613918 normal
-0.2928182390683161 normal
-1.2108365699643666 normal
0.37019209556872373 normal
-0.2040663562304196 normal
-1.7148007169486439 normal
1.0931052827177286 normal
-0.6679527482299739 normal
0.8004514534901619 normal
0.23659189951770057 normal
… …
(plot/render-image {:data samples
                    :mapping {:y :v :color :sample}
                    :charts [(chart/qq {:x-distribution [:normal]})]
                    :labels {:x "normal quantiles" :y "sample quantiles"}}
                   {:width 600 :height 400})

18.4.1 Style

The point style and the reference line’s width:

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)
:line-width a non-negative number 1 reference line width in pixels
(plot/render-image {:data (filter #(= "t(3)" (:sample %)) samples)
                    :mapping {:y :v}
                    :charts [(chart/qq {:x-distribution [:normal] :size 9 :shape \s :color [200 80 40 120]
                                        :stroke-color :black :stroke-width 0.5 :line-width 3})]
                    :labels {:title "squares, :line-width 3"}}
                   {:width 500 :height 350})

18.4.2 Distributions

Either side may be a distribution instead of data; with both sides distributions the chart draws :points quantile pairs labelled by :label. The reference line through the quartiles may be turned off, and the padding set (none on the right):

Option Type Default Description
:x-distribution a fastmath distribution, [:name] or [:name params] or vector, [keyword, optional map] or nil the :x side as a distribution instead of data: a fastmath distribution, [:name] or [:name params] (wins over the plot's mapping of :x)
:y-distribution a fastmath distribution, [:name] or [:name params] or vector, [keyword, optional map] or nil the :y side as a distribution instead of data: a fastmath distribution, [:name] or [:name params] (wins over the plot's mapping of :y)
:line one of false :quartiles :quartiles reference line, one per group: :quartiles (through the first and third quartile pairs, R qqline) or false (none)
:points integer (at least 2) 100; an error with a data side both sides distributions only: the number of points (Q-Q plotting positions, P-P values over :domain)
:label string or keyword or nil " vs "; an error with a data side both sides distributions only: the label of the chart's one row {:label l}, styled by the chart's own mapping of :label (e.g. {:color :label})
:padding map of one of :x :y to [lo hi], non-negative numbers {:x [0.05 0.05] :y [0.05 0.05]} padding of the data span per position axis, {:x [lo hi] :y [lo hi]} fractions of the span
(plot/render-image (plot/arrange [{:charts [(chart/qq {:x-distribution [:normal] :y-distribution [:t {:degrees-of-freedom 3}]
                                                       :label "t(3)" :mapping {:color :label}})
                                            (chart/qq {:x-distribution [:normal] :y-distribution [:t {:degrees-of-freedom 30}]
                                                       :label "t(30)" :mapping {:color :label} :line false :points 30})]
                                   :labels {:title "both sides distributions"}}
                                  {:data faithful
                                   :mapping {:y :eruptions}
                                   :charts [(chart/qq {:x-distribution [:uniform-real {:lower 0 :upper 1}] :line false
                                                       :padding {:x [0 0] :y [0 0]}})]
                                   :labels {:title "uniform, no line, no padding"}}]
                                 {:columns 2 :widths [1.25 1]})
                   {:width 1000 :height 380})

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

18.5 pp

Probabilities against probabilities: each value’s empirical (or theoretical) CDF on one side against the other side’s at the same value; points on y = x mean the same distribution.

Option Type Default Description
:x a keyword, a function or a categorical marker of one or nil data of the x side (unless :x-distribution is given)
:y a keyword, a function or a categorical marker of one or nil data of the y side (unless :y-distribution is given)
:color a keyword, a function or a categorical marker of one or nil groups, each with its own point and line colour
:shape a keyword, a function or a categorical marker of one or nil groups, each with its own point shape
:size a keyword, a function or a categorical marker of one or nil groups, each with its own point size
:group a keyword, a function or a categorical marker of one or nil groups without a visual aesthetic
(plot/render-image {:data samples
                    :mapping {:y :v :color :sample}
                    :charts [(chart/pp {:x-distribution [:normal] :size 4})]
                    :labels {:x "normal probabilities" :y "sample probabilities"}}
                   {:width 600 :height 400})

18.5.1 Style

The point style and the reference line’s width:

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)
:line-width a non-negative number 1 reference line width in pixels
(plot/render-image {:data (filter #(= "t(3)" (:sample %)) samples)
                    :mapping {:y :v}
                    :charts [(chart/pp {:x-distribution [:normal] :size 8 :shape \O :color [200 80 40 140]
                                        :stroke-color :black :stroke-width 1 :line-width 3})]
                    :labels {:title "outlined points, :line-width 3"}}
                   {:width 500 :height 350})

18.5.2 Distributions

Either side may be a distribution; with both sides distributions the chart evaluates both CDFs at :points values over :domain (required then); the right panel has no line and wide padding:

Option Type Default Description
:x-distribution a fastmath distribution, [:name] or [:name params] or vector, [keyword, optional map] or nil the :x side as a distribution instead of data: a fastmath distribution, [:name] or [:name params] (wins over the plot's mapping of :x)
:y-distribution a fastmath distribution, [:name] or [:name params] or vector, [keyword, optional map] or nil the :y side as a distribution instead of data: a fastmath distribution, [:name] or [:name params] (wins over the plot's mapping of :y)
:line one of false :identity :identity reference line, one per group: :identity (y = x) or false (none)
:points integer (at least 2) 100; an error with a data side both sides distributions only: the number of points (Q-Q plotting positions, P-P values over :domain)
:label string or keyword or nil " vs "; an error with a data side both sides distributions only: the label of the chart's one row {:label l}, styled by the chart's own mapping of :label (e.g. {:color :label})
:padding map of one of :x :y to [lo hi], non-negative numbers {:x [0.05 0.05] :y [0.05 0.05]} padding of the data span per position axis, {:x [lo hi] :y [lo hi]} fractions of the span
:domain [a finite number, a finite number] both sides distributions only (then required): [lo hi] of the values the CDFs are evaluated at
(plot/render-image (plot/arrange [{:charts [(chart/pp {:x-distribution [:normal] :y-distribution [:t {:degrees-of-freedom 2}]
                                                       :domain [-5 5] :points 40 :label "t(2)" :mapping {:color :label}})
                                            (chart/pp {:x-distribution [:normal] :y-distribution [:logistic]
                                                       :domain [-5 5] :points 40 :label "logistic" :mapping {:color :label}})]
                                   :labels {:title "both sides distributions, :domain [-5 5]" :y "probabilities"}}
                                  {:data faithful
                                   :mapping {:y :waiting}
                                   :charts [(chart/pp {:x-distribution [:normal {:mu 71 :sd 13.6}] :line false :size 3
                                                       :padding {:x [0.2 0.2] :y [0.2 0.2]}})]
                                   :labels {:title "normal(71, 13.6), no line, padding 0.2"}}]
                                 {:columns 2 :widths [1.25 1]})
                   {:width 1000 :height 380})

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

source: notebooks/reference/charts_distribution_stats.clj