29  Troubleshooting

Common mistakes and how to fix them.

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

Column Not Found

Symptom: "Column :foo (from :x) not found in dataset" error, listing the available columns at the end of the message.

Cause: The column reference does not match a dataset column name. Matching is strict – :foo matches keyword column :foo only, and "foo" matches string column "foo" only. The two forms do not interchange. Three common triggers:

1. Typo. A misspelled column name. Check the spelling against the dataset’s columns:

(tc/column-names (rdatasets/datasets-iris))
(:rownames
 :sepal-length
 :sepal-width
 :petal-length
 :petal-width
 :species)

2. Keyword vs string. A CSV loaded without :key-fn keyword produces string column names; using a keyword reference against that dataset throws:

(try
  (-> (tc/dataset {"sepal_length" [5.0 6.0] "sepal_width" [3.0 3.5]})
      (pj/pose :sepal_length :sepal_width)
      pj/lay-point
      pj/plot)
  (catch clojure.lang.ExceptionInfo e (ex-message e)))
"Column :sepal_width (from :y) not found in dataset. Available: (\"sepal_length\" \"sepal_width\") To place the mark at :sepal_width itself rather than read a column, write {:y {:value :sepal_width}}."

The fix is to either pass {:key-fn keyword} when loading the CSV (so the dataset has keyword columns) or to use string references everywhere.

3. Whitespace or punctuation mismatch. A column literally named "sepal length" (with a space) does not match :sepal-length (with a hyphen):

(try
  (-> (tc/dataset {"sepal length" [5.0 6.0] "sepal width" [3.0 3.5]})
      (pj/pose :sepal-length :sepal-width)
      pj/lay-point
      pj/plot)
  (catch clojure.lang.ExceptionInfo e (ex-message e)))
"Column :sepal-width (from :y) not found in dataset. Available: (\"sepal length\" \"sepal width\") To place the mark at :sepal-width itself rather than read a column, write {:y {:value :sepal-width}}."

:key-fn keyword on "sepal length" produces :sepal length – a keyword whose printed form contains a space, not the hyphenated form a Clojure reader would normally produce. Spaces and other special characters in CSV headers usually need a custom :key-fn, e.g. (comp keyword #(clojure.string/replace % " " "-")).

Wrong Chart Type from Inference

Symptom: pj/pose produces a chart type that isn’t what you wanted – a boxplot when you wanted individual points, a line when you wanted a scatter.

Cause: pj/pose infers the layer type from column types. The defaults fit the most common use case for each column-type pair (see Inference Rules), but they can be overridden.

Fix: Use an explicit pj/lay-* function. For example, a categorical x with a numerical y defaults to a boxplot:

(-> (rdatasets/datasets-iris)
    (pj/pose :species :sepal-width))
sepal widthspeciessetosaversicolorvirginica2.02.53.03.54.04.5

Use pj/lay-point if you want the individual points instead:

(-> (rdatasets/datasets-iris)
    (pj/lay-point :species :sepal-width))
sepal widthspeciessetosaversicolorvirginica2.02.53.03.54.04.5

Numeric IDs Treated as Continuous Color

Symptom: You color by a subject/group ID column that contains numbers (e.g., 1, 2, 3), but instead of discrete colored groups you get a single continuous gradient.

Cause: Inference treats a numeric column as continuous. Continuous color means no grouping – all data stays in one group with a gradient legend.

(def subject-scores
  {:day     [1 2 3 4 1 2 3 4 1 2 3 4]
   :score   [3 5 4 6 6 7 5 8 8 9 7 10]
   :subject [1 1 1 1 2 2 2 2 3 3 3 3]})

Gradient (wrong for IDs) – one line for the whole dataset, drawn in the default color rather than in any color from the gradient: a line passes through rows of twelve different values and can take none of them. The legend still shows the gradient, and Plotje reports at the REPL that the column was not used, pointing at the fix:

(-> subject-scores
    (pj/lay-line :day :score {:color :subject}))
scoredaysubject1231234345678910

Fix: Add :color-type :categorical to override the inference – three discrete groups, one line per subject:

(-> subject-scores
    (pj/lay-line :day :score {:color :subject :color-type :categorical}))
scoredaysubject1231234345678910

See Inference Rules for the full mechanism.

Numeric Column Treated as Continuous Instead of Categorical

Symptom: A column of discrete numbers (hour of day, year, subject ID) is treated as a continuous axis. A categorical-axis mark like :boxplot, :violin, or :lollipop rejects it with an error like "requires a categorical column".

A pj/lay-bar given a numeric :x column and a value column draws a bar at each value and reports nothing; a counting pj/lay-bar given one numeric column reports the error. The fix below gives evenly spaced bands in both cases.

Cause: The column contains numbers, so column-type inference classifies it as :numerical. These marks need :categorical.

A boxplot keyed by hour runs into this – :hour looks like integers, so it is inferred numerical:

(try
  (-> {:hour [9 9 10 10 11 11] :value [1 2 3 4 5 6]}
      (pj/lay-boxplot :hour :value)
      pj/plan)
  (catch clojure.lang.ExceptionInfo e (ex-message e)))
"pj/lay-boxplot requires a categorical column on either :x or :y, but :hour is numerical and :value is numerical. Override with {:x-type :categorical} or {:y-type :categorical} to treat a numeric column as categorical."

Fix: Add :x-type :categorical (or :y-type :categorical for horizontal layouts) to override the inferred type. No need to convert the column itself:

(-> {:hour [9 9 10 10 11 11] :value [1 2 3 4 5 6]}
    (pj/lay-boxplot :hour :value {:x-type :categorical}))
valuehour91011123456

The override propagates into infer-column-types, so every downstream step (scale type, tick placement, domain) treats :hour as categorical. The same switch works for :y-type when a numeric column is on the y axis of a horizontal boxplot or similar layout. See Inference Rules for a worked example.

A Number Where a Category Was Meant

Symptom: A value written for a categorical axis reports "pj/plan got 2021 for :x, which is past the ends of this axis".

Cause: A number written for a categorical axis is a place among the categories rather than one of them, counted from one. On an axis built from the years 2020, 2021 and 2022 the number 2021 is a place far past the third and last category, which is what the error names. Placing Marks teaches the reading in full.

(try
  (-> {:cohort [2020 2021 2022] :n [3 5 4]}
      (pj/lay-bar :cohort :n {:x-type :categorical})
      (pj/lay-text {:x 2021 :y 5.5 :text "the 2021 cohort"})
      pj/plan)
  (catch clojure.lang.ExceptionInfo e (ex-message e)))
"pj/plan got 2021 for :x, which is past the ends of this axis. Categories: [\"2020\" \"2021\" \"2022\"]. A number written for a categorical axis is a place counted from one -- 1 is the first category, 1.5 sits halfway to the second -- and this axis runs from 0.5 to 3.5. To move a mark by a distance on the page instead, use :offset-x / :offset-y, which work on any axis; to draw the number as a category, give the axis a column of them."

Fix: Name the category as a value. {:x {:value "2021"}} puts the mark on that band whatever numbers the categories are written from:

(-> {:cohort [2020 2021 2022] :n [3 5 4]}
    (pj/lay-bar :cohort :n {:x-type :categorical :color "#a6cee3"})
    (pj/lay-text {:x {:value "2021"} :y 4.5 :align-x :center
                  :text "on the band"}))
ncohorton the band2020202120220.00.51.01.52.02.53.03.54.04.55.0

Moving a Mark Clear of Another

Symptom: A value label sits on top of the bar it names, and the amount that separates them has to be guessed again on every chart.

Cause: A :dx or :dy is a distance in the data – a fraction of a band on a categorical axis, a value on a numeric one – so the number that clears a mark depends on what the axis holds. A label has to clear its mark by roughly the mark’s own size, which is a length on the page.

Fix: Use :offset-x/:offset-y, which are drawing units applied after the scales and so mean the same on every axis:

(-> {:species ["setosa" "versicolor" "virginica"] :pct [33.3 33.3 33.3]}
    (pj/lay-bar :species :pct)
    (pj/lay-text :species :pct {:text :pct :align-x :center :offset-y -6}))
pctspecies33.333.333.3setosaversicolorvirginica051015202530

To place a label relative to its own point, anchor it with :align-x/:align-y – :align-x :right puts the label’s right edge at the bar’s end, so the text sits inside the bar. (To spread overlapping marks on a categorical axis, use :jitter or :position :dodge.)

(-> {:species ["setosa" "versicolor" "virginica"] :pct [33.3 33.3 33.3]}
    (pj/lay-bar :species :pct {:color "#a6cee3"})
    (pj/lay-text :species :pct {:text :pct :align-x :right})
    (pj/coord :flip))
speciespct33.333.333.3051015202530setosaversicolorvirginica

Anchoring is covered in Placing Marks, and :dx itself in the same chapter.

Log Scale via :scale-x / :scale-y Options

Symptom: Passing {:scale-x :log} (or {:scale-y :log}) to a layer or to pj/options prints a warning – "does not recognize [:scale-x]" – and the chart comes out on a linear axis.

Cause: :scale-x and :scale-y are not option keys at all. A scale is set with pj/scale, called on the pose, or inside a mapping written out in full – {:x {:column :carat :scale :log}}.

The wrong form does not throw; it warns and falls back to a linear axis:

(with-out-str
  (-> (rdatasets/ggplot2-diamonds)
      (pj/lay-point :carat :price {:scale-y :log})
      pj/plan))
"Warning: pj/lay-point does not recognize [:scale-y].\n  Set by pj/scale, not by an options map: [:scale-y]\n  Accepted: [:alpha :color :color-type :data :dx :dy :group :in :jitter :mark :offset-x :offset-y :position :shape :size :stat :tooltip :x :x-type :y :y-type]\n"

Fix: Use pj/scale:

(-> (rdatasets/ggplot2-diamonds)
    (pj/lay-point :carat :price {:alpha 0.1})
    (pj/scale :y :log))
pricecarat