20  Charts: part of a whole

Shares of a total as areas: nested rectangles (treemap), adjacent bands or rings (partition), nested circles (pack), a grid of cells (waffle) and overlapping sets (venn). 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-part-of-whole
  (:require [cljplot2.chart :as chart]
            [cljplot2.core :as plot]
            [cljplot2.describe :as describe]
            [notebook-utils :as nu]))
(def budget [{:dept "science" :team "physics" :cost 40} {:dept "science" :team "biology" :cost 25}
             {:dept "science" :team "chemistry" :cost 18} {:dept "arts" :team "music" :cost 15}
             {:dept "arts" :team "theatre" :cost 10} {:dept "arts" :team "painting" :cost 7}
             {:dept "sport" :team "football" :cost 20} {:dept "sport" :team "tennis" :cost 8}])
dept team cost
science physics 40
science biology 25
science chemistry 18
arts music 15
arts theatre 10
arts painting 7
sport football 20
sport tennis 8

20.1 treemap

Each leaf a rectangle of area proportional to its value, nested inside the rectangle of its group; the levels come from :hierarchy, outer level first.

Option Type Default Description
:value (required) a keyword, a function or a categorical marker of one node size: a positive finite number per row (other rows are dropped)
:hierarchy (required) vector of a keyword, a function or a categorical marker of one (at least 1) the levels of the tree, outer level first (a row's path ends at its first nil or "")
:color a keyword, a function or a categorical marker of one or nil leaf colour (rows with the same path but different colours stay separate leaves)
(plot/render-image {:data budget
                    :mapping {:hierarchy [:dept :team] :value :cost}
                    :charts [(chart/treemap)]}
                   {:width 600 :height 380})

Its options: the gap inside a group, the cell borders and the layout - near-square cells or slices turning direction at each depth:

Option Type Default Description
:padding a non-negative number 3 gap inside every group around its header and children, in pixels
:border-color a clojure2d color or :none :white cell border colour
:border-width a non-negative number 1 cell border width in pixels
:layout one of :squarify :slice-dice :squarify rectangle layout: :squarify (near-square cells) or :slice-dice (slices turning by depth)
(plot/render-image (plot/arrange [{:data budget
                                   :mapping {:hierarchy [:dept :team] :value :cost}
                                   :charts [(chart/treemap {:padding 8 :border-color :black :border-width 2})]
                                   :labels {:title ":padding 8, black borders"}}
                                  {:data budget
                                   :mapping {:hierarchy [:dept :team] :value :cost}
                                   :charts [(chart/treemap {:layout :slice-dice})]
                                   :labels {:title ":layout :slice-dice"}}]
                                 {:columns 2})
                   {:width 1000 :height 380})

Shared options: Fill patterns (:pattern :pattern-color); Data and mapping (:data :mapping); Named theme styles (:theme-style); Hierarchy labels (:group-labels :leaf-labels).

20.2 partition

The hierarchy as adjacent bands, one per level, each node as wide as its value - an icicle plot; in polar coordinates the bands become rings (a sunburst).

Option Type Default Description
:value (required) a keyword, a function or a categorical marker of one node size: a positive finite number per row (other rows are dropped)
:hierarchy (required) vector of a keyword, a function or a categorical marker of one (at least 1) the levels of the tree, outer level first (a row's path ends at its first nil or "")
:color a keyword, a function or a categorical marker of one or nil leaf colour (rows with the same path but different colours stay separate leaves)
(plot/render-image (plot/arrange [{:data budget
                                   :mapping {:hierarchy [:dept :team] :value :cost}
                                   :charts [(chart/partition)]
                                   :labels {:title "cartesian: icicle"}}
                                  {:data budget
                                   :mapping {:hierarchy [:dept :team] :value :cost}
                                   :charts [(chart/partition)]
                                   :coord {:kind :polar}
                                   :labels {:title "polar: sunburst"}}]
                                 {:columns 2})
                   {:width 1000 :height 420})

Its options: borders, the order of siblings, how nodes are coloured (by their top-level node, lighter by depth, or each by its name) and whether the root is drawn:

Option Type Default Description
:border-color a clojure2d color or :none :white cell border colour
:border-width a non-negative number 1 cell border width in pixels
:order one of :value :data :value order of siblings: :value (descending) or :data (first appearance)
:color-by one of :top :name :top node colours: :top - each top-level node's palette colour, lighter by depth; :name - every node by its name through the color scale
:root true or false or nil false draw the root too, every level one up
(plot/render-image (plot/arrange [{:data budget
                                   :mapping {:hierarchy [:dept :team] :value :cost}
                                   :charts [(chart/partition {:order :data :root true})]
                                   :coord {:kind :polar}
                                   :labels {:title ":order :data, :root true"}}
                                  {:data budget
                                   :mapping {:hierarchy [:dept :team] :value :cost}
                                   :charts [(chart/partition {:color-by :name :border-width 2})]
                                   :coord {:kind :polar}
                                   :labels {:title ":color-by :name"}
                                   :legend false}]
                                 {:columns 2})
                   {:width 1000 :height 460})

The borders in an icicle: the default thin white lines, and thick dark ones:

(plot/render-image (plot/arrange [{:data budget
                                   :mapping {:hierarchy [:dept :team] :value :cost}
                                   :charts [(chart/partition {:order :value})]
                                   :labels {:title ":order :value (default)"}}
                                  {:data budget
                                   :mapping {:hierarchy [:dept :team] :value :cost}
                                   :charts [(chart/partition {:border-color [40 40 40] :border-width 3})]
                                   :labels {:title ":border-color, :border-width 3"}}]
                                 {:columns 2})
                   {:width 1000 :height 360})

Shared options: Fill patterns (:pattern :pattern-color); Data and mapping (:data :mapping); Named theme styles (:theme-style); Hierarchy labels (:group-labels :leaf-labels).

20.3 pack

Each leaf a circle of area proportional to its value, packed inside the circle of its group.

Option Type Default Description
:value (required) a keyword, a function or a categorical marker of one node size: a positive finite number per row (other rows are dropped)
:hierarchy (required) vector of a keyword, a function or a categorical marker of one (at least 1) the levels of the tree, outer level first (a row's path ends at its first nil or "")
:color a keyword, a function or a categorical marker of one or nil leaf colour (rows with the same path but different colours stay separate leaves)
(plot/render-image {:data budget
                    :mapping {:hierarchy [:dept :team] :value :cost}
                    :charts [(chart/pack)]}
                   {:width 500 :height 450})

Its options: the gap between circles, borders, the packing order, the root circle and the depths left out of the drawing (the layout unchanged - here the group circles, depth 1):

Option Type Default Description
:padding a non-negative number 3 gap between siblings and between a child and its parent's edge, in pixels
:border-color a clojure2d color or :none :white circle border colour
:border-width a non-negative number 1 circle border width in pixels
:order one of :value :data :value packing order of siblings: :value (descending, largest near the centre) or :data
:root true or false or nil false draw the root circle too
:hidden-levels set of integer (at least 0) or nil #{} depths (root 0, top level 1) not drawn, the layout unchanged
(plot/render-image (plot/arrange [{:data budget
                                   :mapping {:hierarchy [:dept :team] :value :cost}
                                   :charts [(chart/pack {:padding 10 :root true :border-color :black})]
                                   :labels {:title ":padding 10, :root true"}}
                                  {:data budget
                                   :mapping {:hierarchy [:dept :team] :value :cost}
                                   :charts [(chart/pack {:hidden-levels #{1}})]
                                   :labels {:title ":hidden-levels #{1}"}}]
                                 {:columns 2})
                   {:width 1000 :height 480})

The packing order, here of the budget rows reversed (smallest first): siblings by value (largest first) or in data order; and wider borders:

(plot/render-image (plot/arrange [{:data (reverse budget)
                                   :mapping {:hierarchy [:dept :team] :value :cost}
                                   :charts [(chart/pack {:order :value})]
                                   :labels {:title ":order :value (default)"}}
                                  {:data (reverse budget)
                                   :mapping {:hierarchy [:dept :team] :value :cost}
                                   :charts [(chart/pack {:order :data})]
                                   :labels {:title ":order :data"}}
                                  {:data budget
                                   :mapping {:hierarchy [:dept :team] :value :cost}
                                   :charts [(chart/pack {:border-color [40 40 40] :border-width 3})]
                                   :labels {:title ":border-width 3"}}]
                                 {:columns 3})
                   {:width 1000 :height 360})

Shared options: Fill patterns (:pattern :pattern-color); Data and mapping (:data :mapping); Named theme styles (:theme-style); Hierarchy labels (:group-labels :leaf-labels).

20.4 waffle

A grid of cells (10 × 10 by default), each category taking a share of the cells proportional to its value.

Option Type Default Description
:color (required) a keyword, a function or a categorical marker of one the category: one colour and a share of the cells per category
:value a keyword, a function or a categorical marker of one or nil every row counts 1 weight per row, a non-negative finite number (other rows are dropped)
:pattern a keyword, a function or a categorical marker of one or nil fill pattern, splitting categories too
(def energy-mix [{:source "coal" :share 35} {:source "gas" :share 23} {:source "nuclear" :share 10}
                 {:source "hydro" :share 15} {:source "wind and solar" :share 17}])
source share
coal 35
gas 23
nuclear 10
hydro 15
wind and solar 17
(plot/render-image {:data energy-mix
                    :mapping {:color :source :value :share}
                    :charts [(chart/waffle)]}
                   {:width 500 :height 380})

Its options: the cell shape, gap and border; the grid size and fill order; or :unit - the value of one cell, so the cell count follows the values (here one cell per 2 units, 5 columns wide):

Option Type Default Description
:gap a finite number, >= 0, < 1 0.1 space between cells, a fraction of a cell
:shape one of :square :circle :square cell shape: :square or :circle
:border-color a clojure2d color or :none :none cell border colour
:border-width a non-negative number 1 cell border width in pixels
:rows integer (1..10000) 10 grid rows (ignored with :unit)
:columns integer (1..10000) 10 grid columns
:unit a positive finite number none - the categories share the :rows x :columns grid the value of one cell: a category gets value / unit cells, the grid :columns wide
:fill-order one of :rows :columns :rows fill from the bottom-left cell: :rows (row by row, upwards) or :columns (column by column)
(plot/render-image (plot/arrange [{:data energy-mix
                                   :mapping {:color :source :value :share}
                                   :charts [(chart/waffle {:shape :circle :gap 0.2 :rows 5 :columns 20})]
                                   :labels {:title ":circle, 5 × 20"}
                                   :legend false}
                                  {:data energy-mix
                                   :mapping {:color :source :value :share}
                                   :charts [(chart/waffle {:fill-order :columns :border-color :white :border-width 2
                                                           :gap 0})]
                                   :labels {:title ":fill-order :columns"}
                                   :legend false}
                                  {:data energy-mix
                                   :mapping {:color :source :value :share}
                                   :charts [(chart/waffle {:unit 2 :columns 5})]
                                   :labels {:title ":unit 2"}}]
                                 {:columns 3 :widths [1.3 1 1.1]})
                   {:width 1000 :height 380})

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

20.5 venn

Two or three sets as overlapping circles, each region labelled with the number of items it holds; a row names an item and a set it belongs to.

Option Type Default Description
:set (required) a keyword, a function or a categorical marker of one the set a row's item belongs to
:item (required) a keyword, a function or a categorical marker of one the item (repeated rows count once)
(def memberships (concat (for [i (range 12)] {:club "chess" :member (str "p" i)})
                         (for [i (range 8 14)] {:club "running" :member (str "p" i)})
                         (for [i (range 0 24 3)] {:club "choir" :member (str "p" i)})))
club member
chess p0
chess p1
chess p2
chess p3
chess p4
chess p5
chess p6
chess p7
chess p8
chess p9
chess p10
chess p11
running p8
running p9
running p10
running p11
running p12
running p13
choir p0
choir p3
choir p6
choir p9
choir p12
choir p15
choir p18
choir p21
(plot/render-image {:data memberships
                    :mapping {:set :club :item :member}
                    :charts [(chart/venn)]}
                   {:width 500 :height 420})

Its options: the fill and outline, which sets are shown, proportional areas (two sets), and the set and region labels - a function of {:sets :size :share} writes a region’s text:

Option Type Default Description
:alpha a number in [0 1] 0.4 fill opacity
:border-color :darker or a clojure2d color or :none :darker circle outline colour: a colour, :none or :darker (the darkened set colour)
:border-width a non-negative number 1 circle outline width in pixels
:sets vector of any value (at least 1) every set, in first-appearance order the sets shown (2 or 3), in order
:proportional true or false or nil true 2 sets: circle and overlap areas proportional to the counts (false - equal circles)
:set-labels true or false or nil true write the set names outside their circles
:region-labels true or false or a function or nil true region labels: true - the item count, false - none, or a function of {:sets :size :share} returning the text
(plot/render-image (plot/arrange [{:data memberships
                                   :mapping {:set :club :item :member}
                                   :charts [(chart/venn {:sets ["chess" "running"]})]
                                   :labels {:title "two sets, proportional"}}
                                  {:data memberships
                                   :mapping {:set :club :item :member}
                                   :charts [(chart/venn {:sets ["chess" "running"] :proportional false :alpha 0.6
                                                         :border-color :none})]
                                   :labels {:title ":proportional false"}}
                                  {:data memberships
                                   :mapping {:set :club :item :member}
                                   :charts [(chart/venn {:region-labels #(format "%.0f%%" (* 100.0 (double (:share %))))})]
                                   :labels {:title ":region-labels - shares"}}]
                                 {:columns 3})
                   {:width 1000 :height 380})

Without set names, and with thick outlines in the darkened set colours:

(plot/render-image (plot/arrange [{:data memberships
                                   :mapping {:set :club :item :member}
                                   :charts [(chart/venn {:set-labels false})]
                                   :labels {:title ":set-labels false"}}
                                  {:data memberships
                                   :mapping {:set :club :item :member}
                                   :charts [(chart/venn {:border-width 4})]
                                   :labels {:title ":border-width 4"}}]
                                 {:columns 2})
                   {:width 900 :height 380})

The gap between a circle and its set name is a theme key of [:chart :venn], not an option:

Option Type Default Description
:label-gap a non-negative number 4 pixels between a circle and its set name (theme only, not an option)
(plot/render-image {:data memberships
                    :mapping {:set :club :item :member}
                    :charts [(chart/venn)]
                    :theme {:chart {:venn {:label-gap 16}}}}
                   {:width 500 :height 420})

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

source: notebooks/reference/charts_part_of_whole.clj