1  Reference

The reference lists every option of cljplot2: the plot map, render options, scales, facets, labels, the theme, every chart, builder and user-facing data function. The tables are generated from the library’s validating schemas, so they name exactly what plot/build accepts. Use cases and galleries show the options at work; this part says what exists.

(ns reference.intro
  (:require [cljplot2.chart :as chart]
            [cljplot2.core :as plot]
            [cljplot2.describe :as describe]
            [cljplot2.pipeline :as pipeline]
            [notebook-utils :as nu]))

1.1 How the part is organised

  • Concepts (this chapter and the next eleven) - rendering, data and mapping, aesthetics and legends, scales and coordinates, layering, labels and text, facets and multiplots, the theme, the shared option groups, errors and what the library doesn’t do.

  • Charts, builders and data functions - the options of each chart kind, builder and user-facing function, with examples (to come; until then the use cases and galleries show them).

1.2 How to read a table

Each table is one group of related options, followed by an example using them - a chart or a value.

  • Option - the key; a nested key is listed after its map, with its full path (:scales :x). (required) marks a key that must be given.

  • Type - the values the schema accepts.

  • Default - the value used when the key is absent (style defaults come from the theme), and / or how a computed default is found. Empty: absent means not set - an optional feature off, or a map whose own keys have the defaults.

  • Description - what the key does.

  • Group - shown when a table mixes keys of a shared group (data and mapping, curves, strips …) with others; a group is described once, in the Shared options chapter.

1.3 A plot is a map

plot/render-image takes a plot map and render options and returns a java.awt.image.BufferedImage. The plot keys come in groups; each has its chapter.

1.3.1 Content

The rows, how their values map to dimensions, and the charts drawing them (Data and mapping chapter).

Option Type Default Description
:data a sequence of rows (maps) or a map of columns (sequences) or nil rows (a sequence of maps) or columns (a map of sequences) the charts read
:mapping a mapping: key -> accessor (a keyword or a function of a row) or nil accessors of the dimensions the charts read (keywords or functions), merged under each chart's
:charts vector of a chart value (see cljplot2.chart) or nil the charts, drawn in order
:groups map grouping options
(def points [{:x 1 :y 2 :g "a"} {:x 2 :y 3 :g "a"} {:x 3 :y 1 :g "b"} {:x 4 :y 4 :g "b"}])
x y g
1 2 a
2 3 a
3 1 b
4 4 b
(plot/render-image {:data points
                    :mapping {:x :x :y :y}
                    :charts [(chart/line) (chart/scatter)]}
                   {:width 500 :height 300})

The same rows under another mapping - :x and :y swapped - give another plot:

(plot/render-image {:data points
                    :mapping {:x :y :y :x}
                    :charts [(chart/line) (chart/scatter)]}
                   {:width 500 :height 300})

1.3.2 Scales

How data values become positions, colours, shapes … (Scales and Aesthetics chapters).

Option Type Default Description
:scales map position, facet and aesthetic scale specs
(plot/render-image {:data points
                    :mapping {:x :x :y :y :color :g}
                    :charts [(chart/scatter {:size 12})]
                    :scales {:y {:domain [0 5] :reverse true}
                             :color {:range [:darkorange :teal] :title "group"}}}
                   {:width 500 :height 300})

A logarithmic x scale spreads the small values:

(plot/render-image {:data (for [i (range 1 30)] {:x (* i i i) :y i})
                    :mapping {:x :x :y :y}
                    :charts [(chart/scatter)]
                    :scales {:x {:kind :log :formatter :power}}}
                   {:width 500 :height 300})

1.3.3 Coordinates

Cartesian (optionally flipped or with equal units) or polar (Scales chapter).

Option Type Default Description
:aspect one of :iso or nil :iso - equal pixels per data unit on x and y (linear scales)
:flip true or false or nil false draw the :x scale vertically and :y horizontally
:coord {:kind :cartesian} or a polar coord {:kind :polar ...} or nil cartesian coordinate system: cartesian or polar
(plot/render-image {:data [{:fruit "apples" :n 3} {:fruit "pears" :n 5} {:fruit "plums" :n 2}]
                    :mapping {:x :fruit :y :n}
                    :charts [(chart/bar)]
                    :scales {:x {:reverse true}}
                    :flip true}
                   {:width 500 :height 300})

The same bars in polar coordinates - a rose:

(plot/render-image {:data [{:fruit "apples" :n 3} {:fruit "pears" :n 5} {:fruit "plums" :n 2}]
                    :mapping {:x :fruit :y :n :color :fruit}
                    :charts [(chart/bar {:width 1.0})]
                    :coord {:kind :polar}
                    :legend false}
                   {:width 350 :height 350})

