Author Archives: liebke

Dynamic charts with Incanter

I had the opportunity to attend last week’s PragmaticStudio Clojure workshop taught by Rich Hickey and Stuart Halloway (I highly recommend it, and there are still seats open for the May class). During the three days I talked with Rich about features he’d like to see in Incanter, and the first thing he asked about was adding dynamic charts, like are available in Mathematica using the Manipulate function. So I ended up spending much of my lab time working on this feature, the first draft of which is now available.

Incanter has three new macros, sliders, dynamic-xy-plot, and dynamic-scatter-plot. The sliders macro can bind multiple named sequences to an equal number of JSlider widgets. When a slider is manipulated a user defined expression is evaluated. For instance, the following code will display two slider widgets bound to two sequences, x and y.

(sliders [x (range -3 3 0.01)
          y (range 0.01 10 0.1)]
  (foo x y))

Each time one of the sliders is manipulated the expression (foo x y) will be evaluated with the new value of either x or y. Presumably, foo has side effects, like changing the value of a ref or manipulating a GUI widget, since it is running in the separate thread used by the slider widget.

I then combined this macro with incanter.charts/set-data function to create dynamic versions of xy-plot and scatter-plot, named appropriately dynamic-xy-plot and dynamic-scatter-plot respectively.

The following example creates an xy-plot of a sequence of values named x versus the normal PDF of x, and displays two sliders bound to the mean and standard deviation of the PDF.

(let [x (range -3 3 0.1)]
  (view (dynamic-xy-plot [mean (range -3 3 0.1)
                          std-dev (range 0.1 10 0.1)]
          [x (pdf-normal x :mean mean :sd std-dev)])))

The expression provided to dynamic-xy-plot must produce a sequence containing either two sequences with N values, or N sequences with two values each. In other words, a N-by-2 matrix or a 2-by-N matrix, where N is the number of points plotted. The expression above,

[x (pdf-normal x :mean mean :sd std-dev)]

produces a vector containing a sequence called x and the sequence produced by calling pdf-normal on x (equivalent to a N-by-2 matrix).

Manipulating the sliders will change the shape and position of the curve.

The dynamic-scatter-plot macro works the same way as dynamic-xy-plot. All three macros are available in the version of Incanter on Github and in

Incanter blog post roundup

There have been several cool blog posts over the last few weeks featuring Incanter that I would like to highlight here.

Incanter code repository

It’s been a long time coming, but Incanter is available in a public code repository ( once again.

The version available in the Clojars repository has grown increasingly out of date, but couldn’t be updated due to Incanter’s new modular build structure; a structure that lets developers include only the subset of Incanter functionality that they need.

For instance, if you only need the functionality found in incanter.core and incanter.stats, then include the incanter-core dependency in your project.clj file. If you need charts but, not Processing visualizations, include incanter-charts. If you want access to the incanter.chrono library, but nothing else, use the incanter-chrono dependency.

Instructions for building Leiningen-based projects using the repository are available on the Incanter repo’s homepage,

Data Sorcery with Clojure & Incanter: Introduction to Datasets & Charts

I put together my slides (pdf) for next week’s National Capital Area Clojure Users Group February Meetup. Being snow-bound this week, I’ve been able to make more slides than I’ll have time to cover during next week’s session, so I’ll be skimming over some of the examples.

Russ Olsen will start the session with an introduction to Clojure, so if you’re in the D.C. area next Thursday (February 18), sign-up for the meetup.

The code used in this presentation is available here, and a more printer-friendly version of the presentation itself, with a white background, is available here.

Who is Incanter?

In preparation for a talk I’m giving next week at the CapClug (I plan to post the slides in the next couple days) I finally put together a list of people that have help build Incanter over the last year.

Committers: This group has a new member today, Sean Devlin. He’s working on incanter.chrono 2.0, making him the project’s Time Lord.

  • David Edgar Liebke
  • Bradford Cross (FlightCaster)
  • Alex Ott (build master)
  • Tom Faulhaber (autodoc)
  • Sean Devlin (chrono 2.0)
  • Jared Strate (FlightCaster)

Contributors: This group includes those that have patched, optimized, and improved Incanter.

  • Chris Burroughs
  • Phil Hagelberg
  • Edmund Jackson (proposed time-series)
  • Steve Jenson
  • Steve Purcell
  • Brendan Ribera
  • Alexander Stoddard

Community: This group includes those that through questions, suggestions, and answers have improved Incanter.

Related Project: This is the first public project built on Incanter, and a great one at that.

Thanks everyone for helping make Incanter so much fun to work on!


Dark theme for Incanter charts

JFreeChart has been a fantastic library, I’ve been able to include useful charting functionality in Incanter very quickly because of it, but I’m not a big fan of its default visual theme. Eventually I’d like to create some new themes, or better yet include themes created by others, but in the meantime I have created the set-theme function, which accepts a chart and either a keyword indicating a built-in theme or a JFreeChart ChartTheme object, and applies the theme to the chart.

At the moment, the only built-in themes are :default and :dark, but hopefully that will change in the future.

Here’s an example of using set-theme. First I’ll create a chart with the default theme,

(use '(incanter core charts datasets))

(with-data (get-dataset :iris)
  (view (scatter-plot :Sepal.Length :Sepal.Width :group-by :Species)))

and here’s the same scatter-plot with the dark theme.

(with-data (get-dataset :iris)
  (doto (scatter-plot :Sepal.Length :Sepal.Width :group-by :Species)
    (set-theme :dark)

The set-theme function is available in the latest version Incanter @ Github.

I have also added the incanter-pdf module discussed in the previous blog post, but it isn’t installed by default. To install it in your local Maven repository, run ‘mvn install’ from the incanter/modules/incanter-pdf directory.

Saving Incanter charts as PDF documents

Incanter charts can be saved as PNG files using the save function, but I had a request earlier today to add the ability to save them as PDF documents.

So I’ve created a new function called save-pdf in a new package called incanter.pdf.

Here’s a basic example.

(use '(incanter core charts pdf))
(save-pdf (function-plot sin -4 4) "./pdf-chart.pdf")

Which outputs the following PDF file.

Working with R from Clojure and Incanter

Joel Boehland has introduced Rincanter, which lets you use R from Clojure and Incanter. This is fantastically cool, as it opens up the vast number of R libraries to Clojure/Incanter, translating between R and Clojure data types, including Incanter datasets.

Check out Joel’s latest blog post, All your datasets R belong to us (I love that name), where he introduces Rincanter and demonstrates its use.

New name for blog: Data Sorcery with Clojure

I Just renamed the blog; previously it was called “Data Analysis and Visualization with Clojure,” and now it is simply called “Data Sorcery with Clojure.” I’ve also changed the default URL from to (the former URL now redirects to the new one).

Why rename it? Because “data sorcery” fits the theme of the name Incanter, it captures the perception people have of both statistics and lisp languages as dark arts, and it broadens the scope from just data analysis and visualization to include all the other machinations necessary when making sense of data.

Incanter Data Sorcery t-shirt

I’ve created an Incanter Data Sorcery t-shirt for Clojure-loving data geeks. I’ve made the design available on a basic black shirt and a higher quality one (more expensive) through Zazzle (where you can also find Clojure t-shirts). Proceeds offset the cost of the coffee used to fuel development of Incanter.

Here’s a close up of the design.

Other shirt color options are also available at the Incanter Zazzle store. If you have other design or tag-line suggestions, let me know.