Gadfly is an implementation of a "grammar of graphics" style statistical graphics system for Julia. This tutorial will outline general usage patterns and will give you a feel for the overall system.
To begin, we need some data. Gadfly can work with data supplied as either a DataFrame or as plain AbstractArrays. In this tutorial, we'll pick and choose some examples from the RDatasets package.
Let us use Fisher's iris dataset as a starting point.
using Gadfly, RDatasets
iris = dataset("datasets", "iris")
The first argument is the data to be plotted and the keyword arguments at the end map "aesthetics" to columns in the data frame. All input arguments between data and mapping are some number of "elements", which are the nouns and verbs, so to speak, that form the grammar.
Let's get to it.
p = plot(iris, x=:SepalLength, y=:SepalWidth, Geom.point);
First note that we've taken advantage of the flexibility of Julia's handling of function signatures and put the keyword arguments in the midst of the positional arguments. This is purely for ease of reading.
The example above produces a Plot object. It can be saved to a file by drawing to one or more backends using draw.
Now we have the following charming little SVG image.
If some of the text in this image is overlapping other text, your browser likely has a minimum font size set. You will need to unset this option for the plots to render correctly in your web browser.
If you are working at the REPL, a quicker way to see the image is to omit the semi-colon trailing plot. This automatically renders the image to your default multimedia display, typically an internet browser. No need to capture the output argument in this case.
plot(iris, x=:SepalLength, y=:SepalWidth)
Note that Geom.point will be automatically supplied if no other geometries are given.
Alternatively one can manually call display on a Plot object. This workflow is necessary when display would not otherwise be called automatically.
For the rest of the demonstrations, we'll simply omit the trailing semi-colon for brevity.
In this plot we've mapped the x aesthetic to the SepalLength column and the y aesthetic to the SepalWidth. The last argument, Geom.point, is a geometry element which takes bound aesthetics and renders delightful figures. Adding other geometries produces layers, which may or may not result in a coherent plot.
Note that with the Array interface, extra elements must be included to specify the axis labels, whereas with a DataFrame they default to the column names.
Let's do add something meaningful by mapping the color aesthetic.
plot(iris, x=:SepalLength, y=:SepalWidth, color=:Species, Geom.point);
# or equivalently for Arrays:
Color = iris.Species
plot(x=SepalLength, y=SepalWidth, color=Color, Geom.point,
Guide.xlabel("SepalLength"), Guide.ylabel("SepalWidth"),
Guide.colorkey(title="Species"))
Ah, a scientific discovery: Setosa has short but wide sepals!
Color scales in Gadfly by default are produced from perceptually uniform colorspaces (LUV/LCHuv or LAB/LCHab), though it supports RGB, HSV, HLS, XYZ, and converts arbitrarily between these. Color values can also be specified by most names common to CSS or X11, e.g. "oldlace" or "aliceblue". The full list of valid color names is defined in the Colors.jl library.
Color, and other aesthetics, can also be mapped by using arrays with group labels or functional types e.g. ["group label"] or [colorant"red"]. ["Group labels"] are added to the key.