cljplot2

cljplot2

cljplot2 is a Clojure plotting library rendering static images with clojure2d. A plot is a plain map: data (a sequence of maps), a mapping from data to dimensions, a vector of charts, labels, scales and a theme. There is no DSL and no grammar of graphics: you build and transform the map with ordinary Clojure. Statistics come from fastmath, scales from wadogo.

(ns book.index
  (:require [cljplot2.chart :as chart]
            [cljplot2.core :as plot]
            [notebook-utils :as nu]))

How this book is organised

  • Reference - every option of the library in tables generated from its schemas, in four parts:

    • concepts - rendering, data and mapping, aesthetics and legends, scales and coordinates, layering, labels, facets and multiplots, the theme, shared option groups, errors and limits;

    • charts - every chart kind: its mapping, its options and examples;

    • builders - graphs, trees and series built into plots;

    • data functions - levels, graphs and trees, sets and series, fields, and specific cases.

  • Use cases - one chapter per family of the R Graph Gallery merged with From Data to Viz: distribution, correlation, ranking, part of a whole, evolution, flow, and colours. Each use case has one canonical example here.

  • Galleries - figures replicated from other sources: cljplot extras, the lattice book, Vega-Lite and Vega galleries. Figures are reproduced by content (same data, same idea), not pixel by pixel, and link to the use case they illustrate.

Every example names its source and its dataset with the dataset’s licence. All code is Clojure written for cljplot2; no code of the original sources is copied. The datasets are in notebooks/data/ (listed in datasets.edn), not in the library.

How to read an example

Each example is the code followed by the image it renders. Examples use these aliases: cljplot2.core as plot (plot/render-image returns a java.awt.image.BufferedImage, which the notebook shows) and cljplot2.chart as chart. Data is read with nu/dataset.

A first example: Old Faithful

Waiting time between eruptions against eruption duration (left) and the distribution of eruption durations (right). Two plots sharing one dataset, arranged side by side.

Source: R Graph Gallery - Scatter, Density. Data: Old Faithful geyser eruptions, R datasets package faithful (Härdle 1991; Azzalini & Bowman 1990); licence: Part of R (GPL-2 | GPL-3).

(def faithful (nu/dataset :faithful))
eruptions waiting
3.6 79
1.8 54
3.333 74
2.283 62
4.533 85
2.883 55
4.7 88
3.6 85
1.95 51
4.35 85
1.833 54
3.917 84
4.2 78
1.75 47
4.7 83
2.167 52
1.75 62
4.8 84
1.6 52
4.25 79
1.8 51
1.75 47
3.45 78
3.067 69
4.533 74
3.6 83
1.967 55
4.083 76
3.85 78
4.433 79
4.3 73
4.467 77
3.367 66
4.033 80
3.833 74
2.017 52
1.867 48
4.833 80
1.833 59
4.783 90
4.35 80
1.883 58
4.567 84
1.75 58
4.533 73
3.317 83
3.833 64
2.1 53
4.633 82
2 59
4.8 75
4.716 90
1.833 54
4.833 80
1.733 54
4.883 83
3.717 71
1.667 64
4.567 77
4.317 81
2.233 59
4.5 84
1.75 48
4.8 82
1.817 60
4.4 92
4.167 78
4.7 78
2.067 65
4.7 73
4.033 82
1.967 56
4.5 79
4 71
1.983 62
5.067 76
2.017 60
4.567 78
3.883 76
3.6 83
4.133 75
4.333 82
4.1 70
2.633 65
4.067 73
4.933 88
3.95 76
4.517 80
2.167 48
4 86
2.2 60
4.333 90
1.867 50
4.817 78
1.833 63
4.3 72
4.667 84
3.75 75
1.867 51
4.9 82
… …
(plot/render-image
 (plot/arrange [{:data faithful
                 :mapping {:x :eruptions :y :waiting}
                 :charts [(chart/scatter)]
                 :labels {:x "eruptions [min]" :y "waiting [min]"}}
                {:data faithful
                 :mapping {:x :eruptions}
                 :charts [(chart/density)]
                 :labels {:x "eruptions [min]"}}]
               {:columns 2 :title "Old Faithful geyser"})
 {:width 800 :height 400})

source: notebooks/book/index.clj