Skip to content

Commit 5d3bf18

Browse files
committed
aog wip
1 parent 6381c3b commit 5d3bf18

1 file changed

Lines changed: 31 additions & 4 deletions

File tree

src/data_visualization/aog_in_clojure_part1.clj

Lines changed: 31 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,8 @@
99
:tags [:datavis]
1010
:keywords [:datavis]
1111
:toc true}}}
12-
(ns data-visualization.aog-in-clojure-part1)
12+
(ns data-visualization.aog-in-clojure-part1
13+
(:require [tablecloth.api :as tc]))
1314

1415
^{:kindly/hide-code true
1516
:kindly/kind :kind/hiccup}
@@ -3818,11 +3819,21 @@ iris
38183819
(let [;; For faceted plots, remove :data from individual layers
38193820
;; Data goes at TOP level for VL faceting, not inside spec
38203821
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)]
38223833
(cond-> {:$schema "https://vega.github.io/schema/vega-lite/v5.json"
38233834
: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))
38263837
:config ggplot2-config
38273838
:spec {:layer layers-without-data}}
38283839
col-var (assoc :facet {:column {:field (name col-var) :type "nominal"}})
@@ -4035,6 +4046,22 @@ iris
40354046
:line {:color (:grid theme) :width 1}}
40364047
:showlegend false})
40374048

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+
40384065
nil)))
40394066

40404067
;; Create subplot layout

0 commit comments

Comments
 (0)