9  Scatter

Point mark variations β€” color, size, alpha, shape, jitter, and continuous color scale.

(ns napkinsketch-book.scatter
  (:require
   ;; Shared datasets for these docs
   [napkinsketch-book.datasets :as data]
   ;; Kindly β€” notebook rendering protocol
   [scicloj.kindly.v4.kind :as kind]
   ;; Napkinsketch β€” composable plotting
   [scicloj.napkinsketch.api :as sk]))

Basic Scatter

Sepal dimensions, no color β€” the default mark.

(-> data/iris
    (sk/lay-point :sepal_length :sepal_width))
sepal widthsepal length4.55.05.56.06.57.07.58.02.02.53.03.54.04.5

Colored by Species

Adding :color :species groups points by species with distinct colors.

(-> data/iris
    (sk/lay-point :sepal_length :sepal_width {:color :species}))
sepal widthsepal lengthspeciessetosaversicolorvirginica4.55.05.56.06.57.07.58.02.02.53.03.54.04.5

Petal Dimensions

Petal length vs width β€” a strongly correlated pair.

(-> data/iris
    (sk/lay-point :petal_length :petal_width {:color :species}))
petal widthpetal lengthspeciessetosaversicolorvirginica12345670.00.51.01.52.02.5

Fixed Color

A fixed color string (not a column reference) applies to all points.

(-> data/iris
    (sk/lay-point :sepal_length :sepal_width {:color "#E74C3C"}))
sepal widthsepal length4.55.05.56.06.57.07.58.02.02.53.03.54.04.5

Custom Dimensions

Wider plot with custom title and labels.

(-> data/tips
    (sk/lay-point :total_bill :tip {:color :day})
    (sk/options {:width 700 :height 300
                 :title "Tips by Day"
                 :x-label "Total Bill ($)"
                 :y-label "Tip ($)"}))
Tips by DayTip ($)Total Bill ($)daySunSatThurFri5101520253035404550246810

Bubble Plot

Map :size to a numeric column to create a bubble plot. Each point’s radius reflects the column value.

(-> data/tips
    (sk/lay-point :total_bill :tip {:color :day :size :size}))
tiptotal billdaySunSatThurFrisize1.02.33.54.86.0510152025303540455012345678910

Combine size with alpha for dense data.

(-> data/tips
    (sk/lay-point :total_bill :tip {:color :day :size :size :alpha 0.6}))
tiptotal billdaySunSatThurFrisize1.02.33.54.86.0510152025303540455012345678910

Jitter

When plotting a numeric column against a categorical column, points overlap. Use :jitter true to add random pixel offsets.

(-> data/iris
    (sk/lay-point :species :sepal_width {:jitter true}))
sepal widthspeciessetosaversicolorvirginica2.02.53.03.54.04.5

Control the jitter amount in pixels.

(-> data/iris
    (sk/lay-point :species :sepal_width {:jitter 10 :alpha 0.5}))
sepal widthspeciessetosaversicolorvirginica2.02.53.03.54.04.5

Continuous Color

When :color maps to a numeric column, Napkinsketch uses a continuous blue gradient instead of discrete palette colors.

(-> data/iris
    (sk/lay-point :sepal_length :sepal_width {:color :petal_length}))
sepal widthsepal lengthpetal length1.0006.9004.55.05.56.06.57.07.58.02.02.53.03.54.04.5

Continuous color with size β€” a color-size bubble plot.

(-> data/iris
    (sk/lay-point :sepal_length :sepal_width {:color :petal_length :size :petal_width :alpha 0.7}))
sepal widthsepal lengthpetal length1.0006.900petal width0.10.71.31.92.54.55.05.56.06.57.07.58.02.02.53.03.54.04.5

Scatter Plot Matrix (SPLOM)

sk/cross generates all combinations of two lists. Passing column names produces a grid of scatter plots β€” one per pair of variables. The diagonal shows histograms (automatic inference for same-column pairs).

(def cols [:sepal_length :sepal_width :petal_length :petal_width])
(-> data/iris
    (sk/view (sk/cross cols cols) {:color :species})
    sk/lay-point)
speciessetosaversicolorvirginica5678234246680122345012sepal lengthsepal widthpetal lengthpetal widthsepal lengthsepal widthpetal lengthpetal width

See the Faceting chapter for more SPLOM variations, including brush selection.

What’s Next

source: notebooks/napkinsketch_book/scatter.clj