|
9 | 9 | :tags [:datavis] |
10 | 10 | :keywords [:datavis] |
11 | 11 | :toc true}}} |
12 | | -(ns data-visualization.aog-in-clojure-part1) |
| 12 | +(ns data-visualization.aog-in-clojure-part1 |
| 13 | + (:require [tablecloth.api :as tc])) |
13 | 14 |
|
14 | 15 | ^{:kindly/hide-code true |
15 | 16 | :kindly/kind :kind/hiccup} |
@@ -3818,11 +3819,21 @@ iris |
3818 | 3819 | (let [;; For faceted plots, remove :data from individual layers |
3819 | 3820 | ;; Data goes at TOP level for VL faceting, not inside spec |
3820 | 3821 | layers-without-data (mapv #(dissoc % :data) vl-layers) |
3821 | | - all-data (mapcat layer->vl-data layers-vec)] |
| 3822 | + all-data (mapcat layer->vl-data layers-vec) |
| 3823 | + |
| 3824 | + ;; Count actual number of unique facet values |
| 3825 | + first-layer (first layers-vec) |
| 3826 | + dataset (ensure-dataset (:=data first-layer)) |
| 3827 | + num-cols (if col-var |
| 3828 | + (count (distinct (tc/column dataset col-var))) |
| 3829 | + 1) |
| 3830 | + num-rows (if row-var |
| 3831 | + (count (distinct (tc/column dataset row-var))) |
| 3832 | + 1)] |
3822 | 3833 | (cond-> {:$schema "https://vega.github.io/schema/vega-lite/v5.json" |
3823 | 3834 | :data {:values all-data} |
3824 | | - :width (int (/ width (if col-var 3 1))) |
3825 | | - :height (int (/ height (if row-var 3 1))) |
| 3835 | + :width (int (/ width num-cols)) |
| 3836 | + :height (int (/ height num-rows)) |
3826 | 3837 | :config ggplot2-config |
3827 | 3838 | :spec {:layer layers-without-data}} |
3828 | 3839 | col-var (assoc :facet {:column {:field (name col-var) :type "nominal"}}) |
@@ -4035,6 +4046,22 @@ iris |
4035 | 4046 | :line {:color (:grid theme) :width 1}} |
4036 | 4047 | :showlegend false}) |
4037 | 4048 |
|
| 4049 | + :grouped-histogram |
| 4050 | + ;; For faceted histograms, data is already filtered to single facet |
| 4051 | + ;; but transform still returns grouped result with one group |
| 4052 | + (let [groups (:groups transform-result) |
| 4053 | + ;; Extract bars from the single group (first value in groups map) |
| 4054 | + bars (:bars (second (first groups)))] |
| 4055 | + {:type "bar" |
| 4056 | + :x (mapv (fn [b] (/ (+ (:x-min b) (:x-max b)) 2)) bars) |
| 4057 | + :y (mapv :height bars) |
| 4058 | + :width (mapv (fn [b] (- (:x-max b) (:x-min b))) bars) |
| 4059 | + :xaxis (name xaxis-key) |
| 4060 | + :yaxis (name yaxis-key) |
| 4061 | + :marker {:color (:default-mark theme) |
| 4062 | + :line {:color (:grid theme) :width 1}} |
| 4063 | + :showlegend false}) |
| 4064 | + |
4038 | 4065 | nil))) |
4039 | 4066 |
|
4040 | 4067 | ;; Create subplot layout |
|
0 commit comments