Layers and Stacks

Layers and Stacks

Gadfly also supports more advanced plot composition techniques like layering and stacking.

Layers

Draw multiple layers onto the same plot with

plot(layer(x=rand(10), y=rand(10), Geom.point),
     layer(x=rand(10), y=rand(10), Geom.line))
x 0.0 0.5 1.0 0.0 0.5 1.0 y

Or if your data is in a DataFrame:

plot(my_data, layer(x="some_column1", y="some_column2", Geom.point),
              layer(x="some_column3", y="some_column4", Geom.line))

You can also pass different data frames to each layer:

layer(another_dataframe, x="col1", y="col2", Geom.point)

Ordering of layers in the Z direction can be controlled with the order keyword. A higher order number will cause a layer to be drawn on top of any layers with a lower number. If not specified, default order for a layer is 0.

# using stacks (see below)
xs = rand(0:10, 100, 2)
p1 = plot(layer(x=xs[:, 1], color=[colorant"orange"], Geom.histogram),
          layer(x=xs[:, 2], Geom.histogram), Guide.title("Default ordering"))
p2 = plot(layer(x=xs[:, 1], color=[colorant"orange"], Geom.histogram, order=1),
          layer(x=xs[:, 2], Geom.histogram, order=2),
          Guide.title("Manual ordering"))
hstack(p1, p2)
x 0 5 10 0 5 10 15 20 Manual ordering x 0 5 10 0 5 10 15 20 Default ordering

Guide attributes may be added to a multi-layer plots:

plt=plot(layer(x=rand(10), y=rand(10), Geom.point),
         layer(x=rand(10), y=rand(10), Geom.line),
         Guide.xlabel("x label"),
         Guide.ylabel("y label"),
         Guide.title("Title"))
x label 0.0 0.5 1.0 0.0 0.5 1.0 y label Title

Stacks

Plots can also be stacked horizontally with hstack or vertically with vstack, and arranged into a rectangular array with gridstack. This allows more customization in regards to tick marks, axis labeling, and other plot details than is available with Geom.subplot_grid.

p1 = plot(x=[1,2,3], y=[4,5,6]);
p2 = plot(x=[1,2,3], y=[6,7,8]);
hstack(p1,p2)
x 1.0 1.5 2.0 2.5 3.0 6.0 6.5 7.0 7.5 8.0 y x 1.0 1.5 2.0 2.5 3.0 4.0 4.5 5.0 5.5 6.0 y
p3 = plot(x=[5,7,8], y=[8,9,10]);
p4 = plot(x=[5,7,8], y=[10,11,12]);

# these two are equivalent
vstack(hstack(p1,p2),hstack(p3,p4));
gridstack([p1 p2; p3 p4])
x 5 6 7 8 10.0 10.5 11.0 11.5 12.0 y x 5 6 7 8 8.0 8.5 9.0 9.5 10.0 y x 1.0 1.5 2.0 2.5 3.0 6.0 6.5 7.0 7.5 8.0 y x 1.0 1.5 2.0 2.5 3.0 4.0 4.5 5.0 5.5 6.0 y

You can use title to add a descriptive string to the top of a stack

title(hstack(p3,p4), "My great data")
x 5 6 7 8 10.0 10.5 11.0 11.5 12.0 y x 5 6 7 8 8.0 8.5 9.0 9.5 10.0 y My great data

You can also leave panels empty in a stack by passing a Compose.context() object

# empty panel
gridstack(Union{Plot,Compose.Context}[p1 p2; p3 Compose.context()])
x 5 6 7 8 8.0 8.5 9.0 9.5 10.0 y x 1.0 1.5 2.0 2.5 3.0 6.0 6.5 7.0 7.5 8.0 y x 1.0 1.5 2.0 2.5 3.0 4.0 4.5 5.0 5.5 6.0 y