Skip to content

Instantly share code, notes, and snippets.

@Mattriks
Created April 16, 2020 11:57
Show Gist options
  • Save Mattriks/eee7469a10443f4291358dbb22d2cab7 to your computer and use it in GitHub Desktop.
Save Mattriks/eee7469a10443f4291358dbb22d2cab7 to your computer and use it in GitHub Desktop.
Custom guides in Gadfly
module gg
using Compose, Gadfly
PositionedGuide = Guide.PositionedGuide
top_guide_position = Guide.top_guide_position
right_guide_position = Guide.right_guide_position
struct Marginal <: Gadfly.GuideElement
side::String
top::Gadfly.GeometryElement
right::Gadfly.GeometryElement
end
margins(;side="both", top=Geom.histogram(bincount=15),
right=Geom.histogram(orientation=:horizontal, bincount=15)) =
Marginal(side, top, right)
function Guide.render(guide::Marginal, theme::Gadfly.Theme, aes::Gadfly.Aesthetics)
adict = Dict{String, Vector{Int}}("top"=>[1], "right"=>[2], "both"=>[1,2])
dims = adict[guide.side]
noguides = [Guide.xlabel(nothing), Guide.ylabel(nothing), Guide.yticks(label=false), Guide.xticks(label=false)]
pgv = PositionedGuide[]
if in(1, dims)
p1 = plot(x=aes.x, guide.top, noguides..., Theme(theme, plot_padding=[0mm]) )
ctx = compose(context(minheight=20), render(p1))
push!(pgv, PositionedGuide([ctx], 0, top_guide_position))
end
if in(2, dims)
p1 = plot(y=aes.y, guide.right, noguides..., Theme(theme, plot_padding=[0mm]) )
ctx = compose(context(minwidth=20), render(p1))
push!(pgv, PositionedGuide([ctx], 0, right_guide_position))
end
return pgv
end
end
@Mattriks
Copy link
Author

Example:

using Gadfly
X = randn(1000, 2)

p = plot(x=X[:,1], y=X[:,2], Geom.point, gg.margins(),
    color=[colorant"deepskyblue"], 
    Theme(panel_stroke="gray", default_color="gray80")
)

marginal

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment