Created
April 16, 2020 11:57
-
-
Save Mattriks/eee7469a10443f4291358dbb22d2cab7 to your computer and use it in GitHub Desktop.
Custom guides in Gadfly
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Example: