-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
exploring ggplot - combining-tablecloth-and-hanami - WIP
- Loading branch information
Showing
1 changed file
with
73 additions
and
0 deletions.
There are no files selected for viewing
73 changes: 73 additions & 0 deletions
73
projects/datavis/ggplot/notebooks/combining_tablecloth_and_hanami.clj
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
(ns combining-tablecloth-and-hanami | ||
(:require [scicloj.kindly.v4.kind :as kind] | ||
[scicloj.noj.v1.paths :as paths] | ||
[scicloj.tempfiles.api :as tempfiles] | ||
[tablecloth.api :as tc] | ||
[tech.v3.dataset :as ds] | ||
[aerial.hanami.common :as hc] | ||
[aerial.hanami.templates :as ht] | ||
[scicloj.metamorph.ml.toydata :as toydata])) | ||
|
||
(defn invoke [[f arg]] | ||
(f arg)) | ||
|
||
(defn prepare-data [dataset] | ||
(let [{:keys [path _]} | ||
(tempfiles/tempfile! ".csv")] | ||
(-> dataset | ||
(ds/write! path)) | ||
{:values (slurp path) | ||
:format {:type "csv"}})) | ||
|
||
(defn safe-update [m k f] | ||
(if (m k) | ||
(update m k f) | ||
m)) | ||
|
||
(defn layered-xform [{:keys [template args]}] | ||
(-> template | ||
(safe-update :layer | ||
(partial map (fn [layer] | ||
(-> layer | ||
(update :template (partial merge (dissoc template | ||
:layer | ||
:metamorph/data))) | ||
(update :args (partial merge args)) | ||
layered-xform)))) | ||
(update :metamorph/data prepare-data) | ||
(hc/xform args) | ||
(update-keys #(case % :metamorph/data :data %)) | ||
kind/vega-lite)) | ||
|
||
(def point-chart | ||
(assoc ht/point-chart | ||
:usermeta | ||
{:embedOptions {:renderer :svg}})) | ||
|
||
(defn plot | ||
([dataset template args] | ||
(plot (assoc template | ||
:metamorph/data dataset) | ||
args)) | ||
([template args] | ||
(let [dataset (:metamorph/data template)] | ||
(if (tc/grouped? dataset) | ||
(-> dataset | ||
(tc/aggregate {:plot (fn [group-dataset] | ||
[(plot (assoc template | ||
:metamorph/data group-dataset) | ||
args)])}) | ||
(tc/rename-columns {:plot-0 :plot}) | ||
kind/table) | ||
;; else | ||
(kind/fn [layered-xform | ||
{:template template | ||
:args args}]))))) | ||
|
||
|
||
|
||
|
||
(-> (toydata/iris-ds) | ||
(plot point-chart | ||
{:X :sepal_width | ||
:Y :sepal_length})) |