17 Relationships
Regression, smoothing, density estimation, and heatmaps β revealing structure between two variables.
(ns plotje-book.relationships
(: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]
;; Fastmath -- random number generation
[fastmath.random :as rng]))Linear Regression
A single regression line through all data.
(-> (rdatasets/datasets-iris)
(pj/lay-point :sepal-length :sepal-width)
(pj/lay-smooth {:stat :linear-model}))Per-Group Regression
Fit a regression line per group.
(-> (rdatasets/datasets-iris)
(pj/pose :petal-length :petal-width {:color :species})
pj/lay-point
(pj/lay-smooth {:stat :linear-model}))Regression with Confidence Ribbon
Pass {:confidence-band true} to show a 95% confidence band around the line.
(-> (rdatasets/datasets-iris)
(pj/pose :sepal-length :sepal-width {:color :species})
pj/lay-point
(pj/lay-smooth {:stat :linear-model :confidence-band true}))Tips with Regression
Do smokers and non-smokers tip differently?
(-> (rdatasets/reshape2-tips)
(pj/pose :total-bill :tip {:color :smoker})
pj/lay-point
(pj/lay-smooth {:stat :linear-model}))LOESS Smoothing
A smooth curve through noisy data.
(-> (let [r (rng/rng :jdk 42)
xs (vec (range 50))]
{:x xs
:y (mapv #(+ (Math/sin (* % 0.2))
(* 0.3 (- (rng/drandom r) 0.5)))
xs)})
(pj/lay-point :x :y)
(pj/lay-smooth {:bandwidth 0.2}))Heatmap (Auto-Binned)
Bin x and y into a grid, count points per cell.
(-> (rdatasets/datasets-iris)
(pj/lay-tile :sepal-length :sepal-width))Heatmap (Pre-Computed)
Use a numeric column for tile color.
(def grid-data
(let [r (rng/rng :jdk 99)]
{:x (for [i (range 5) _j (range 5)] i)
:y (for [_i (range 5) j (range 5)] j)
:value (repeatedly 25 #(rng/irandom r 100))}))(-> grid-data
(pj/lay-tile :x :y {:fill :value}))Density 2D
KDE-smoothed 2D density heatmap.
(-> (rdatasets/datasets-iris)
(pj/lay-density-2d :sepal-length :sepal-width))Density 2D with Points
Overlay scatter points on the density heatmap.
(-> (rdatasets/datasets-iris)
(pj/lay-density-2d :sepal-length :sepal-width)
(pj/lay-point {:alpha 0.5}))Contour Lines
Iso-density contour lines from 2D KDE.
(-> (rdatasets/datasets-iris)
(pj/lay-contour :sepal-length :sepal-width))Contour with Points
Contour lines overlaid on scatter points.
(-> (rdatasets/datasets-iris)
(pj/lay-point :sepal-length :sepal-width {:alpha 0.3})
(pj/lay-contour {:levels 8}))Whatβs Next
- Polar Coordinates β radial charts and pie-style visualizations
- Faceting β split any chart into panels by category