(Plots.jl) linked (aligned) x-axis figures with outer legends

FWIW, for the github example linked above, the following workaround produces an equivalent plot, with “outer” legends and axis aligned:


using Printf, StatsPlots; gr()

d16mer = Dict("Making BioSeq"=> 0.719,
    "Iterating kmers"=> 0.104,
    "Checking sym kmer"=> 0.063,
    "Reading line"=> 0.06,
    "Creating kmer iterator"=> 0.037
    )

dcpg = Dict("Making BioSeq"=> 0.695,
    "Reading line"=> 0.06,
    "GpC detection"=> 0.046,
    "Length of seq"=> 0.032,
    "Indexing seq"=> 0.038
    )

function bp(dict; kw...)
    dict["Misc"] = 1 .- sum(values(dict))
    vals = reverse(collect(values(dict)))'
    labs = permutedims(reverse(collect(keys(dict))))
    groupedbar(vals, label = labs, bar_position = :stack, orientation=:horizontal,yticks = []; kw...)
end

x = LinRange(0,1,5)
xstr = [@sprintf("%.2f", xi) for xi in x]
a = bp(d16mer)
plot!(x, 0*x .+ ylims(a)[1], lc=:black,label=false,widen=false)
xticks!(x, xstr)
b = bp(dcpg)
plot!(x, 0*x .+ ylims(b)[1], lc=:black,label=false,widen=false)
xticks!(x, xstr)
plot(a,b, link=:x,layout=(2,1),xlims=(0,1.7),legend=:topright,showaxis=false)

Plots_gr_linked_vertical_sublplots_outerlegends

2 Likes