1.3.4 Layout

Panels split by data, several plots in a grid, charts beside the panel (Facets chapter).

Option Type Default Description
:facets map facet layout: wrap shape, order, NA panel, scale relations, strips, panel weights
:custom-grid map or nil a hand-written layout grid instead of the generated one (only :labels {:title t} beside it)
:arrange an arrange map {:plots :columns :rows :widths :heights :align :equal-panels} or nil a multiplot: sub-plots in a grid (only :labels {:title t} beside it)
:margins map charts beside the panel sharing its scales (marginal plots)
(plot/render-image {:data points
                    :mapping {:x :x :y :y :facet :g}
                    :charts [(chart/line) (chart/scatter)]}
                   {:width 500 :height 300})

A hand-written grid of plots and a note (:custom-grid, Facets chapter):

(plot/render-image {:custom-grid {:columns [{:size [:flexible 2]} {:size [:flexible 1]}]
                                  :rows [{:size [:flexible 1]} {:size [:measured]}]
                                  :items [{:id :lines :column 0 :row 0
                                           :content {:kind :plot :plot {:data points :mapping {:x :x :y :y}
                                                                        :charts [(chart/line)]}}}
                                          {:id :bars :column 1 :row 0
                                           :content {:kind :plot :plot {:data points :mapping {:x :g :y :y}
                                                                        :charts [(chart/bar {:stat :sum})]}}}
                                          {:id :note :column {:from 0 :to 1} :row 1
                                           :content {:kind :label :role :axis-title :text "two plots, 2 : 1"}}]}}
                   {:width 600 :height 320})

1.3.5 Titles

The plot title and the axis titles (Labels chapter).

Option Type Default Description
:labels map plot title and axis titles
(plot/render-image {:data points
                    :mapping {:x :x :y :y}
                    :charts [(chart/line)]
                    :labels {:title "Four points" :x "time [s]" :y "height [m]"}}
                   {:width 500 :height 300})

1.3.6 Legend

Legends are drawn for mapped aesthetics; these keys hide them or draw only them (Aesthetics chapter).

Option Type Default Description
:legend true or false or nil true draw the legends (false hides all)
:legend-only true or false or nil false draw only the legends (a legend cell of an :arrange)
(plot/render-image {:data points
                    :mapping {:x :x :y :y :color :g}
                    :charts [(chart/scatter {:size 12})]
                    :legend false}
                   {:width 500 :height 300})

1.3.7 Style

The theme holds every style default; a plot overrides parts of it (Theme chapter).

Option Type Default Description
:alternate-panel true or false or nil false shade a single panel as an odd facet panel (theme [:panel :alternate-fill])
:theme any value theme overrides, deep-merged over the default theme
(plot/render-image {:data points
                    :mapping {:x :x :y :y}
                    :charts [(chart/line) (chart/scatter)]
                    :theme {:plot {:background :white}
                            :panel {:fill [245 245 250] :border-color [180 180 180]}}}
                   {:width 500 :height 300})

1.3.8 Render options

Render options travel with the plot under :options; those given to render-image win (Render chapter).

Option Type Default Description
:options map or nil render options, merged under the ones given to build / render
(plot/render-image {:data points
                    :mapping {:x :x :y :y}
                    :charts [(chart/line)]
                    :options {:width 300 :height 200}})

1.4 The pipeline

plot/build validates the plot first and throws an error listing the problems (Errors chapter). Then the plot passes through pure stages, each adding one key: :split (rows split per facet panel and group) → :stats (each chart’s computed data) → :extents → :scale-specs → :grid → :layout → :finalized-scales → :subplots. plot/render draws the result.

1.5 The tables as data

The tables come from cljplot2.describe, usable at the REPL too: describe/options takes a chart value, a builder or data function var or a schema; describe/mapping the mapping keys a chart reads; describe/theme-rows the theme; describe/fragment-rows a shared group; describe/api-vars lists the user-facing data functions, describe/chart-constructors the charts, describe/attribute-values the built-in shapes, dashes, patterns and cells. A row is a plain map; :section names its table slice:

(first (describe/options pipeline/plot-schema))
{:description
 "rows (a sequence of maps) or columns (a map of sequences) the charts read",
 :path [:data],
 :required? false,
 :key :data,
 :fragment nil,
 :default nil,
 :section :content,
 :type
 "a sequence of rows (maps) or a map of columns (sequences) or nil",
 :default-doc nil}
source: notebooks/reference/intro.clj