clojuress.v1.inside-ggplot-test

clojuress.v1.inside-ggplot-test - created by notespace, Thu Jan 23 19:02:45 IST 2020.

Some R libraries have beautiful internal representation of their notions as data, that allows for great composability. One example for that is ggplot2

In the R culture, internal data representations are commonly hidden from the user. In Clojure, we often like to see everything as plain data.

What happens when we convert R objects generated by ggplot2 into plain Clojure data?

(require '[clojuress.v1.r :as r :refer
           [r eval-r->java r->java java->r java->clj java->naive-clj clj->java
            r->clj clj->r ->code r+ colon function]]
         '[clojuress.v1.require :refer [require-r]]
         '[clojuress.v1.robject :as robject]
         '[clojuress.v1.session :as session]
         '[tech.ml.dataset :as dataset]
         '[notespace.v1.util :refer [check]])

(require-r '[graphics :refer [plot]])
(require-r '[ggplot2 :refer [ggplot aes geom_point xlab ylab labs]])
(require '[clojuress.v1.applications.plotting :refer
           [plotting-function->svg ggplot->svg]])

We will use the java->naive-clj function, that allows one to convert the Java representation layer to plain Clojure data, exposing the metadata, etc.

(ggplot->svg (let [x (repeatedly 99 rand)
                   y (map + x (repeatedly 99 rand))]
               (-> {:x x, :y y}
                   dataset/name-values-seq->dataset
                   (ggplot (aes :x x :y y :color '(+ x y) :size '(/ x y)))
                   (r+ (geom_point) (xlab "x") (ylab "y")))))

0.5 1.0 1.5 0.00 0.25 0.50 0.75 1.00 x y (x/y) 0.25 0.50 0.75 1 2 (x + y)


clojuress.v1.inside-ggplot-test - created by notespace, Thu Jan 23 19:02:45 IST 2020.