You can instantiate a Figure and use the mutating draw! function to draw a layer into an existing Figure or Axis. It is preferable to pass a GridPosition, e.g. fig[1, 1], instead of an Axis because draw! can pass Axis attributes, such as axis labels and axis tick labels, to the underlying visualization. If you pass an Axis to draw! these attributes need to be specified again as keyword arguments inside Axis:

    1. # Figure
    2. fig = Figure()
    3. # First Axis
    4. plt_barplot = data(df) *
    5. mapping(
    6. :name,
    7. color=:year,
    8. dodge=:year) *
    9. visual(BarPlot)
    10. subfig1 = draw!(fig[1, 1], plt_barplot)
    11. # Second Axis
    12. plt_custom = data(synthetic_df) *
    13. mapping(:x, :y) *
    14. (
    15. visual(Scatter; color=:steelblue, marker=:cross)
    16. + (
    17. linear() * visual(; color=:red, linestyle=:dot, linewidth=5)
    18. )
    19. )
    20. subfig2 = draw!(fig[2, 1:2], plt_custom)
    21. # Third Axis
    22. mapping(:name, :grade) *
    23. expectation()
    24. subfig3 = draw!(fig[1, 2], plt_expectation)
    25. # Insert the legend
    26. legend!(
    27. fig[end+1, 1:2],
    28. subfig1;
    29. tellheight=true
    30. )
    31. fig

    Here, we are instantiating a Figure called fig. Then, we proceed by creating three Layers that have the prefix plt_. Each one of these are followed by a draw! with the desired fig’s GridPosition as the first position argument followed by the Layer as the second argument. Furthermore, we use AlgebraOfGraphics.jl’s legend! to add the legend to the visualization. They way legend! works is by passing first the desired fig’s GridPosition for the placement of the legend, and the desired legend labels. For the legend’s label, we use the output of the draw! function that was called in the Layers and that has legend labels already, in our case the plt_barplot. All of the Legend/axislegend keyword arguments (Section 6.2) can be used in legend!. Finally, as the last step, we call the Figure, fig, to recover it after it was mutated by all of the mutating “bang” functions, e.g. draw! and , in order to render our visualization.