# Compositing

## Facets

It is easy to make multiple plots that all share a common dataset and axis.

using Gadfly, RDatasets
iris = dataset("datasets", "iris")
plot(iris, xgroup="Species", x="SepalLength", y="SepalWidth",
Geom.subplot_grid(Geom.point))

Geom.subplot_grid can similarly arrange plots vertically, or even in a 2D grid if there are two shared axes.

Subplots have some inner and outer elements, including Guides and Scales. For example, place the guide inside Geom.subplot_grid(...) to change the subplot labels, or outside to change the outer plot labels.

haireye = dataset("datasets", "HairEyeColor")
palette = ["brown", "blue", "tan", "green"]

plot(haireye, y=:Sex, x=:Freq, color=:Eye, ygroup=:Hair,
Geom.subplot_grid(Geom.bar(position=:stack, orientation=:horizontal),
Guide.ylabel(orientation=:vertical) ),
Scale.color_discrete_manual(palette...),
Guide.colorkey(title="Eye\ncolor"),
Guide.ylabel("Hair color"), Guide.xlabel("Frequency") )

More examples can be found in the plot gallery at Geom.subplot_grid and Scale.{x,y}group.

## Stacks

To composite plots derived from different datasets, or the same data but different axes, a declarative interface is used. The Tutorial showed how such disparate plots can be horizontally arranged with hstack. Here we illustrate how to vertically stack them with vstack or arrange them in a grid with gridstack. These commands allow more customization in regards to tick marks, axis labeling, and other plot details than is available with Geom.subplot_grid.

theme1 = Theme(key_position=:none)
fig1a = plot(iris, x=:SepalLength, y=:SepalWidth, color=:Species, theme1,
alpha=[0.6], size=:PetalLength, Scale.size_area(maxvalue=7))
fig1b = plot(iris, x=:SepalLength, color=:Species, Geom.density,
Guide.ylabel("density"), Coord.cartesian(xmin=4, xmax=8), theme1)
vstack(fig1a,fig1b)