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.
plot(f::Function, lower, upper, elements...; mapping...)
plot(fs::Vector{T}, lower, upper, elements...; mapping...) where T <: Base.Callable
plot(f::Function, xmin, xmax, ymin, ymax, elements...; mapping...)
spy(M::AbstractMatrix, elements...; mapping...) -> Plot
For example:
p1 = plot([sin,cos], 0, 2pi)
p2 = plot((x,y)->sin(x)+cos(y), 0, 2pi, 0, 2pi)
p3 = spy(ones(33)*sin.(0:(pi/16):2pi)' + cos.(0:(pi/16):2pi)*ones(33)')
hstack(p1,p2,p3)
x
-50
-40
-30
-20
-10
0
10
20
30
40
50
60
70
80
90
-35
-30
-25
-20
-15
-10
-5
0
5
10
15
20
25
30
35
40
45
50
55
60
65
70
-50
0
50
100
-34
-32
-30
-28
-26
-24
-22
-20
-18
-16
-14
-12
-10
-8
-6
-4
-2
0
2
4
6
8
10
12
14
16
18
20
22
24
26
28
30
32
34
36
38
40
42
44
46
48
50
52
54
56
58
60
62
64
66
68
-2
-1
0
1
2
Color
h,j,k,l,arrows,drag to pan
i,o,+,-,scroll,shift-drag to zoom
r,dbl-click to reset
c for coordinates
? for help
?
-50
-40
-30
-20
-10
0
10
20
30
40
50
60
70
80
90
-35
-30
-25
-20
-15
-10
-5
0
5
10
15
20
25
30
35
40
45
50
55
60
65
70
-50
0
50
100
-34
-32
-30
-28
-26
-24
-22
-20
-18
-16
-14
-12
-10
-8
-6
-4
-2
0
2
4
6
8
10
12
14
16
18
20
22
24
26
28
30
32
34
36
38
40
42
44
46
48
50
52
54
56
58
60
62
64
66
68
y
x
-10
-8
-6
-4
-2
0
2
4
6
8
10
12
14
16
18
-8.0
-7.5
-7.0
-6.5
-6.0
-5.5
-5.0
-4.5
-4.0
-3.5
-3.0
-2.5
-2.0
-1.5
-1.0
-0.5
0.0
0.5
1.0
1.5
2.0
2.5
3.0
3.5
4.0
4.5
5.0
5.5
6.0
6.5
7.0
7.5
8.0
8.5
9.0
9.5
10.0
10.5
11.0
11.5
12.0
12.5
13.0
13.5
14.0
14.5
15.0
15.5
16.0
-10
0
10
20
-8.0
-7.5
-7.0
-6.5
-6.0
-5.5
-5.0
-4.5
-4.0
-3.5
-3.0
-2.5
-2.0
-1.5
-1.0
-0.5
0.0
0.5
1.0
1.5
2.0
2.5
3.0
3.5
4.0
4.5
5.0
5.5
6.0
6.5
7.0
7.5
8.0
8.5
9.0
9.5
10.0
10.5
11.0
11.5
12.0
12.5
13.0
13.5
14.0
14.5
15.0
15.5
16.0
-2
-1
0
1
2
f(x,y)
h,j,k,l,arrows,drag to pan
i,o,+,-,scroll,shift-drag to zoom
r,dbl-click to reset
c for coordinates
? for help
?
-10
-8
-6
-4
-2
0
2
4
6
8
10
12
14
16
18
-8.0
-7.5
-7.0
-6.5
-6.0
-5.5
-5.0
-4.5
-4.0
-3.5
-3.0
-2.5
-2.0
-1.5
-1.0
-0.5
0.0
0.5
1.0
1.5
2.0
2.5
3.0
3.5
4.0
4.5
5.0
5.5
6.0
6.5
7.0
7.5
8.0
8.5
9.0
9.5
10.0
10.5
11.0
11.5
12.0
12.5
13.0
13.5
14.0
14.5
15.0
15.5
16.0
-10
0
10
20
-8.0
-7.5
-7.0
-6.5
-6.0
-5.5
-5.0
-4.5
-4.0
-3.5
-3.0
-2.5
-2.0
-1.5
-1.0
-0.5
0.0
0.5
1.0
1.5
2.0
2.5
3.0
3.5
4.0
4.5
5.0
5.5
6.0
6.5
7.0
7.5
8.0
8.5
9.0
9.5
10.0
10.5
11.0
11.5
12.0
12.5
13.0
13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2.2
2.3
2.4
2.5
2.6
2.7
2.8
2.9
3.0
f(x)
Another feature is that a plot can be added to incrementally, using push!
.
p = plot()
push!(p, layer(x=[2,4], y=[2,4], size=[1.4142], color=[colorant"gold"]))
push!(p, Coord.cartesian(fixed=true))
push!(p, Guide.title("My Awesome Plot"))
x
-7
-6
-5
-4
-3
-2
-1
0
1
2
3
4
5
6
7
8
9
10
11
12
13
-6.0
-5.5
-5.0
-4.5
-4.0
-3.5
-3.0
-2.5
-2.0
-1.5
-1.0
-0.5
0.0
0.5
1.0
1.5
2.0
2.5
3.0
3.5
4.0
4.5
5.0
5.5
6.0
6.5
7.0
7.5
8.0
8.5
9.0
9.5
10.0
10.5
11.0
11.5
12.0
-10
0
10
20
-6.0
-5.8
-5.6
-5.4
-5.2
-5.0
-4.8
-4.6
-4.4
-4.2
-4.0
-3.8
-3.6
-3.4
-3.2
-3.0
-2.8
-2.6
-2.4
-2.2
-2.0
-1.8
-1.6
-1.4
-1.2
-1.0
-0.8
-0.6
-0.4
-0.2
0.0
0.2
0.4
0.6
0.8
1.0
1.2
1.4
1.6
1.8
2.0
2.2
2.4
2.6
2.8
3.0
3.2
3.4
3.6
3.8
4.0
4.2
4.4
4.6
4.8
5.0
5.2
5.4
5.6
5.8
6.0
6.2
6.4
6.6
6.8
7.0
7.2
7.4
7.6
7.8
8.0
8.2
8.4
8.6
8.8
9.0
9.2
9.4
9.6
9.8
10.0
10.2
10.4
10.6
10.8
11.0
11.2
11.4
11.6
11.8
12.0
h,j,k,l,arrows,drag to pan
i,o,+,-,scroll,shift-drag to zoom
r,dbl-click to reset
c for coordinates
? for help
?
-7
-6
-5
-4
-3
-2
-1
0
1
2
3
4
5
6
7
8
9
10
11
12
13
-6.0
-5.5
-5.0
-4.5
-4.0
-3.5
-3.0
-2.5
-2.0
-1.5
-1.0
-0.5
0.0
0.5
1.0
1.5
2.0
2.5
3.0
3.5
4.0
4.5
5.0
5.5
6.0
6.5
7.0
7.5
8.0
8.5
9.0
9.5
10.0
10.5
11.0
11.5
12.0
-10
0
10
20
-6.0
-5.8
-5.6
-5.4
-5.2
-5.0
-4.8
-4.6
-4.4
-4.2
-4.0
-3.8
-3.6
-3.4
-3.2
-3.0
-2.8
-2.6
-2.4
-2.2
-2.0
-1.8
-1.6
-1.4
-1.2
-1.0
-0.8
-0.6
-0.4
-0.2
0.0
0.2
0.4
0.6
0.8
1.0
1.2
1.4
1.6
1.8
2.0
2.2
2.4
2.6
2.8
3.0
3.2
3.4
3.6
3.8
4.0
4.2
4.4
4.6
4.8
5.0
5.2
5.4
5.6
5.8
6.0
6.2
6.4
6.6
6.8
7.0
7.2
7.4
7.6
7.8
8.0
8.2
8.4
8.6
8.8
9.0
9.2
9.4
9.6
9.8
10.0
10.2
10.4
10.6
10.8
11.0
11.2
11.4
11.6
11.8
12.0
y
My Awesome Plot
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.
births = RDatasets.dataset("HistData", "Arbuthnot")[:,[:Year, :Males, :Females]]
Row Year Males Females 1 1629 5218 4683 2 1630 4858 4457 3 1631 4422 4102 4 1632 4994 4590 5 1633 5158 4839 6 1634 5035 4820 ... ... ... ...
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.
plot(stack(births, [:Males, :Females]), x=:Year, y=:value, color=:variable,
Geom.line)
Year
1500
1525
1550
1575
1600
1625
1650
1675
1700
1725
1750
1775
1800
1825
1850
1520
1530
1540
1550
1560
1570
1580
1590
1600
1610
1620
1630
1640
1650
1660
1670
1680
1690
1700
1710
1720
1730
1740
1750
1760
1770
1780
1790
1800
1810
1820
1830
1500
1600
1700
1800
1900
1525
1530
1535
1540
1545
1550
1555
1560
1565
1570
1575
1580
1585
1590
1595
1600
1605
1610
1615
1620
1625
1630
1635
1640
1645
1650
1655
1660
1665
1670
1675
1680
1685
1690
1695
1700
1705
1710
1715
1720
1725
1730
1735
1740
1745
1750
1755
1760
1765
1770
1775
1780
1785
1790
1795
1800
1805
1810
1815
1820
1825
Males
Females
variable
h,j,k,l,arrows,drag to pan
i,o,+,-,scroll,shift-drag to zoom
r,dbl-click to reset
c for coordinates
? for help
?
-1.50×10⁴
-1.00×10⁴
-5.00×10³
0
5.00×10³
1.00×10⁴
1.50×10⁴
2.00×10⁴
2.50×10⁴
-1.00×10⁴
-9.00×10³
-8.00×10³
-7.00×10³
-6.00×10³
-5.00×10³
-4.00×10³
-3.00×10³
-2.00×10³
-1.00×10³
0
1.00×10³
2.00×10³
3.00×10³
4.00×10³
5.00×10³
6.00×10³
7.00×10³
8.00×10³
9.00×10³
1.00×10⁴
1.10×10⁴
1.20×10⁴
1.30×10⁴
1.40×10⁴
1.50×10⁴
1.60×10⁴
1.70×10⁴
1.80×10⁴
1.90×10⁴
2.00×10⁴
-1.0×10⁴
0
1.0×10⁴
2.0×10⁴
-1.000×10⁴
-9.500×10³
-9.000×10³
-8.500×10³
-8.000×10³
-7.500×10³
-7.000×10³
-6.500×10³
-6.000×10³
-5.500×10³
-5.000×10³
-4.500×10³
-4.000×10³
-3.500×10³
-3.000×10³
-2.500×10³
-2.000×10³
-1.500×10³
-1.000×10³
-5.000×10²
0
5.000×10²
1.000×10³
1.500×10³
2.000×10³
2.500×10³
3.000×10³
3.500×10³
4.000×10³
4.500×10³
5.000×10³
5.500×10³
6.000×10³
6.500×10³
7.000×10³
7.500×10³
8.000×10³
8.500×10³
9.000×10³
9.500×10³
1.000×10⁴
1.050×10⁴
1.100×10⁴
1.150×10⁴
1.200×10⁴
1.250×10⁴
1.300×10⁴
1.350×10⁴
1.400×10⁴
1.450×10⁴
1.500×10⁴
1.550×10⁴
1.600×10⁴
1.650×10⁴
1.700×10⁴
1.750×10⁴
1.800×10⁴
1.850×10⁴
1.900×10⁴
1.950×10⁴
2.000×10⁴
value