Two y-axes in AoG/Makie?

How can create a plot with two y-axes using AoG / Makie? I want to have two “marks” / geometries with different y-axes. Here is an example using VegaLite.jl.

plt =
    @vlplot(
        data = data0,
            mark = {
                :errorband,
                cornerRadiusTopLeft = 3,
                cornerRadiusTopRight = 3,
                color = "#004c97",
        x = {
            "Year:o",
            axis = {
                title = "Year",
                labelAlign = "center",
                labelAngle = 0,
                titleFontSize = 30,
                labelFontSize = 20,
                dx = 200,
                }},
        height = 800,
        width = 800,
        resolve = {scale = {y = "independent"},
           },
        y = {
            "investment",
            axis = {
                title = "Current Dollars (\$B)",
                titleFontSize = 30,
                labelFontSize = 20,
                titlePadding = 10,
                }},
	    y2 = {"ub"},
        ) +
    @vlplot(
        data = data1,
        mark = {
            :bar,
            cornerRadiusTopLeft = 3,
            cornerRadiusTopRight = 3,
            fillOpacity = 0.25,
            color = "#d86018",
            },
        y = {
            "value",
            axis = {
                title = "Growth Rate (%)",
                titleFontSize = 30,
                labelFontSize = 20,
                titlePadding = 10,
                },
            stack = nothing,
            color = :variable,
            },
    )

Dual axes are generally opposed by the grammar of graphics philosophy because they break the one-to-one correspondence between visual space and data space.

Further, dual axis plots seem to relate quantities that aren’t related: one of the most salient features of a plot is where two elements intersect visually, but in dual-axis plots there is no meaningful substantive interpretation of this feature.

It is considered better to have stacked plots sharing an x axis.

See the section on twin axes in the Makie axis documentation, which contains this example:

using CairoMakie

f = Figure()

ax1 = Axis(f[1, 1], yticklabelcolor = :blue)
ax2 = Axis(f[1, 1], yticklabelcolor = :red, yaxisposition = :right)
hidespines!(ax2)
hidexdecorations!(ax2)

lines!(ax1, 0..10, sin, color = :blue)
lines!(ax2, 0..10, x -> 100 * cos(x), color = :red)

f
2 Likes

@jzr, one notable exception to your good advice is when we want to have dual axis for one data series displaying different scale units (e.g.: km/h and mph)

2 Likes