10  Pose Rules

Each rule below states one behaviour and shows it on a rendered pose, with the printed structure where the shape is the point.

The rules are organized into seven sections (Construction, Layer Placement, Inferred Columns, Scope, Options, Assembly, Layout) and cover the call shapes of pj/pose, pj/lay-*, pj/arrange, pj/options, pj/scale, pj/coord, and pj/facet.

Read Poses first. On a first read through the book you can skim or skip this chapter, and return when you want the precise rule behind a behavior.

(ns plotje-book.pose-rules
  (:require
   ;; Kindly -- notebook rendering protocol
   [scicloj.kindly.v4.kind :as kind]
   ;; Tablecloth -- dataset operations
   [tablecloth.api :as tc]
   ;; Rdatasets -- standard datasets
   [scicloj.metamorph.ml.rdatasets :as rdatasets]
   ;; Plotje -- composable plotting
   [scicloj.plotje.api :as pj]))

Setup

(def iris (rdatasets/datasets-iris))

A helper to inspect pose structure without :data – the dataset is heavy and not what we are checking. We strip :data from the pose and every nested sub-pose and layer.

(defn strip-data [pose]
  (cond-> (dissoc pose :data)
    (:layers pose) (update :layers (partial mapv #(dissoc % :data)))
    (:poses pose) (update :poses (partial mapv strip-data))))
(defn pose-summary
  "Print pose structure without :data (for readability)."
  [pose]
  (kind/pprint (strip-data pose)))

The cells of a composite – its leaves, in reading order. pj/arrange puts its cells in rows, so they are not its :poses directly.

(defn cells [pose]
  (filterv #(not (contains? % :poses)) (rest (tree-seq :poses :poses pose))))

Overview

A pose is a plain Clojure map with a documented set of keys:

Key On Purpose
:data leaf or any ancestor dataset (Tablecloth)
:mapping pose or layer column-to-aesthetic mappings (on a layer, also the parameters of its layer type)
:layers pose per-scope layers
:poses composite only sub-poses
:layout composite, or a leaf faceted by one category on a composite, the direction of its rows and cells, weights, and :cols; on a faceted leaf, :cols (Rule L3)
:facet leaf a facet over columns, expanded into cells when the pose is drawn (Rule O3)
:opts root plot-level options (including composite-level keys like :share-scales)
:share-scales composite the axes the cells share; pj/arrange writes it under :opts, and pj/marginal writes it on the composite itself
:where any pose a map from a column to a value or a set of values; the pose draws only the rows that match

A leaf pose has :data, :mapping, :layers; no :poses. A composite pose has :poses, and a sub-pose may itself be a composite: pj/arrange wraps its cells in a row per line of the grid, so the composite it returns holds rows and each row holds cells. A composite the caller supplies is accepted the same way: pj/arrange given one among its inputs nests it (Rule C8), pj/facet called on one divides every cell of it (Rule O3), and a nested map written out and handed to pj/pose is read as written. So a grid inside a grid can be written directly. A layer is a map with :layer-type and an optional :mapping, plus sibling keys :stat, :position, :mark when the user provides them.


Construction

How poses and composites come into existence: every pj/pose call shape, pj/arrange, and the facet that crosses two lists of columns.

Rule C1: pj/pose on raw data creates a leaf

Called with a dataset as first argument, pj/pose returns a leaf pose. The arity determines what’s in :mapping: a keyword is :x; two keywords are :x and :y; a final map is the mapping and contributes aesthetic keys.

(-> iris
    (pj/pose :sepal-length :sepal-width))
sepal widthsepal length4.55.05.56.06.57.07.58.02.02.53.03.54.04.5
(-> iris
    (pj/pose :sepal-length :sepal-width)
    pose-summary)
{:mapping {:x :sepal-length, :y :sepal-width}, :layers []}

With only :color in the mapping, the pose is a leaf that names no :x and no :y. On data of one to three columns, the columns are read by default when the pose is drawn (Rule LI1). Iris has more columns, so a layer added to this pose has to name its columns, or a later pj/pose call has to name them.

(-> iris
    (pj/pose {:color :species})
    pose-summary)
{:mapping {:color :species}, :layers []}

Rule C2: pj/pose on a leaf merges into its mapping

pj/pose called on a leaf merges the new mapping into the leaf’s own mapping, whatever the leaf or its layers already name. The leaf remains a leaf, and no composite is created.

(-> iris
    pj/pose
    (pj/pose :sepal-length :sepal-width))
sepal widthsepal length4.55.05.56.06.57.07.58.02.02.53.03.54.04.5

A second call naming other aesthetics adds them beside the leaf’s own – here :x and :y beside :color. Where both calls name one aesthetic, the later call’s column is kept:

(-> iris
    (pj/pose {:color :species})
    (pj/pose :sepal-length :sepal-width))
sepal widthsepal lengthspeciessetosaversicolorvirginica4.55.05.56.06.57.07.58.02.02.53.03.54.04.5

Property P-C2 – construction commutativity. A mapping built over several pj/pose calls is equal to the same mapping written in one call.

(= (-> iris
       pj/pose
       (pj/pose {:color :species})
       (pj/pose :sepal-length :sepal-width))
   (pj/pose iris :sepal-length :sepal-width {:color :species}))
true

Rule C3: pj/pose naming other columns replaces the leaf’s :x and :y

pj/pose writes one mapping and does not add a panel. Called with an :x or :y other than the one the leaf’s mapping has, it replaces that column, and the layers that name no columns of their own read the new columns. So a pose returned by a function can be pointed at other columns:

(defn trend-plot [data]
  (-> data
      (pj/pose :sepal-length :sepal-width)
      pj/lay-point
      pj/lay-smooth))
(-> (trend-plot iris)
    (pj/pose :petal-length :petal-width))
petal widthpetal length12345670.00.51.01.52.02.5
(-> (trend-plot iris)
    (pj/pose :petal-length :petal-width)
    pose-summary)
{:mapping {:x :petal-length, :y :petal-width},
 :layers [{:layer-type :point} {:layer-type :smooth}]}

A layer that names its own columns keeps them. The pose’s new :x and :y are then drawn by no layer, and drawing the pose prints a warning naming them:

(-> iris
    (pj/lay-point :sepal-length :sepal-width)
    (pj/pose :petal-length :petal-width))
sepal widthsepal length4.55.05.56.06.57.07.58.02.02.53.03.54.04.5

For panels side by side, pj/arrange takes a cell for each panel, here a mapping naming the panel’s two columns:

(-> iris
    (pj/arrange [{:x :sepal-length :y :sepal-width} {:x :petal-length :y :petal-width}])
    pj/lay-point)
sepal widthsepal length56782.02.53.03.54.04.5petal widthpetal length2460.00.51.01.52.02.5

Property P-C3 – writing the same columns again changes nothing. A pj/pose call naming the leaf’s own :x and :y returns the same pose.

(= (trend-plot iris)
   (pj/pose (trend-plot iris) :sepal-length :sepal-width))
true

Rule C4: pj/pose with no :x or :y merges into the leaf’s mapping

A pj/pose call naming no :x and no :y, on a leaf whose mapping names them, merges the aesthetic into the leaf’s :mapping. The leaf stays a leaf.

(-> iris
    (pj/pose :sepal-length :sepal-width)
    (pj/pose {:color :species}))
sepal widthsepal lengthspeciessetosaversicolorvirginica4.55.05.56.06.57.07.58.02.02.53.03.54.04.5
(-> iris
    (pj/pose :sepal-length :sepal-width)
    (pj/pose {:color :species})
    pose-summary)
{:mapping {:x :sepal-length, :y :sepal-width, :color :species},
 :layers []}

Property P-C4 – the aesthetic may come in a second call. Adding the aesthetic in a second call equals writing it in the first.

(= (-> iris
       (pj/pose :sepal-length :sepal-width)
       (pj/pose {:color :species}))
   (pj/pose iris :sepal-length :sepal-width {:color :species}))
true

Rule C5: pj/pose leaves the layers where they are

pj/pose changes only the mapping. The layers stay on the pose they were added to, and a layer that names no columns of its own reads the merged mapping.

(-> iris
    (pj/pose :sepal-length :sepal-width)
    pj/lay-point
    (pj/pose {:color :species}))
sepal widthsepal lengthspeciessetosaversicolorvirginica4.55.05.56.06.57.07.58.02.02.53.03.54.04.5
(-> iris
    (pj/pose :sepal-length :sepal-width)
    pj/lay-point
    (pj/pose {:color :species})
    pose-summary)
{:mapping {:x :sepal-length, :y :sepal-width, :color :species},
 :layers [{:layer-type :point}]}

Rule C6: pj/pose on a composite merges into the root mapping

On a composite, pj/pose merges into the root :mapping, which reaches every cell that does not map the same aesthetic itself. So an aesthetic that no cell maps itself reaches every cell:

(-> iris
    (pj/arrange [{:x :sepal-length :y :sepal-width} {:x :petal-length :y :petal-width}])
    (pj/pose {:color :species})
    pj/lay-point)
sepal widthsepal length682.02.53.03.54.04.5petal widthpetal length2460.00.51.01.52.02.5speciessetosaversicolorvirginica
(-> iris
    (pj/arrange [{:x :sepal-length :y :sepal-width} {:x :petal-length :y :petal-width}])
    (pj/pose {:color :species})
    pj/lay-point
    pose-summary)
{:mapping {:color :species},
 :layout {:direction :vertical, :cols :auto},
 :poses
 [{:layout {:direction :horizontal},
   :poses
   [{:mapping {:x :sepal-length, :y :sepal-width}, :layers []}
    {:mapping {:x :petal-length, :y :petal-width}, :layers []}]}],
 :layers [{:layer-type :point}]}

An :x or a :y written on a composite whose cells each name their own :x and :y reaches none of the cells. The cells keep their columns, and drawing the pose prints a warning for each axis, saying that the column written on the composite is not drawn:

(-> iris
    (pj/arrange [{:x :sepal-length :y :sepal-width} {:x :petal-length :y :petal-width}])
    (pj/pose :sepal-length :petal-length)
    pj/lay-point)
sepal widthsepal length56782.02.53.03.54.04.5petal widthpetal length2460.00.51.01.52.02.5

Rule C7: pj/pose with nothing new has no effect

(pj/pose pose) and (pj/pose pose {}), where pose is a leaf or a composite, return pose unchanged, and writing a mapping twice gives the pose that writing it once gives. So pj/pose is safe to use as a pipeline step that does nothing.

A leaf pose. First the rendered plot, then its structure:

(def leaf-pose (-> iris (pj/pose :sepal-length :sepal-width)))
leaf-pose
sepal widthsepal length4.55.05.56.06.57.07.58.02.02.53.03.54.04.5
(pose-summary leaf-pose)
{:mapping {:x :sepal-length, :y :sepal-width}, :layers []}

Wrapping it with pj/pose returns the same value:

(= leaf-pose
   (pj/pose leaf-pose)
   (pj/pose leaf-pose {}))
true
(= (pj/pose leaf-pose {:color :species})
   (pj/pose (pj/pose leaf-pose {:color :species}) {:color :species}))
true

A composite pose. The rendered plot, then its structure:

(def composite-pose
  (-> iris
      (pj/arrange [{:x :sepal-length :y :sepal-width} {:x :petal-length :y :petal-width}])
      pj/lay-point))
composite-pose
sepal widthsepal length56782.02.53.03.54.04.5petal widthpetal length2460.00.51.01.52.02.5
(pose-summary composite-pose)
{:layout {:direction :vertical, :cols :auto},
 :poses
 [{:layout {:direction :horizontal},
   :poses
   [{:mapping {:x :sepal-length, :y :sepal-width}, :layers []}
    {:mapping {:x :petal-length, :y :petal-width}, :layers []}]}],
 :layers [{:layer-type :point}]}

And again, pj/pose has no effect:

(= composite-pose
   (pj/pose composite-pose)
   (pj/pose composite-pose {}))
true

Rule C8: pj/arrange composes poses into a composite

pj/arrange returns a composite. It takes a vector of cells, or data or a pose first and then the cells. Each cell is written the way the argument after the data is written in pj/pose: a pose, a composite, a column (a panel of that column), or a mapping such as {:x :a :y :b} (a panel of the columns the mapping names). So a cell and (pj/pose nil cell) draw the same. nil is the exception: in pj/arrange it is a cell left empty, where after the data in pj/pose it names no column. A vector is never a cell. A vector in the cell list is a list of cells: a row, where the cell list is a list of rows, and otherwise an arrangement of its own. The cells are placed in rows: the composite’s :poses are the rows, and each row’s :poses are the cells of that row.

(pj/arrange
 [(-> iris
      (pj/pose :sepal-length :sepal-width)
      pj/lay-point)
  (-> iris
      (pj/pose :petal-length :petal-width)
      pj/lay-point)])
sepal widthsepal length56782.02.53.03.54.04.5petal widthpetal length2460.00.51.01.52.02.5

A cell may itself be a composite, and nests rather than being flattened – which is the shape pj/arrange builds for itself, one row per line of the grid:

(-> (pj/arrange [(pj/arrange [(pj/pose iris :sepal-length :sepal-width)
                              (pj/pose iris :petal-length :petal-width)])
                 (pj/pose iris :sepal-length :petal-length)])
    pj/lay-point)
sepal widthsepal length682.02.53.03.54.04.5petal widthpetal length2460.00.51.01.52.02.5petal lengthsepal length56781234567

Given data first, a list of vectors of columns is a list of rows, with a panel for each column, as a list of vectors of poses is:

(-> iris
    (pj/arrange [[:sepal-length :sepal-width] [:petal-length :petal-width]])
    pj/lay-histogram)
countsepal length567801020countsepal width2.02.53.03.54.04.502040countpetal length24602040countpetal width0.00.51.01.52.02.502040

pj/arrange writes the options it is given into the composite’s :opts: :title, :width, :height, :share-scales, and any plot option pj/options takes. :cols decides how many cells each row holds, and is written on the composite’s :layout rather than its :opts: an arrangement made from a flat list keeps it there, as :auto where no :cols was given, so that cells pj/arrange adds later are laid out the same way as if they had been in the original list.

(pj/arrange
 [(pj/pose iris :sepal-length :sepal-width)
  (pj/pose iris :petal-length :sepal-width)]
 {:title "Arranged"
  :share-scales #{:y}})
sepal length56782.02.53.03.54.04.5petal length246sepal widthArranged

Rule C9: a facet crossing two lists of columns is a leaf pose

pj/facet given the map that crosses two lists of columns, {:col {:x cols} :row {:y cols}}, keeps that map on a leaf pose – no :poses – which the facet splits into a panel for every pair when the pose is drawn. A mapping written on the pose reaches every panel.

(-> iris
    (pj/pose {:color :species})
    (pj/facet {:col {:x [:sepal-length :sepal-width]}
               :row {:y [:sepal-length :sepal-width]}}))
5678682.02.53.03.54.04.5234sepal lengthsepal widthsepal lengthsepal widthspeciessetosaversicolorvirginica

Property P-C9 – the mapping can come first or last. A mapping written with pj/pose before the facet or after it gives an equal pose.

(let [a (-> iris
            (pj/pose {:color :species})
            (pj/facet {:col {:x [:sepal-length :sepal-width]}
                       :row {:y [:sepal-length :sepal-width]}}))
      b (-> iris
            (pj/facet {:col {:x [:sepal-length :sepal-width]}
                       :row {:y [:sepal-length :sepal-width]}})
            (pj/pose {:color :species}))]
  (= a b))
true

Layer Placement

Where lay-* calls place the layer in the pose tree. The rules cover whether the call names :x and :y, whether the receiver is a leaf or a composite, which cells of a composite draw the layer, the raw-data convenience case, the panel the layers of a leaf share, and the columns a layer reads from that panel.

Where the columns are written: the columns a lay-* call names are written on the layer’s own :mapping, and only there; the pose’s :mapping is what pj/pose writes. The two meet by scope merge when the pose is drawn: a layer that names a column overrides the pose’s, and a layer that names none reads the pose’s.

Rule LP1: bare lay-* attaches at the current pose’s root

A lay-* call naming no columns attaches the layer to the current pose’s top-level :layers. On a leaf, that is the leaf’s own :layers. On a composite, it is the root :layers, and the layer is drawn in every cell with as many columns as its layer type draws (Property P-LP1); where no cell has that many columns, it is drawn in every cell (Rule LP3) – a histogram added to two scatter cells draws a histogram of each cell’s :x column.

(-> iris
    (pj/pose :sepal-length :sepal-width)
    pj/lay-point)
sepal widthsepal length4.55.05.56.06.57.07.58.02.02.53.03.54.04.5

On a composite, the same call attaches at the root, and here both cells draw it:

(-> (pj/arrange
     [(pj/pose iris :sepal-length :sepal-width)
      (pj/pose iris :petal-length :petal-width)])
    pj/lay-point)
sepal widthsepal length56782.02.53.03.54.04.5petal widthpetal length2460.00.51.01.52.02.5
(-> (pj/arrange
     [(pj/pose iris :sepal-length :sepal-width)
      (pj/pose iris :petal-length :petal-width)])
    pj/lay-point
    pose-summary)
{:layout {:direction :vertical, :cols :auto},
 :poses
 [{:layout {:direction :horizontal},
   :poses
   [{:mapping {:x :sepal-length, :y :sepal-width}, :layers []}
    {:mapping {:x :petal-length, :y :petal-width}, :layers []}]}],
 :layers [{:layer-type :point}]}

Property P-LP1 – bare layers flow downward. After adding one bare layer to a composite, the composite’s root :layers holds that single entry. A cell that draws the root’s layer draws it first, underneath the cell’s own layers, and has no layer inferred for it.

(let [before (pj/arrange
              [(pj/pose iris :sepal-length :sepal-width)
               (pj/pose iris :petal-length :petal-width)])
      after  (-> (pj/arrange
                  [(pj/pose iris :sepal-length :sepal-width)
                   (pj/pose iris :petal-length :petal-width)])
                 pj/lay-point)]
  [(count (or (:layers before) []))
   (count (or (:layers after)  []))])
[0 1]

Rule LP2: on a composite, a lay-* naming columns is drawn in every cell that fits it

When a lay-* call names columns on a composite, the layer is added to the composite’s root :layers, as a bare layer is (Rule LP1), and its own :mapping carries the columns the call named. It is drawn in every cell that fits it; where no cell fits it, Rule LP3 applies.

Each cell has at most one column on :x and one on :y. The cell’s column on an axis is the column the cell’s mapping names there, after the mappings above the cell are merged in. Where that mapping names none, it is the column of the cell’s first layer that names one on that axis. Other layers of the cell may draw other columns on the same panel, but they do not count here.

A cell fits a layer when both of these hold:

  • On each axis where the layer names a column, the cell’s column is the same one, or the cell has none.

  • Counting the cell’s columns and the layer’s together, one per axis, gives a number the layer type draws: one for a histogram, density or rug, two for a scatter, line or smooth, and either for a bar.

So a trend line naming {:y :petal-width} fits every cell whose column on :y is :petal-width. A histogram naming :sepal-length does not fit a scatter cell of :sepal-length and :sepal-width, because that cell has two columns and a histogram draws one. Beside a cell of :sepal-length alone, the histogram is drawn in that cell only. Columns are matched by strict equality: a column reference is the keyword or string the user typed, and the two forms are not interchangeable.

Here the trend line names the first cell’s columns, so it is drawn in the first cell only. The second cell draws the points added at the root:

(-> iris
    (pj/arrange [{:x :sepal-length :y :sepal-width} {:x :petal-length :y :petal-width}])
    pj/lay-point
    (pj/lay-smooth :sepal-length :sepal-width))
sepal widthsepal length56782.02.53.03.54.04.5petal widthpetal length2460.00.51.01.52.02.5

Cells that split the data with :where all draw the pose’s columns, so a layer naming those columns is drawn in every cell, as it is drawn in every panel of the facet the cells split:

(-> iris
    (pj/pose :sepal-length :sepal-width)
    (pj/arrange [{:where {:species "setosa"}}
                 {:where {:species "versicolor"}}
                 {:where {:species "virginica"}}])
    pj/lay-point
    (pj/lay-smooth :sepal-length :sepal-width))
sepal widthsepal length682.02.53.03.54.04.56868setosaversicolorvirginica

A facet over columns places a layer naming columns the same way: the layer is drawn in the panels that fit it, and nothing is printed for the panels it is not drawn in, as on a composite. Here a trend line naming :sepal-length on :y is drawn in the panel of that column only:

(-> iris
    (pj/pose {:x :petal-length})
    (pj/facet {:y [:sepal-length :sepal-width]})
    (pj/lay-smooth {:y :sepal-length}))
sepal length567sepal width12345672.02.53.03.54.04.5petal length

A cell built by lay-* names its columns on its layer and not in its own mapping. The panel’s columns are the same either way, so the layer is drawn in the cell whose layers draw its columns:

(-> (pj/arrange [(pj/lay-point iris :sepal-length :sepal-width)
                 (pj/lay-point iris :petal-length :petal-width)])
    (pj/lay-smooth :petal-length :petal-width))
sepal widthsepal length56782.02.53.03.54.04.5petal widthpetal length2460.00.51.01.52.02.5

A lay-* call naming one column follows the same rule: it is drawn in every cell whose column on that axis is the same one or none, and reads the cell’s column on the other axis – as a layer naming one column on a leaf does (Rule LP6). Here a trend line naming only :sepal-width on :y is drawn in the first cell, against that cell’s :sepal-length:

(-> iris
    (pj/arrange [{:x :sepal-length :y :sepal-width}
                 {:x :sepal-length :y :petal-width}])
    pj/lay-point
    (pj/lay-smooth {:y :sepal-width}))
sepal width56782.02.53.03.54.04.5petal width56780.00.51.01.52.02.5sepal length

Rule LP3: on a composite, a lay-* that fits no cell is drawn in every cell

When no cell fits a layer (Rule LP2 for a layer naming columns, Property P-LP1 for a bare one), the layer is drawn in every cell, as written, on that cell’s panel – the layers of a cell share its panel, as the layers of a leaf do (Rule LP5). A lay-* call adds no cell. Here the second layer names :sepal-length and :petal-length, which no cell draws together, so both cells draw it beside the points added at the root, and each axis is titled by every column drawn on it. A colour is written on each layer, as in the examples of Rule LP5, so no legend is added:

(-> iris
    (pj/arrange [{:x :sepal-length :y :sepal-width} {:x :petal-length :y :petal-width}])
    (pj/lay-point {:color "#377eb8"})
    (pj/lay-point :sepal-length :petal-length {:color "#999999"}))
sepal width, petal lengthsepal length246801234567petal width, petal lengthpetal length, sepal length246801234567

A histogram naming :sepal-length fits no scatter cell – two columns are more than a histogram draws from – so it is drawn in both cells, over the points. Each cell’s :y axis then reads a count and a column, and drawing the plot prints a Note for each cell saying so:

(-> iris
    (pj/arrange [{:x :sepal-length :y :sepal-width} {:x :petal-length :y :petal-width}])
    pj/lay-point
    (pj/lay-histogram :sepal-length))
sepal widthsepal length24680510152025petal widthpetal length, sepal lengthpetal lengthsepal length24680510152025

Where a cell would then draw only that layer – the cell holds no layer of its own, and no other layer at the root fits it – and on :x or :y every cell draws another column than the layer names, the cell’s own columns would be drawn by no layer. That reports an error naming the columns, as it does on a facet over columns. A cell’s own columns are those its mapping or its own layers name:

(try
  (pj/plan (-> iris
               (pj/arrange [{:x :sepal-length :y :sepal-width} {:x :petal-length :y :petal-width}])
               (pj/lay-point :sepal-length :petal-length)))
  (catch clojure.lang.ExceptionInfo e
    (ex-message e)))
"pj/lay-point names :petal-length on :y, and no panel draws it: the panels draw :sepal-width and :petal-width on :y. Add :petal-length to the :y columns the panels are made from -- the facet's, or the cells given to pj/arrange -- and leave :y off the layer, or draw the layer on a pose of its own and put the two side by side with pj/arrange. A layer naming no column is drawn in every panel."

Each cell reads its own data, so a layer added to cells that carry their own data, under a root that has none, is drawn from each cell’s data:

(-> (pj/arrange [(pj/lay-point iris :sepal-length :sepal-width)
                 (pj/lay-point iris :petal-length :petal-width)])
    pj/lay-smooth)
sepal widthsepal length56782.02.53.03.54.04.5petal widthpetal length2460.00.51.01.52.02.5

A cell is added by pj/arrange: given an arrangement, it adds the cells it is given after the arrangement’s own, here as the third cell of the row. The root’s points are drawn in the new cell too, under its trend line:

(-> iris
    (pj/arrange [{:x :sepal-length :y :sepal-width} {:x :petal-length :y :petal-width}])
    pj/lay-point
    (pj/arrange [(pj/lay-smooth iris :sepal-length :petal-length)]))
sepal widthsepal length682.02.53.03.54.04.5petal widthpetal length2460.00.51.01.52.02.5petal lengthsepal length681234567

Property: a split and an arrangement draw the same panels. Two layers on different columns, split over those columns with pj/facet, and the same layers attached to a composite built with pj/arrange (LP2 draws each in its matching cell) draw the same panels with the same marks. The pose values differ: the facet keeps one leaf and expands it into cells when it is drawn, while pj/arrange builds a composite.

(let [via-facet (-> iris
                    (pj/lay-point :sepal-length :sepal-width)
                    (pj/lay-point :petal-length :petal-width)
                    (pj/facet {:x [:sepal-length :petal-length]
                               :y [:sepal-width :petal-width]}))
      via-arrange (-> iris
                      (pj/arrange [{:x :sepal-length :y :sepal-width} {:x :petal-length :y :petal-width}])
                      (pj/lay-point :sepal-length :sepal-width)
                      (pj/lay-point :petal-length :petal-width))
      drawn (fn [pose] (select-keys (pj/svg-summary pose) [:panels :points]))]
  {:via-facet-drawn (drawn via-facet)
   :via-arrange-drawn (drawn via-arrange)
   :via-facet-shape (if (:poses via-facet) :composite :leaf)
   :via-arrange-shape (if (:poses via-arrange) :composite :leaf)})
{:via-facet-drawn {:panels 2, :points 300},
 :via-arrange-drawn {:panels 2, :points 300},
 :via-facet-shape :leaf,
 :via-arrange-shape :composite}

Rule LP4: lay-* on raw data coerces the data into a leaf pose

lay-* called with a dataset as its first argument coerces the data into a leaf pose first, then applies the layer. The result draws what calling pj/pose on the data with the same two columns, and then pj/lay-point, draws. This keeps the convenience one-liner (-> data (pj/lay-point :x :y)) working as the shortest path from data to plot.

(def tiny
  {:a [1 2 3 4 5]
   :b [2 4 3 5 4]})
(-> tiny
    (pj/lay-point :a :b))
ba123452345

The explicit two-step form draws the same plot. The two poses differ in where the columns are written: the lay-* call above keeps them on its layer, and here pj/pose writes them on the pose, where every layer added afterwards reads them:

(-> tiny
    (pj/pose :a :b)
    pj/lay-point
    pose-summary)
{:mapping {:x :a, :y :b}, :layers [{:layer-type :point}]}

nil in place of the data, followed by columns or a mapping, is a template, as it is for pj/pose: a leaf pose holding the layer and no data, completed by pj/with-data or by an arrangement given data first. nil alone reports an error.

(pj/lay-point nil :a :b)
bano data0101

Rule LP5: the layers of a leaf share its panel

A leaf draws one panel, and every layer on it draws there, whatever columns it names. Its :x and :y stay on the layer’s own :mapping, and the leaf’s :mapping is left as it was.

Each axis is titled by every column drawn on it. A layer with no :color specified, on the pose or on the layer, also takes a color and a legend entry naming its columns by default, unless a layer of the panel is colored by a column. Each layer below specifies its color, so no color and no legend entry is added.

(-> iris
    (pj/pose :sepal-length :sepal-width)
    (pj/lay-point :sepal-length :sepal-width {:color "#377eb8"})
    (pj/lay-point :petal-length :petal-width {:color "#e6550d"}))
sepal width, petal widthsepal length, petal length123456780.00.51.01.52.02.53.03.54.04.5

One panel with the blue sepal points and the orange petal points on it, each axis titled by both of its columns, and their domains stretched to hold the petal values too. Printed, the leaf’s :mapping is untouched and the petal columns sit on the second layer:

(-> iris
    (pj/pose :sepal-length :sepal-width)
    (pj/lay-point :sepal-length :sepal-width {:color "#377eb8"})
    (pj/lay-point :petal-length :petal-width {:color "#e6550d"})
    pose-summary)
{:mapping {:x :sepal-length, :y :sepal-width},
 :layers
 [{:layer-type :point,
   :mapping {:x :sepal-length, :y :sepal-width, :color "#377eb8"}}
  {:layer-type :point,
   :mapping {:x :petal-length, :y :petal-width, :color "#e6550d"}}]}

On a composite, each layer is drawn in the cells that fit it, and the third layer, which fits no cell, is drawn in both (Rule LP3):

(-> iris
    (pj/arrange [{:x :sepal-length :y :sepal-width} {:x :petal-length :y :petal-width}])
    (pj/lay-point :sepal-length :sepal-width {:color "#377eb8"})
    (pj/lay-point :petal-length :petal-width {:color "#e6550d"})
    (pj/lay-point :sepal-width :petal-width {:color "#4daf4a"}))
sepal width, petal widthsepal length, sepal width24680.00.51.01.52.02.53.03.54.04.5petal widthpetal length, sepal width24680.00.51.01.52.02.53.03.54.04.5

A leaf that already carries columns, called with a lay-* that names different columns, keeps the layer with the columns the call named written on it. The pose stays a leaf, and the layer is drawn on the leaf’s one panel. The pose’s own columns are drawn only by layers that name none, so here only the petal columns are drawn, and drawing the pose prints a warning that the pose’s :x and :y are drawn by no layer:

(-> iris
    (pj/pose :sepal-length :sepal-width)
    (pj/lay-point :petal-length :petal-width))
petal widthpetal length12345670.00.51.01.52.02.5

Where the columns are written does not change the rule. A layer names its columns either in the call’s argument slots or in its mapping, and placement reads both alike: (pj/lay-line pose :sepal-length :petal-width) and (pj/lay-line pose {:x :sepal-length :y :petal-width}) add the same layer. A written value is not a column – it places a mark at that value. Where a number could also name a column, the data decides between the two readings, as Columns named by number shows.

Two lines over one :x, each naming its own :y in its mapping, therefore draw on one panel, the y axis covering both columns:

(-> {:time [1 2 3 4 5] :linear [1 2 3 4 5] :squared [1 4 9 16 25]}
    (pj/pose :time)
    (pj/lay-line {:y :linear})
    (pj/lay-line {:y :squared}))
linear, squaredtimelinearsquared123450510152025

A written value in the same slot places a label. Here :x and :y are numbers, and the dataset has no column named 2 or 4, so the label is drawn at 2 on :x and 4 on :y:

(-> {:time [1 2 3 4 5] :linear [1 2 3 4 5] :squared [1 4 9 16 25]}
    (pj/lay-point :time :linear)
    (pj/lay-text {:x 2 :y 4 :text "a note"}))
lineartimea note1234512345

Separate panels for the layers of one leaf come from a split, (pj/facet pose {:x [...] :y [...]}), or from pj/arrange. Asking for them with {:overlay false} or (pj/overlay pose false) reports an error naming both:

(try
  (-> iris
      (pj/lay-point :sepal-length :sepal-width)
      (pj/lay-point :petal-length :petal-width {:overlay false}))
  (catch clojure.lang.ExceptionInfo e
    (ex-message e)))
"pj/lay-point -- :overlay is retired. The layers of a pose share one panel. To draw layers on separate panels, split the pose over the columns, as in (pj/facet pose {:y [:a :b]}), or give each layer a pose of its own and arrange the poses with pj/arrange."

Rule LP6: a layer reads the panel’s column where it needs one

The panel’s column on an axis is the column the pose names there, and where the pose names none, the column of the first layer that names one there. A layer reads the panel’s column on an axis where it names none and its layer type needs one: both columns for a layer naming none, and the other column for a layer naming one whose layer type draws from two.

(-> iris
    (pj/lay-point :sepal-length :sepal-width)
    (pj/lay-line {:y :petal-width})
    pj/lay-smooth)
sepal width, petal widthsepal lengthsepal widthpetal width4.55.05.56.06.57.07.58.00.00.51.01.52.02.53.03.54.04.5

The columns stay on the layers. The pose’s own mapping is not written, so a layer type that can draw from one column reads no second column from a neighbouring layer:

(-> iris
    (pj/lay-point :sepal-length :sepal-width)
    (pj/lay-rug {:x :petal-length}))
sepal widthsepal length, petal lengthsepal lengthpetal length123456782.02.53.03.54.04.5

Property: column-existence safety check. When a lay-* call names a column on an axis where the pose already draws another column, it first checks that those columns exist in the data the layer would draw from – the layer’s own :data if present, otherwise the inherited data. A missing column is almost always a typing mistake, so the call reports an error at once, naming the columns the data has. A first lay-* call on raw data is not checked at the call; a missing column there reports an error when the pose is drawn.

The check at the call covers the columns written in the argument slots. A name written in the layer’s mapping may be a column or a value, which the data decides when the pose is drawn, so a name there that matches no column reports an error when the pose is drawn.

(try
  (-> iris
      (pj/pose :sepal-length :sepal-width)
      (pj/lay-point :nope :nada))
  (catch clojure.lang.ExceptionInfo e
    (ex-message e)))
"pj/lay-point :x :nope names a column that doesn't exist in the data the layer would draw from. Available columns: [:petal-length :petal-width :rownames :sepal-length :sepal-width :species]. If you meant another dataset, pass `:data` on this lay-* call."

Supplying :data on the lay-* call satisfies the safety check: the layer draws its own data, on the leaf’s panel.

(-> iris
    (pj/pose :sepal-length :sepal-width)
    (pj/lay-point :foo :bar
                  {:data {:foo [1 2 3] :bar [4 5 6]}}))
barfoo123456

Inferred Columns

Which columns a pose draws when no column names are written. One rule. (How column references are compared is covered by Rule LP2.)

Rule LI1: few-column datasets infer columns from column order

When lay-* or pj/pose is called on a dataset without explicit column arguments, the mapping is read off the order of the dataset’s columns when the pose is drawn:

Columns Inferred mapping
1 {:x col0}
2 {:x col0 :y col1}
3 {:x col0 :y col1 :color col2}
4+ none (name the columns)

The example is a dataset rather than a map literal, because the rule reads an order and a Clojure map does not carry one.

(-> (tc/dataset {:height [1 2 3] :weight [4 5 6] :species ["a" "b" "a"]})
    pj/lay-point)
weightheightspeciesab123456

With four or more columns no column is read by default. The pj/lay-point call returns a pose, and a later step may name the columns:

(-> {:a [1 2] :b [3 4] :c [5 6] :d [7 8]}
    pj/lay-point
    (pj/pose {:x :a :y :b}))
ba1234

Drawn with no column named, the pose reports an error naming the layer and the columns of the data:

(try
  (-> {:a [1 2] :b [3 4] :c [5 6] :d [7 8]}
      pj/lay-point
      pj/plan)
  (catch Exception e
    (ex-message e)))
"pj/lay-point draws from two columns, one on :x and one on :y, and this layer has no column on :x or :y. It names no column. Name both on the layer -- (pj/lay-point pose :x-column :y-column) -- or in the pose's mapping, which every layer of the pose reads. The data has 4 columns, so none is read by default. Available columns: [:a :b :c :d]"

Scope

How mappings, data and row filters flow through the pose tree: from the root, through each composite, to the leaf and its layers, at any depth.

Where two values for one key meet – a later pj/pose on the same pose, a layer under its pose, a cell under its composite – they combine the same way at every level. A key holds either one value, which the later or inner value replaces, or a map of independent settings, which combines key by key: a scale spec (Rule S6), a row filter (Rule S5), a :theme (Rule O5).

Rule S1: mapping scope is a tree-walk merge; narrower wins

The effective :mapping for a rendered layer is computed by merging, in order: root’s :mapping, then each ancestor composite’s :mapping, then the leaf’s own :mapping, then the layer’s own :mapping. Inner keys override outer. Any depth of composite nesting works the same way.

Root-level aesthetic flows to every leaf. Using a two-panel composite with :color declared at root:

(def s1-composite
  (pj/pose
   {:mapping {:color :species}
    :poses [{:mapping {:x :sepal-length :y :sepal-width}
             :layers [{:layer-type :point}]}
            {:mapping {:x :petal-length :y :petal-width}
             :layers [{:layer-type :point}]}]
    :data iris}))
s1-composite
sepal widthsepal length682.02.53.03.54.04.5petal widthpetal length2460.00.51.01.52.02.5speciessetosaversicolorvirginica

Property P-S1 – sibling independence. A sub-pose’s own mapping does not leak into its siblings.

(def s1-siblings
  (pj/pose
   {:poses [{:mapping {:x :sepal-length :y :sepal-width}
             :layers [{:layer-type :point}]}
            {:mapping {:x :petal-length :y :petal-width :color :species}
             :layers [{:layer-type :point}]}]
    :data iris}))
s1-siblings
sepal widthsepal length56782.02.53.03.54.04.5petal widthpetal lengthspeciessetosaversicolorvirginica2460.00.51.01.52.02.5

Rule S2: data scope – nearest-ancestor-non-nil wins

The effective :data for a rendered layer is the nearest non-nil :data walking from the layer up through each ancestor to the root. Layer :data > leaf :data > nearest ancestor composite :data > root :data. Unlike mappings, data does not merge – it is picked, wholesale.

(def s2-tree
  (pj/pose
   {:poses [{:mapping {:x :sepal-length :y :sepal-width}
             :layers [{:layer-type :point}]}
            {:mapping {:x :a :y :b}
             :layers [{:layer-type :point}]
             :data (tc/dataset {:a [1 2 3] :b [3 5 4]})}]
    :data iris}))
s2-tree
sepal widthsepal length56782.02.53.03.54.04.5ba123345

Rule S3: nil in a mapping cancels an inherited value

Assigning nil to a mapping key at an inner scope cancels the value inherited from outer scopes. The rendering path treats a nil mapping value as equivalent to “no mapping for that aesthetic.”

(-> iris
    (pj/pose :sepal-length :sepal-width {:color :species})
    pj/lay-point
    (pj/lay-smooth {:color nil :stat :linear-model}))
sepal widthsepal lengthspeciessetosaversicolorvirginica4.55.05.56.06.57.07.58.02.02.53.03.54.04.5

Rule S4: layer :mapping is the narrowest scope

A mapping written in a layer’s own :mapping (aesthetic mappings passed to lay-*) scopes to that layer only. Other layers – even on the same leaf – do not read that mapping. This is the terminal case of S1: the layer’s mapping is innermost in the merge.

(-> iris
    (pj/pose :sepal-length :sepal-width)
    (pj/lay-point {:color :species})
    (pj/lay-smooth {:stat :linear-model}))
sepal widthsepal lengthspeciessetosaversicolorvirginica4.55.05.56.06.57.07.58.02.02.53.03.54.04.5

A series written on one layer reaches the other layers of the pose. A vector of columns written on one layer reshapes the pose’s dataset into one row per column and value, and the other layers of the pose read the reshaped rows. Here a point layer naming no columns follows a line layer drawing two columns as a series, and draws a point for each row of the reshaped data – two for each flower:

(-> iris
    (pj/lay-line :sepal-length [:sepal-width :petal-width])
    pj/lay-point)
sepal width, petal widthsepal lengthsepal widthpetal width4.55.05.56.06.57.07.58.00.00.51.01.52.02.53.03.54.04.5

Series shows what the other layers can read.

Rule S5: a row filter combines column by column at every level

A :where is a map from a column to the values kept, and each column is a separate condition. Where two filters meet, a column named in both takes the later or inner value, and columns named in one of them are kept as they are. A composite’s filter reaches every cell below it, a cell carrying data of its own included, as the composite’s mapping does.

Under a composite that keeps setosa, a cell naming versicolor draws versicolor; a cell naming a petal width of 0.2 keeps setosa too. The points are colored by species, so each cell shows which species it kept:

(-> iris
    (pj/pose {:x :sepal-length :y :sepal-width :where {:species "setosa"}})
    (pj/arrange [{:where {:species "versicolor"}}
                 {:where {:petal-width 0.2}}])
    (pj/lay-point {:color :species}))
5672.02.22.42.62.83.03.23.43.63.84.04.2567sepal lengthsepal widthversicolor0.2speciessetosaversicolor

The same two filters written with pj/pose, one after the other, give the same rows as the cell does:

(-> iris
    (pj/pose {:x :sepal-length :y :sepal-width :where {:species "setosa"}})
    (pj/pose {:where {:species "versicolor"}})
    pj/lay-point)
sepal widthsepal length4.85.05.25.45.65.86.06.26.46.66.87.02.02.22.42.62.83.03.23.4

Rule S6: a scale combines key by key, whichever function writes the mapping

A mapping value names one column and may carry a scale. Where two meet, the column is replaced and the scale settings combine key by key, as Scales describes. This holds for a layer under its pose, a cell under its composite, pj/scale, and pj/pose writing into a pose that already has a mapping – so a scale set before the columns are named reaches them:

(-> iris
    (pj/pose {})
    (pj/scale :x :log)
    (pj/pose :sepal-length :sepal-width)
    pj/lay-point)
sepal widthsepal length4.55.05.56.06.57.07.58.02.02.53.03.54.04.5

Options

Plot options describe a plot rather than a layer. They are written in a pose’s :opts and reach every cell below it, where a cell’s own value replaces the inherited one – key by key for :theme and :config (Rule O5).

Rule O1: pj/options writes to the root’s :opts

pj/options merges its argument into the current pose’s :opts. On a leaf, that is the leaf’s :opts. On a composite, the root’s. The root’s options reach every cell below it, and a cell’s own options win key by key (Rule O5); they configure panels and the plot, not layers.

(-> iris
    (pj/pose :sepal-length :sepal-width)
    pj/lay-point
    (pj/options {:title "Iris"}))
Irissepal widthsepal length4.55.05.56.06.57.07.58.02.02.53.03.54.04.5

Repeated calls merge, later-wins on collisions:

(-> iris
    (pj/pose :sepal-length :sepal-width)
    pj/lay-point
    (pj/options {:title "One"})
    (pj/options {:title "Two" :subtitle "Sub"}))
TwoSubsepal widthsepal length4.55.05.56.06.57.07.58.02.02.53.03.54.04.5

Rule O2: pj/scale writes the mapping, pj/coord writes options

A scale belongs to the aesthetic it reads, so pj/scale writes it into the pose’s :mapping, under that aesthetic’s :scale. It is the same key a mapping written out in full uses, so the two are one setting written two ways. pj/coord writes :coord into :opts, which has no aesthetic to belong to.

Axis aesthetics (:x, :y) accept :linear, :log, :categorical; the visual ones (:size, :alpha, :fill, :color) accept :linear and :log; :shape accepts :categorical. Both pj/scale and pj/coord flow down to every leaf beneath the pose they are called on.

(-> iris
    (pj/pose :sepal-length :sepal-width)
    pj/lay-point
    (pj/scale :x :log)
    (pj/coord :flip))
sepal lengthsepal width2.02.22.42.62.83.03.23.43.63.84.04.24.44.55.05.56.06.57.07.58.0

Where the aesthetic is already mapped, the scale joins that mapping. A mapping written plainly has no room for a scale, so it is rewritten in the full form under :from – the plain reading spelled out, which leaves what the mapping names unchanged:

(-> iris
    (pj/pose :sepal-length :sepal-width {:size :petal-length})
    pj/lay-point
    (pj/scale :size :log))
sepal widthsepal lengthpetal length12354.55.05.56.06.57.07.58.02.02.53.03.54.04.5

Where it is not mapped at that pose, the scale is written on its own and applies to whatever names the aesthetic below:

(-> iris
    (pj/pose :sepal-length :sepal-width)
    (pj/lay-point {:size :petal-length})
    (pj/scale :size :log))
sepal widthsepal lengthpetal length12354.55.05.56.06.57.07.58.02.02.53.03.54.04.5

Rule O3: pj/facet writes a faceting column to the mapping, and a facet over columns to :facet

pj/facet writes the faceting column into the pose’s :mapping under :col, and a map {:col ... :row ...} writes both for a grid. These are aesthetics, so they follow the scope rules every other mapping follows: written on a pose they reach every layer and every sub-pose below, and a sub-pose that writes its own overrides them. The layout effect – splitting each leaf’s panel into a group of panels – happens at render time.

(-> iris
    (pj/pose :sepal-length :sepal-width)
    pj/lay-point
    (pj/facet :species))
sepal widthsepal length682.02.53.03.54.04.56868setosaversicolorvirginica

A 2D grid uses both keys, each naming a different column – here whether the party had a smoker across, and the sex of the person who paid down:

(-> (rdatasets/reshape2-tips)
    (pj/pose :total-bill :tip)
    pj/lay-point
    (pj/facet {:col :smoker :row :sex}))
tiptotal bill51020405102040NoYesFemaleMale

A grid crossing categories with columns keeps the two apart: the categories in the mapping, the columns under :facet. The two are crossed when the pose is drawn.

(-> iris
    (pj/pose {:x :sepal-length})
    pj/lay-point
    (pj/facet {:col :species :row {:y [:sepal-width :petal-width]}})
    (select-keys [:mapping :facet])
    kind/pprint)
{:mapping {:x :sepal-length, :col :species},
 :facet {:y [:sepal-width :petal-width]}}

Because the faceting column is a mapping, a facet written on a composite reaches every cell of it, and a cell that writes its own overrides what it inherits:

(-> (pj/arrange [(pj/pose iris :sepal-length :sepal-width)
                 (pj/pose iris :petal-length :petal-width)])
    pj/lay-point
    (pj/facet :species))
sepal widthsepal length682.02.53.03.54.04.56868setosaversicolorvirginicapetal widthpetal length2460.00.51.01.52.02.5246246setosaversicolorvirginica
(-> (pj/arrange [(-> (pj/pose iris :sepal-length :sepal-width)
                     (pj/facet :species))
                 (pj/pose iris :petal-length :petal-width)])
    pj/lay-point)
sepal widthsepal length682.02.53.03.54.04.56868setosaversicolorvirginicapetal widthpetal length2460.00.51.01.52.02.5

A facet over columns is not a mapping. pj/facet given a map naming columns, such as {:y [:sepal-width :petal-width]}, keeps it under the pose’s :facet key, and the pose is expanded into a cell per column when it is drawn:

(-> iris
    (pj/pose {:x :sepal-length})
    pj/lay-point
    (pj/facet {:y [:sepal-width :petal-width]})
    pose-summary)
{:mapping {:x :sepal-length},
 :facet {:y [:sepal-width :petal-width]},
 :layers [{:layer-type :point}]}

Rule O4: pj/lay-rule-* and pj/lay-band-* are layers

pj/lay-rule-h, pj/lay-rule-v, pj/lay-band-h and pj/lay-band-v produce layers, and the layers are placed as any other lay-* layer is (Rules LP1 to LP3): on a composite a rule is added at the root and drawn in every cell that fits it – a rule draws from two columns, so a rule naming none is drawn in the cells of two columns – and where no cell fits it, in every cell. On a pose with one panel, columns written on a rule or a band that the panel does not draw are not read: the rule is drawn on that panel, and a warning names the columns. Where the line or the band sits is written as layer-type keys (:y-intercept, :x-intercept, :y-min/:y-max, :x-min/:x-max), not as columns.

(-> iris
    (pj/pose :sepal-length :sepal-width)
    (pj/lay-point {:color :species})
    (pj/lay-rule-h {:y-intercept 3.0}))
sepal widthsepal lengthspeciessetosaversicolorvirginica4.55.05.56.06.57.07.58.02.02.53.03.54.04.5

A rule naming two columns, on a composite, is drawn only in the cells whose panels draw those columns – here the first cell:

(-> iris
    (pj/arrange [{:x :sepal-length :y :sepal-width} {:x :petal-length :y :petal-width}])
    (pj/lay-rule-h :sepal-length :sepal-width {:y-intercept 3.0}))
sepal widthsepal length56782.02.53.03.54.04.5petal widthpetal length2460.00.51.01.52.02.5

The same call on a pose with one panel draws the rule on that panel. The petal columns are not read, so the axes are those of the points:

(-> iris
    (pj/pose :sepal-length :sepal-width)
    pj/lay-point
    (pj/lay-rule-h :petal-length :petal-width {:y-intercept 3.0}))
sepal widthsepal length4.55.05.56.06.57.07.58.02.02.53.03.54.04.5

Rule O5: a :theme combines key by key; other options are replaced

:theme and :config are maps of independent settings, so two of them combine key by key: pj/options twice, and a composite’s options reaching a cell that writes its own. Every other option is one value, replaced by the later or inner one – a :share-scales map included, since an axis it leaves out is not shared.

(-> iris
    (pj/pose :sepal-length :sepal-width)
    pj/lay-point
    (pj/options {:theme {:bg "#FFFFFF"}})
    (pj/options {:theme {:grid "#DDDDDD"}}))
sepal widthsepal length4.55.05.56.06.57.07.58.02.02.53.03.54.04.5

A cell with a theme of its own keeps the composite’s background:

(-> iris
    (pj/arrange [(-> (pj/pose nil {:x :sepal-length :y :sepal-width})
                     (pj/options {:theme {:grid "#DDDDDD"}}))
                 {:x :petal-length :y :petal-width}])
    (pj/options {:theme {:bg "#FFFFFF"}})
    pj/lay-point)
sepal widthsepal length56782.02.53.03.54.04.5petal widthpetal length2460.00.51.01.52.02.5

Assembly

How the rules above combine to produce rendered layers. Each rendered layer corresponds to one (leaf, applicable-layer) pair, with all ancestor scope merged in.

Rule A1: one rendered layer per applicable (leaf, layer) pair

For each leaf, the rendered layers are the leaf’s own, plus each layer written above it that fits the leaf, by Rule LP2 (Property P-LP1 for a layer naming no columns). A layer that fits no leaf below the composite is drawn in every leaf (Rule LP3). A leaf drawing none of these layers has a layer inferred for it.

(-> iris
    (pj/arrange [{:x :sepal-length :y :sepal-width} {:x :petal-length :y :petal-width}])
    ;; written on the root; drawn in both cells
    pj/lay-point
    ;; written on the root; drawn only in the cell whose panel draws
    ;; these columns
    (pj/lay-smooth :sepal-length :sepal-width
                   {:stat :linear-model}))
sepal widthsepal length56782.02.53.03.54.04.5petal widthpetal length2460.00.51.01.52.02.5

Rule A2: each rendered layer carries fully merged scope

Every layer rendered in the final plot reflects the full scope merge: the dataset, every mapping that flows into it, :x and :y among them, and the layer-type bundle (plus any :stat, :position, :mark overrides). No scope level is dropped; no key is unresolved.

(-> iris
    (pj/pose :sepal-length :sepal-width {:color :species})
    pj/lay-point)
sepal widthsepal lengthspeciessetosaversicolorvirginica4.55.05.56.06.57.07.58.02.02.53.03.54.04.5

Layout

How leaves become panels in the rendered plot. Five rules covering single panels, layers sharing a panel, faceting, composite grids with shared scales, and the scatter plot matrix.

Rule L1: each leaf produces a panel block

Each leaf produces one panel block in the rendered plot. Unfaceted, the block is one panel, which all of the leaf’s layers share (Rule LP5); pj/facet multiplies it by the facet values (or by the (row, col) pairs).

A facet over columns expands the leaf into one cell per column when the pose is drawn, so the plan of such a leaf holds :sub-plots, as the plan of a composite does.

(-> iris
    (pj/arrange [{:x :sepal-length :y :sepal-width} {:x :petal-length :y :petal-width}])
    pj/lay-point)
sepal widthsepal length56782.02.53.03.54.04.5petal widthpetal length2460.00.51.01.52.02.5

One leaf whose layers name two pairs of columns is one panel:

(-> iris
    (pj/lay-point :sepal-length :sepal-width)
    (pj/lay-point :petal-length :petal-width))
sepal width, petal widthsepal length, petal lengthsepal length / sepal widthpetal length / petal width24680.00.51.01.52.02.53.03.54.04.5

Rule L2: the layers of a leaf share its panel

Layers applicable to a leaf (the leaf’s own plus the layers written on the root that it draws) draw on the same axis pair – the leaf’s one panel, not separate panels (Rule LP5).

(-> iris
    (pj/pose :sepal-length :sepal-width {:color :species})
    pj/lay-point
    (pj/lay-smooth {:stat :linear-model}))
sepal widthsepal lengthspeciessetosaversicolorvirginica4.55.05.56.06.57.07.58.02.02.53.03.54.04.5

Rule L3: faceting splits each leaf into panels, by category or by column

(pj/facet pose :species) draws one panel per distinct value of :species. On the mpg data, (pj/facet pose {:col :drv :row :cyl}) draws one panel per pair of values, the values of :drv across and the values of :cyl down. (pj/facet pose {:y [:hwy :cty]}) draws one panel per column (Rule O3). Each of the three takes a map of plot options as its last argument, which is the map pj/options takes.

(-> iris
    (pj/pose :sepal-length :sepal-width)
    pj/lay-point
    (pj/facet :species))
sepal widthsepal length682.02.53.03.54.04.56868setosaversicolorvirginica

The options map of a facet by one category across may also hold :cols, the number of panels in a row. (pj/facet pose :class {:cols 4}) writes {:cols 4} on the pose’s :layout, not on its :opts, and draws the panels in rows of four:

(-> (rdatasets/ggplot2-mpg)
    (pj/lay-point :displ :hwy)
    (pj/facet :class {:cols 4}))
203040compactmidsizesuv2462seater246203040minivan246pickup246subcompactdisplhwy

Rule L4: composite layout is controlled by :layout and optional :share-scales

A composite pose carries a :layout map, which holds the direction of its rows and, in each row, the direction of the cells. pj/arrange writes :title, :width, :height and :share-scales into the composite’s :opts, and :cols into its :layout (Rule C8). An optional :share-scales (a subset of #{:x :y}, or a map from an axis to :same-column, :all, or groups of columns) enables shared-scale resolution across sub-poses. Under a set, sharing is column-bucketed.

Column-bucketing: when :x is shared, sub-poses whose effective :x column is the same share that scale’s domain; sub-poses with different :x columns get independent x-domains. :y is shared the same way. Marginal plots rest on this: a top strip draws the scatter’s :x column on the horizontal axis and shares :x with the scatter. A right strip is drawn with :coord :flip, so the scatter’s :y column runs up the strip’s vertical axis, and the strip and the scatter share :y. In :share-scales, :x is the horizontal axis and :y the vertical axis, also for a panel drawn with :coord :flip. A count or density axis is shared among the cells that compute the same thing from the same column.

(def l4-shared
  (pj/arrange
   [(-> iris
        (pj/pose :sepal-length :sepal-width)
        pj/lay-point)
    (-> iris
        (pj/pose :sepal-length :petal-width)
        pj/lay-point)]
   {:share-scales #{:x}}))
l4-shared
sepal width56782.02.53.03.54.04.5petal width56780.00.51.01.52.02.5sepal length

Rule L5: a facet crossing two lists of columns has one panel per pair

(pj/facet data {:col {:x xs} :row {:y ys}}) returns a leaf pose – no :poses – that carries that facet map: a column of the grid for each column of xs, a row for each column of ys, and a panel where they cross. Its axes, tick labels and strips are a facet’s (Rule L3). The same list in both places is a scatter plot matrix.

A panel with one column on both axes draws a histogram of that column, and every other panel draws the layer type inferred for its two columns – a scatter for two numerical columns. A layer added with pj/lay-* is drawn in the panels with as many columns as its layer type draws, so pj/lay-point after the facet leaves the histograms on the diagonal (Property P-LP1). pj/pose given pairs of columns reports an error that suggests pj/arrange and pj/matrix, which writes this facet.

(-> iris
    (pj/pose {:color :species})
    (pj/facet {:col {:x [:sepal-length :sepal-width]}
               :row {:y [:petal-length :petal-width]}}))
246680.00.51.01.52.02.5234sepal lengthsepal widthpetal lengthpetal widthspeciessetosaversicolorvirginica

A Note on pj/cross

pj/cross is deprecated, and calling it prints a warning. It returns the pairs (for [x xs y ys] [x y]), and a pair of columns is not a cell of pj/arrange, where a vector is a list of cells. For a panel for every column of xs crossed with every column of ys, write (pj/matrix data xs ys), which writes the facet of Rule L5:

(-> iris
    (pj/matrix [:sepal-length :sepal-width] [:petal-length :petal-width])
    pj/lay-point)
24656780.00.51.01.52.02.5234sepal lengthsepal widthpetal lengthpetal width

For a panel per pair of columns chosen one by one, write each pair as a mapping, as in Rule C8.

What’s Next

  • Column Types – the three types a column can have, and what each decides about the axis

  • Inference Rules – how Plotje fills in defaults (column types, marks, stats, scales) when you do not specify them

  • Layer Types – the registry of mark + stat + position combinations

source: notebooks/plotje_book/pose_rules.clj