21 Configuration
This chapter is the reference for configuration and plot options β the two categories of settings that sit above individual layers. See Options and Scopes for the taxonomy (why some settings are per-plot and others are per-project defaults).
Here you will find the tables of every key, the precedence chain, and worked examples for theme, palette, color scale, and validation.
(ns plotje-book.configuration
(:require
;; Kindly -- notebook rendering protocol
[scicloj.kindly.v4.kind :as kind]
;; Rdatasets -- standard datasets
[scicloj.metamorph.ml.rdatasets :as rdatasets]
;; Plotje -- composable plotting
[scicloj.plotje.api :as pj]))We use the iris dataset throughout.
A pose holds no configuration: it is drawn under the configuration in effect when it is rendered. So each example below that changes the configuration ends in pj/plot, which renders the pose while the change is in place. base-plot returns the scatter plot the examples start from.
(defn base-plot
[] (-> (rdatasets/datasets-iris)
(pj/lay-point :sepal-length :sepal-width {:color :species})))Inspecting the Current Configuration
pj/config returns the resolved configuration as a plain map. It merges every active level of configuration into one map.
(pj/config){:legend-font-size 10,
:strict false,
:validate true,
:point-stroke "none",
:title-offset 18,
:min-panel-size 20,
:label-offset 38,
:label-font-size 13,
:x-tick-spacing 60,
:thousands-separator nil,
:default-color "#333",
:width 600,
:legend-header-pad 20,
:y-tick-spacing 40,
:point-stroke-width 0,
:decimal-separator nil,
:legend-width 100,
:legend-entry-height 18,
:rule-color "#333",
:theme {:bg "#E8E8E8", :grid "#F5F5F5", :font-size 11},
:bin-method :sturges,
:domain-padding 0.05,
:strip-height 16,
:point-opacity 0.75,
:band-color "#7F7F7F",
:line-width 2.5,
:grid-stroke-width 0.6,
:fit-text-domain true,
:strip-font-size 11,
:strip-labels :value,
:title-font-size 15,
:band-opacity 0.15,
:bar-opacity 0.85,
:height 400,
:margin 10,
:point-radius 3.0}Configuration Keys
Each key, its default value, and a description.
(def category-order
["Layout" "Theme" "Typography" "Points" "Bars & Lines"
"Rules & Bands" "Ticks" "Statistics" "Labels" "Behavior"
"Color" "Interaction" "Output"])(kind/table
{:column-names ["Key" "Default" "Category" "Description"]
:row-maps
(let [cfg (pj/config)]
(->> pj/config-key-docs
(sort-by (fn [[k [cat]]]
[(.indexOf ^java.util.List category-order cat) (name k)]))
(mapv (fn [[k [cat desc]]]
{"Key" (kind/code (pr-str k))
"Default" (kind/code (pr-str (get cfg k)))
"Category" cat
"Description" desc}))))})| Key | Default | Category | Description |
|---|---|---|---|
|
|
Layout | Plot height |
|
|
Layout | Legend placement -- :right, :bottom, :top, or :none |
|
|
Layout | Width reserved for the legend column |
|
|
Layout | Margin around single-panel plots |
|
|
Layout | Smallest width and height a panel is drawn at. A plot whose panels would be smaller reports an error naming the sizes; lower this for a sparkline-sized plot |
|
|
Layout | Plot width |
|
|
Theme | Nested map {:bg :grid :font-size} -- visual identity |
|
|
Typography | Font size for axis labels |
|
|
Typography | Font size for legend entries and numbers; a legend's title is one larger |
|
|
Typography | Font size for facet strip labels |
|
|
Typography | Font size for the plot title |
|
|
Points | Default point opacity (0.0-1.0) |
|
|
Points | Default point radius |
|
|
Points | Point border stroke color ("none" to disable) |
|
|
Points | Point border stroke width |
|
|
Bars & Lines | Default bar fill opacity |
|
|
Bars & Lines | Grid line stroke width |
|
|
Bars & Lines | Default line stroke width |
|
|
Rules & Bands | Fill color a band draws in where its layer names none |
|
|
Rules & Bands | Fill opacity a band draws at where its layer names none |
|
|
Rules & Bands | Stroke color a rule draws in where its layer names none |
|
|
Ticks | Rotation angle for x-axis tick labels in degrees (0 = horizontal, -45 = common diagonal). A positive angle turns clockwise on screen, the opposite of ggplot2's element_text(angle =), so -45 here draws what angle = 45 draws there |
|
|
Ticks | Extra vertical space, in drawing units, reserved below panels for angled x-tick labels, added on top of :label-offset. When nil, computed from :x-tick-angle and the widest x-tick label, up to 30% of :height. When 0, no extra space is reserved and rotated labels may be clipped by the SVG boundary. |
|
|
Ticks | Target spacing, in drawing units, between ticks on the x axis |
|
|
Ticks | Rotation angle for y-axis tick labels in degrees (0 = horizontal, 90 or -90 = along the axis). A positive angle turns clockwise on screen, as :x-tick-angle does, so -90 reads from bottom to top |
|
|
Ticks | Target spacing, in drawing units, between ticks on the y axis |
|
|
Statistics | Histogram bin count method (:sturges, :sqrt, :rice, :fd) |
|
|
Statistics | Fractional padding added to numeric domains |
|
|
Labels | String drawn in place of the decimal point of a number that measures, in the same text :thousands-separator groups (a comma, with a point for the thousands, gives the European 1.234,5). Numbers are formatted under Locale/ROOT, so the two separators are chosen here rather than inherited from the JVM's locale. nil (the default) leaves the point as a point |
|
|
Labels | When true (the default), a numeric domain is widened so that text and label marks near its edge are drawn in full instead of being cut off at the panel boundary. A domain pinned with pj/scale is never widened |
|
|
Labels | Offset of the axis labels from the axis |
|
|
Labels | Height of facet strip label bars |
|
|
Labels | What a facet strip prints: :value (the default) for the category alone, or :both for the column's name and the category, as drv: f |
|
|
Labels | String inserted between three-digit groups of a number that measures (a comma gives 462,389): numeric axis tick labels, text/label mark content, and the values a size or alpha legend prints. A number that names is left alone -- category names, colour and shape legend labels, facet strips, and a categorical axis whose categories happen to be numbers. nil (the default) leaves everything ungrouped |
|
|
Labels | Offset of the title from the top |
|
|
Behavior | Fallback color when no color mapping is set |
|
|
Behavior | When true, throw on unknown option keys instead of warning and stripping |
|
|
Behavior | When true, validate plans against Malli schema |
|
|
Color | The value the middle of the :color gradient is drawn at, which centres a diverging gradient there rather than halfway along the data. The outermost scope of :midpoint in a :color scale spec |
|
|
Color | The gradient a numeric :color column is read through -- :sequential, :diverging, a gradient name, a {:low :high} map (two stops, or three with :mid), or a function. The outermost scope of :range in a :color scale spec, so a spec written on a mapping or a layer wins over it |
|
|
Color | The colours a categorical :color column is drawn in, as a palette name, a vector of colours, or a map from category to colour. The outermost scope of :values in a :color scale spec, so a spec written on a mapping or a layer wins over it |
|
|
Color | The value the middle of the :fill gradient is drawn at. The outermost scope of :midpoint in a :fill scale spec |
|
|
Color | The gradient a numeric :fill column is read through, in the same forms as :color-range. The outermost scope of :range in a :fill scale spec |
|
|
Interaction | Enable drag-to-select brush (truthy value) |
|
|
Interaction | Enable hover tooltips (truthy value) |
|
|
Output | Render format -- :svg (default) |
:panel-size and :margin-multi are retired. Written as configuration or as a plot option, each prints a warning naming what to write instead β :panel-width or :panel-height for the size of each panel, and :margin for the margin of every plot β and the plot is drawn without it.
Plot Options
These options are accepted directly by pj/options, pj/plan, and pj/plot; they are inherently per-plot, with no cross-plot defaults. The coordinate system, the scales and the facets are set with functions of their own β pj/coord, pj/scale and pj/facet β and pj/options does not accept them.
(kind/table
{:column-names ["Key" "Category" "Description"]
:row-maps
(->> pj/plot-option-docs
(sort-by (fn [[k [cat]]] [cat (name k)]))
(mapv (fn [[k [cat desc]]]
{"Key" (kind/code (pr-str k))
"Category" cat
"Description" desc})))})| Key | Category | Description |
|---|---|---|
|
Config | Nested config map merged into resolved config |
|
Content | Alpha legend title, overriding the inferred column name. The outermost scope of the same setting `:label` names in an `:alpha` scale spec, so a spec written on a mapping or a layer wins over it |
|
Content | Plot caption string (bottom) |
|
Content | Color legend title, overriding the inferred column name. The outermost scope of the same setting `:label` names in a `:color` scale spec, so a spec written on a mapping or a layer wins over it. It also titles the legend of a mark drawn in a fill where `:fill-label` is not given, as the other fill settings fall back to their `:color` ones |
|
Content | Title of the legend of a mark drawn in a fill -- a tile reading `:fill` or a numeric `:color`, a 2D density or a 2D histogram -- overriding the inferred name. Read after a `:label` in a `:fill` or `:color` scale spec, and before `:color-label`, which is the order every fill setting is read in. A contour is coloured through `:color`, and so is a tile given a categorical `:color`; `:color-label` titles both legends |
|
Content | Shape legend title, overriding the inferred column name. The outermost scope of the same setting `:label` names in a `:shape` scale spec, so a spec written on a mapping or a layer wins over it |
|
Content | Size legend title, overriding the inferred column name. The outermost scope of the same setting `:label` names in a `:size` scale spec, so a spec written on a mapping or a layer wins over it |
|
Content | Plot subtitle string |
|
Content | Plot title string |
|
Content | X-axis title, overriding the inferred column name. The outermost scope of the same setting `:label` names in an `:x` scale spec, so a spec written on a mapping or a layer wins over it |
|
Content | Y-axis title, overriding the inferred column name. The outermost scope of the same setting `:label` names in a `:y` scale spec, so a spec written on a mapping or a layer wins over it |
|
Layout | The relative widths of the columns of panels of an arrangement or of a facet over columns: a vector with one positive number per column, so [3 1] draws the first column's panels three times as wide as the second's. The plot keeps its :width; the tick labels, strips and titles take their room first, and the weights share out the rest |
|
Layout | Pin the panel height; the plot's :height is then worked out from it. On an arrangement or a facet over columns, a number pins every row's panels, and a vector one number per row, as in [100 300] |
|
Layout | Pin the panel width; the plot's :width is then worked out from it. On an arrangement or a facet over columns, a number pins every column's panels, and a vector one number per column, as in [300 100] |
|
Layout | The relative heights of the rows of panels of an arrangement or of a facet over columns: a vector with one positive number per row, so [1 3] draws the second row's panels three times as tall as the first's. The plot keeps its :height; the tick labels, strips and titles take their room first, and the weights share out the rest |
|
Layout | The axes the panels of a facet, or the cells of an arrangement, share -- a set of axes (#{:x :y}, #{:x}, #{:y}, #{}), or a map from an axis to :same-column, :all, or groups of columns, as in {:y [[:hwy :cty]]} |
Using Plot Options
Pass an options map to pj/options to override any setting for a single plot. Plot options have the highest priority β they override everything else.
Custom dimensions β the defaults are:
(select-keys (pj/config) [:width :height]){:width 600, :height 400}(-> (base-plot)
(pj/options {:width 900 :height 250}))A theme is merged key by key β only the keys written change. Here we set a white background; :grid and :font-size keep their defaults:
(-> (base-plot)
(pj/options {:theme {:bg "#FFFFFF"}}))Named palette:
(-> (base-plot)
(pj/options {:color-values :dark2}))Global Overrides with set-config!
pj/set-config! sets overrides that persist across calls β useful when you want a consistent style for an entire session or notebook.
Set a global width override and render. The override applies to what is rendered while it is in place, so the forms that show a plot end in pj/plot. The pages of this book draw every plot at the width of the text column, so on this page the wider canvas appears as wide as the plots above, and shorter β see Customization:
(pj/set-config! {:width 800}){:width 800}(select-keys (pj/config) [:width :height]){:width 800, :height 400}(-> (base-plot)
pj/plot)A plot of several panels takes the width as well β here a scatter plot matrix, which writes no size of its own:
(-> (rdatasets/datasets-iris)
(pj/matrix [:sepal-length :sepal-width])
pj/plot)Reset to library defaults by passing nil:
(pj/set-config! nil)nil(select-keys (pj/config) [:width :height]){:width 600, :height 400}Thread-Local Overrides with with-config
pj/with-config is a macro that binds configuration overrides for the duration of its body, then automatically reverts. This is useful for one-off experiments or when different sections of a notebook need different settings.
The overrides apply to what is rendered inside the body, so each example below ends in pj/plot within it. A pose returned from the body and shown afterwards is drawn under the configuration in effect when it is shown β the pose does not keep the overrides.
A dark theme, scoped to this block:
(pj/with-config {:theme {:bg "#1a1a2e" :grid "#16213e" :font-size 8}}
(-> (base-plot)
(pj/options {:title "Dark Theme via with-config"})
pj/plot))The same pose returned from the body without pj/plot is shown after the body has exited, under the default theme:
(pj/with-config {:theme {:bg "#1a1a2e" :grid "#16213e" :font-size 8}}
(-> (base-plot)
(pj/options {:title "Default theme: shown outside the body"})))Partial theme override β only :bg changes; :grid and :font-size keep the values of the defaults, merged key by key:
(pj/with-config {:theme {:bg "#F5F5DC"}}
(-> (base-plot)
(pj/options {:title "Partial Theme Override"})
pj/plot))Outside the body, the default theme is back:
(:theme (pj/config)){:bg "#E8E8E8", :grid "#F5F5F5", :font-size 11}The Precedence Chain
When several levels of configuration are active, the highest-priority level wins for each key. The chain from highest to lowest:
plot options > with-config > set-config! > plotje.edn > library defaults
Letβs demonstrate all three programmatic levels at once, using a different key at each level so we can see each one win.
Before overriding, the library default for point-radius is:
(:point-radius (pj/config))3.0Now set a global override for width, height, and point-radius:
(pj/set-config! {:width 800 :height 350 :point-radius 5.0}){:width 800, :height 350, :point-radius 5.0}Add a thread-local override on top for width and height (but not point-radius). The rendered plot reflects the merged configuration:
(def precedence-plot
(pj/with-config {:width 1200 :height 500}
;; Pass plot options for width only, and materialize the plot
;; inside the with-config so the thread-local override applies:
(pj/plot (-> (base-plot)
(pj/options {:width 900})))))precedence-plotOnly set-config! sets point-radius, so the set-config! value wins over the library default shown above:
(def precedence-point-radius
(pj/with-config {:width 1200 :height 500}
(:point-radius (pj/config))))precedence-point-radius5.0Clean up the global override.
(pj/set-config! nil)nil(select-keys (pj/config) [:width :height :point-radius]){:width 600, :height 400, :point-radius 3.0}To summarize what happened:
| Key | Library default | set-config! | with-config | plot options | Winner |
|---|---|---|---|---|---|
:width |
600 | 800 | 1200 | 900 | 900 (plot options) |
:height |
400 | 350 | 500 | β | 500 (with-config) |
:point-radius |
3.0 | 5.0 | β | β | 5.0 (set-config!) |
Project-Level Defaults with plotje.edn
For team-wide consistency, create a plotje.edn file in your project root (or anywhere on the classpath). It is read automatically with a 1-second cache.
Example plotje.edn:
{:width 800
:height 500
:theme {:bg "#FFFFFF" :grid "#F0F0F0" :font-size 10}
:color-values :tableau-10
:point-radius 3}This level sits between library defaults and set-config! in the precedence chain β it overrides defaults but is overridden by any programmatic configuration.
Theme Customization
The :theme key is a nested map with three entries:
:bgβ panel background color (hex string):gridβ grid line color (hex string):font-sizeβ tick label font size (number)
(set (keys (:theme (pj/config))))#{:font-size :grid :bg}At every level (pj/options, pj/with-config, pj/set-config!, and plotje.edn) a :theme is merged key by key into the one below it, so you only need to specify the theme keys you want to change. Every other key is replaced whole by the higher levelβs value.
Override only :bg, keep default :grid and :font-size:
(-> (base-plot)
(pj/options {:theme {:bg "#F5F5DC"}})
pj/plot)Override all three for a dark theme:
(-> (base-plot)
(pj/options {:title "Full Dark Theme"
:theme {:bg "#2d2d2d" :grid "#444444" :font-size 10}})
pj/plot)Comparing Two Themes Side by Side
pj/arrange draws several poses side by side in one picture, and each pose keeps its own theme.
(pj/arrange
[(-> (base-plot)
(pj/options {:title "Light"
:theme {:bg "#FFFFFF" :grid "#EEEEEE" :font-size 8}}))
(-> (base-plot)
(pj/options {:title "Dark"
:theme {:bg "#2d2d2d" :grid "#444444" :font-size 8}}))])Palette Configuration
The :color-values key controls the color cycle for categorical color mappings. It is the outermost scope of :values in a :color scale spec, so a spec written on a mapping or a layer wins over it; Scales has the spec spelling. It accepts:
a keyword β any palette name from the clojure2d color library (thousands available: ColorBrewer, Wes Anderson, thi.ng, paletteer and others)
a vector of hex strings:
["#E74C3C" "#3498DB" "#2ECC71"]a map of
{category-value "#hex"}for explicit assignment
Common palette names: :set1, :set2, :dark2, :tableau-10, :category10, :pastel1, :accent, :paired. Use (clojure2d.color/find-palette #"pattern") to discover more.
The key works at every configuration level.
Named palette via plot options:
(-> (base-plot)
(pj/options {:color-values :tableau-10}))Custom vector palette:
(-> (base-plot)
(pj/options {:color-values ["#E74C3C" "#3498DB" "#2ECC71"]}))Explicit map palette:
(-> (base-plot)
(pj/options {:color-values {"setosa" "#FF6B6B"
"versicolor" "#4ECDC4"
"virginica" "#45B7D1"}}))Global palette via set-config!:
(pj/set-config! {:color-values :pastel1}){:color-values :pastel1}(-> (base-plot)
pj/plot)(pj/set-config! nil)nilThread-local palette via with-config:
(pj/with-config {:color-values :accent}
(-> (base-plot)
pj/plot))Color Scale Configuration
When a numeric column is mapped to :color, Plotje uses a continuous gradient (dark-to-light blue by default). The :color-range option controls which gradient is used, and is the outermost scope of :range in a :color scale spec.
Default continuous color (dark blue to light blue):
(-> {:x (range 50) :y (range 50) :c (range 50)}
(pj/lay-point :x :y {:color :c}))Color scale override via plot options β inferno gradient. The chosen palette propagates so the rendered legend uses the matching gradient stops:
(-> {:x (range 50) :y (range 50) :c (range 50)}
(pj/lay-point :x :y {:color :c})
(pj/options {:color-range :inferno}))Thread-local color scale via with-config:
(pj/with-config {:color-range :plasma}
(-> {:x (range 50) :y (range 50) :c (range 50)}
(pj/lay-point :x :y {:color :c})
pj/plot))Validation Control
By default, pj/plan validates the output against a Malli schema and throws if the plan is malformed. This is controlled by the :validate key.
Two helper functions let you inspect plans manually:
pj/valid-plan?β returns true or falsepj/explain-planβ returns nil if valid, or a Malli explanation map
The membrane stage has the same pair β pj/valid-membrane? and pj/explain-membrane β shown in the Membranes chapter, and so does the pose: pj/valid-pose? and pj/explain-pose. A pose built by pj/pose, pj/lay-* and the rest conforms by construction, so the pose pair is for a pose that has been reached into and changed.
Default behavior (validate = true) β a valid plan passes silently:
(-> (base-plot)
pj/plan
pj/valid-plan?)trueThe rendered plot works normally:
(base-plot)What Validation Catches
To see what happens when a plan is malformed, we can build one with validation disabled, then corrupt it. First, create a valid plan and confirm it passes:
(def good-plan (pj/plan (base-plot) {:validate false}))(pj/valid-plan? good-plan)trueNow corrupt the :width to a string β this violates the schema, which requires a positive integer:
(def bad-plan (assoc good-plan :width "not-a-number"))(pj/valid-plan? bad-plan)falsepj/explain-plan reports the problem. The :errors key in the returned map shows which path failed and why. The :in key is the value-path (where in the plan the bad value lives); :value is the offending value:
(-> (pj/explain-plan bad-plan)
:errors
first
(select-keys [:in :value])){:in [:width], :value "not-a-number"}With validation enabled (the default), pj/plan checks the plan it builds. A plan that does not conform makes pj/plan throw an exception with the message Plan does not conform to schema, and the exceptionβs data holds the pj/explain-plan map under :explanation. The plans the built-in layer types produce conform, so the check fires on a plan from an extension β an extract-layer method that leaves out a required key, as Extensibility describes.
Disabling Validation
Disable validation with :validate false. The plan is built the same way and is not checked:
(-> (base-plot)
(pj/plan {:validate false})
pj/valid-plan?)trueYou can also disable validation globally for a debugging session:
(pj/set-config! {:validate false})
;; ... work freely ...
(pj/set-config! nil) ;; re-enableStrict Option Checking
When you pass an unknown key to pj/options, pj/lay-*, or pj/plot, Plotje by default warns and strips it. The :strict configuration flag turns the warning into a thrown exception β useful in tests or pipelines where a typo should fail loudly rather than silently render a default plot.
Default behavior: warn and continue. The plot renders normally, and the unknown key is dropped with a warning:
(pj/with-config {:strict false}
(-> (rdatasets/datasets-iris)
(pj/lay-point :sepal-length :sepal-width)
(pj/options {:nonsense-key 42})
pj/plot))Warning: pj/options does not recognize [:nonsense-key].
Accepted: [:alpha-label :band-color :band-opacity :bar-opacity :bin-method :brush :caption :col-weights :color-label :color-midpoint :color-range :color-values :config :decimal-separator :default-color :domain-padding :fill-label :fill-midpoint :fill-range :fit-text-domain :format :grid-stroke-width :height :label-font-size :label-offset :legend-font-size :legend-position :legend-width :line-width :margin :min-panel-size :panel-height :panel-width :point-opacity :point-radius :point-stroke :point-stroke-width :row-weights :rule-color :shape-label :share-scales :size-label :strict :strip-font-size :strip-height :strip-labels :subtitle :theme :thousands-separator :title :title-font-size :title-offset :tooltip :validate :width :x-label :x-tick-angle :x-tick-label-pad :x-tick-spacing :y-label :y-tick-angle :y-tick-spacing]
With :strict true, the same call throws.
(pj/with-config {:strict true}
(try
(-> (rdatasets/datasets-iris)
(pj/lay-point :sepal-length :sepal-width)
(pj/options {:nonsense-key 42})
pj/plot)
(catch Exception e (.getMessage e))))"pj/options does not recognize [:nonsense-key].\n Accepted: [:alpha-label :band-color :band-opacity :bar-opacity :bin-method :brush :caption :col-weights :color-label :color-midpoint :color-range :color-values :config :decimal-separator :default-color :domain-padding :fill-label :fill-midpoint :fill-range :fit-text-domain :format :grid-stroke-width :height :label-font-size :label-offset :legend-font-size :legend-position :legend-width :line-width :margin :min-panel-size :panel-height :panel-width :point-opacity :point-radius :point-stroke :point-stroke-width :row-weights :rule-color :shape-label :share-scales :size-label :strict :strip-font-size :strip-height :strip-labels :subtitle :theme :thousands-separator :title :title-font-size :title-offset :tooltip :validate :width :x-label :x-tick-angle :x-tick-label-pad :x-tick-spacing :y-label :y-tick-angle :y-tick-spacing]\n (Set :strict false in plotje.edn or via with-config to downgrade to a warning.)"Summary
| Mechanism | Scope | Persistence | Example |
|---|---|---|---|
| plot options | single call | none | (pj/options {...}) or (pj/plot pose {...}) |
with-config |
the body and every function it calls, on this thread | until the body exits | (pj/with-config {:width 800} ...) |
set-config! |
global | until reset | (pj/set-config! {:width 800}) |
plotje.edn |
project | file on disk | {:width 800} in project root |
| library defaults | everywhere | built-in | resources/plotje-defaults.edn |
Use pj/config at any time to see the resolved configuration.
Whatβs Next
- Scales β types, domains, ranges, and the scale spec each aesthetic reads
- Customization β titles, color scales, palettes, and themes
- Placing Marks β where a mark goes, and in what units
- Faceting β split plots into panels by category