Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Rectbin errors out when given max and min values #845

Closed
grero opened this issue May 16, 2016 · 4 comments
Closed

Rectbin errors out when given max and min values #845

grero opened this issue May 16, 2016 · 4 comments
Labels

Comments

@grero
Copy link

grero commented May 16, 2016

For some of my plots, I like to add a rectangular shaded region to highlight e.g. a sub-region used for a statistical comparison between two distributions. In Winston, I can accomplish this using the FillBetween type. I noticed that Gadfly has the ribbon geometry, but that seems to be bound to a line, i.e. it will only draw a ribbon around an existing line. It would be really useful to have a dedicated ShadedRegion (or a more fitting name) geometry for this use.

@tbreloff
Copy link
Contributor

You could just draw a polygon instead. Or make the line invisible (0 width
or transparent)

On Monday, May 16, 2016, Roger Herikstad notifications@github.com wrote:

For some of my plots, I like to add a rectangular shaded region to
highlight e.g. a sub-region used for a statistical comparison between two
distributions. In Winston, I can accomplish this using the FillBetween
type. I noticed that Gadfly has the ribbon geometry, but that seems to be
bound to a line, i.e. it will only draw a ribbon around an existing line.
It would be really useful to have a dedicated ShadedRegion (or a more
fitting name) geometry for this use.


You are receiving this because you are subscribed to this thread.
Reply to this email directly or view it on GitHub
#845

@Mattriks
Copy link
Member

According to the guide (http://dcjones.github.io/Gadfly.jl/geom_rectbin.html), Geom.rectbin is supposed to be able to do this for arbitrary rectangles, but I get an error when using the xmin=, ymin=, xmax=, ymax= option. So my solution (yet again!) is to make your own geometry:

using Colors, Compose, DataFrames

immutable RectangleGeometry <: Gadfly.GeometryElement 
     tag::Symbol 

    function RectangleGeometry(; tag::Symbol=Gadfly.Geom.empty_tag) 
         new(tag) 
     end 
end 


function Gadfly.element_aesthetics(::RectangleGeometry) 
    [:xmin, :ymin, :xmax, :ymax, :color] 
end 


function Gadfly.render(geom::RectangleGeometry, theme::Gadfly.Theme, aes::Gadfly.Aesthetics) 

     default_aes = Gadfly.Aesthetics() 
    default_aes.color = DataArrays.PooledDataArray(RGBA{Float32}[theme.default_color]) 
    aes = Gadfly.inherit(aes, default_aes)
    alpha_color = map(theme.lowlight_color, aes.color)

    ctx = context()
    compose!(ctx, rectangle(aes.xmin, aes.ymin, 
      aes.xmax-aes.xmin, aes.ymax-aes.ymin, geom.tag), fill(alpha_color)) 
    return compose!(context(order=4), svgclass("geometry"), ctx)
end

rect1 = RectangleGeometry

# in the next line, opacity = 0.2
theme1 = Theme(lowlight_color=c -> RGBA(c.r, c.g, c.b, 0.2))

D = DataFrame( x1=[-2,0], y1=[-1,0], x2=[1,4], y2=[1,2], col=["a","b"] )
p = plot(
    layer(x=1:5, y=randn(5), Geom.point, Theme(default_color=colorant"red")),
    layer(D, xmin=:x1, ymin=:y1, xmax=:x2, ymax=:y2,  color=:col, rect1, theme1)
)

Looking similar to my code in #844 and #842 !

@grero
Copy link
Author

grero commented May 18, 2016

That works beautifully, thanks!

@tlnagy tlnagy added the bug label Sep 2, 2016
@tlnagy tlnagy changed the title Shaded region Rectbin errors out when given max and min values Sep 2, 2016
@Mattriks Mattriks mentioned this issue Apr 29, 2017
@bjarthur
Copy link
Member

fixed by #993

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

5 participants