DataVis in Clojure using R

Short showcase of various charting libraries available in R and accessible with clojisr library.

{:deps
 {org.clojure/clojure {:mvn/version "1.10.1"}
  org.slf4j/slf4j-nop {:mvn/version "1.7.30"}
  scicloj/clojisr {:mvn/version "1.0.0-BETA8"}}}
deps.edn
Extensible Data Notation
echo "maxinbuf 4194304" > /etc/Rserv.conf
0.1s
Bash in Clojure
(require '[clojisr.v1.r :refer [r r+]]
         '[clojisr.v1.require :refer [require-r]]
         '[clojisr.v1.applications.plotting :refer [plot->file]])
0.0s

Setup

Ensure you have installed following packages on R:

  • lattice

  • ggplot2

  • mlmRev

  • RColorBrewer

Graphics

First let's start with basic graphics library.

(require-r '[graphics :as gr])
1.2s

Graphics library renders directly to the device so we need to wrap charting functions in Clojure fn.

(def data (repeatedly 100000 #(+ (rand) (rand) (rand) (rand) (rand))))
(plot->file "/results/graphics.jpg"
            #(gr/hist data :main "Histogram" :col "red" :freq false)
            :quality 90
            :width 800)
1.2s

Lattice

(require-r '[lattice :as lat]
           '[mlmRev])
0.6s

Little helper to glue symbols to string.

(defmacro formula [& frm] `(r ~(apply str frm)))
0.1s
user/formula
(def histogram (-> (formula "~" gcsescore | factor (score))
 								   (lat/histogram :data r.mlmRev/Chem97)))
0.1s
user/histogram
(plot->file "/results/histogram.png" histogram :width 800)
0.7s

ggplot2

(require-r '[ggplot2 :as gg])
0.9s
(plot->file "/results/ggplot.png" 
            (r+ (gg/ggplot gg/diamonds (gg/aes :x 'carat
                                       				 :y 'price))
                (gg/geom_point (gg/aes :color 'cut))
                (gg/geom_smooth)) :width 800)
3.4s

Hexbin

(require-r '[utils :as u])
(u/install-packages "hexbin")
7.8s
(require-r '[hexbin :as h]
           '[RColorBrewer :as brewer]
           '[stats :as stats]
           '[grDevices :as dev]
           '[base :refer [rev]])
3.1s
(def x (stats/rnorm :mean 1.5 5000))
(def y (stats/rnorm :mean 1.6 5000))
(def bin (h/hexbin x y :xbins 40))
(def my-colors (-> (brewer/brewer-pal 11 "Spectral")
                   (rev)
                   (dev/colorRampPalette)))
0.1s
user/my-colors
(plot->file "/results/hexbin.png" #(h/plot bin
                                           :main ""
                                           :colramp my-colors
                                           :xlab "x" :ylab "y") :width 800)
0.7s

CC-BY

Runtimes (1)