Most interaction with Gadfly is through the plot function. Plots are described by binding data to aesthetics, and specifying a number of elements including Scales, Coordinates, Guides, and Geometries. Aesthetics are a set of special named variables that are mapped to a geometry. How this mapping occurs is defined by the elements.
This "grammar of graphics" approach tries to avoid arcane incantations and special cases, instead approaching the problem as if one were drawing a wiring diagram: data is connected to aesthetics, which act as input leads, and elements, each self-contained with well-defined inputs and outputs, are connected and combined to produce the desired result.
Along with the standard plot methods operating on DataFrames and Arrays described in the Tutorial, Gadfly has some special signatures to make plotting functions and expressions more convenient.
Gadfly is designed to plot data in so-called "long form", in which data that is of the same type, or measuring the same quantity, are stored in a single column, and any factors or groups are specified by additional columns. This is how data is typically stored in a database.
Sometimes data tables are organized by grouping values of the same type into multiple columns, with a column name used to distinguish the grouping. We refer to this as "wide form" data.
To illustrate the difference consider some historical London birth rate data.
This table is wide form because "Males" and "Females" are two columns both measuring number of births. Wide form data can always be transformed to long form (e.g. with the stack function in DataFrames) but this can be inconvenient, especially if the data is not already in a DataFrame.
stack(births, [:Males, :Females])
Row
variable
value
Year
1
Males
5218
1629
2
Males
4858
1630
3
Males
4422
1631
...
...
...
...
162
Females
7623
1708
163
Females
7380
1709
164
Females
7288
1710
The resulting table is long form with number of births in one column, here with the default name given by stack: "value". Data in this form can be plotted very conveniently with Gadfly.
In some cases, explicitly transforming the data can be burdensome. Gadfly lets you avoid this by referring to columns or groups of columns in an implicit long-form version of the data.
┌ Warning: Indexing with colon as row will create a copy in the future. Use `df[col_inds]` to get the columns without copying
│ caller = evalmapping(::Gadfly.MeltedData{DataFrames.DataFrame}, ::Symbol) at mapping.jl:196
└ @ Gadfly ~/build/GiovineItalia/Gadfly.jl/src/mapping.jl:196
Here Col.value produces the concatenated values from a set of columns, and Col.index refers to a vector labeling each value in that concatenation by the column it came from. Also useful is Row.index, which will give the row index of items in a concatenation.
This syntax also lets us more conveniently plot data that is not in a DataFrame, such as matrices or arrays of arrays. Below we recreate the plot above for a third time after first converting the DataFrame to an Array.