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.
Let us use Fisher's iris dataset as a starting point.
using Gadfly, RDatasets iris = dataset("datasets", "iris")
When used with a DataFrame, the
plot function in Gadfly is of the form:
plot(data::AbstractDataFrame, elements::Element...; mapping...)
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
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
img = SVG("iris_plot.svg", 14cm, 8cm) draw(img, p)
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)
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.
function get_to_it(d) ppoint = plot(d, x=:SepalLength, y=:SepalWidth, Geom.point) pline = plot(d, x=:SepalLength, y=:SepalWidth, Geom.line) ppoint, pline end ps = get_to_it(iris) map(display, ps)
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.
plot(iris, x=:SepalLength, y=:SepalWidth, Geom.point, Geom.line)
This is the grammar of graphics equivalent of "colorless green ideas sleep furiously". It is valid grammar, but not particularly meaningful.
If by chance your data are stored in Arrays instead of a DataFrame, fear not, identical plots can be created using an alternate
Here, the keyword arguments directly supply the data to be plotted, instead of using them to indicate which columns of a DataFrame to use.
SepalLength = iris.SepalLength SepalWidth = iris.SepalWidth plot(x=SepalLength, y=SepalWidth, Geom.point, Guide.xlabel("SepalLength"), Guide.ylabel("SepalWidth"))
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.
"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
["Group labels"] are added to the key.
y1 = [0.1, 0.26, NaN, 0.5, 0.4, NaN, 0.48, 0.58, 0.83] plot(x=1:9, y=y1, Geom.line, Geom.point, color=["Item 1"], linestyle=[:dash], size=[3pt], layer(x=1:10, y=rand(10), Geom.line, Geom.point, color=["Item 2"], size=[5pt], shape=[Shape.square]), layer(x=1:10, y=rand(10), color=[colorant"hotpink"], linestyle=[[8pt, 3pt, 2pt, 3pt]], Geom.line))
All aesthetics have a Scale e.g.
Scale.x_continuous() and some have a Guide e.g.
Guide.xticks(). Scales can be continuous or discrete. Some Scales also have a corresponding palette in
Scale.x_continuous(format= , minvalue= , maxvalue= )
format can be:
Continuous scales can be transformed. In the next plot, the large animals are ruining things for us. Putting both axes on a log-scale clears things up.
set_default_plot_size(21cm ,8cm) mammals = dataset("MASS", "mammals") p1 = plot(mammals, x=:Body, y=:Brain, label=:Mammal, Geom.point, Geom.label) p2 = plot(mammals, x=:Body, y=:Brain, label=:Mammal, Geom.point, Geom.label, Scale.x_log10, Scale.y_log10) hstack(p1, p2)