Skip to content

Toby rect #178

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

Merged
merged 18 commits into from
Mar 18, 2015
Merged

Toby rect #178

merged 18 commits into from
Mar 18, 2015

Conversation

tdhock
Copy link
Contributor

@tdhock tdhock commented Mar 7, 2015

I implemented geom_rect conversion by treating it as a basic version of a polygon, which is transformed into a trace with mode="lines", type="scatter", fill="tozerox"

@tdhock tdhock mentioned this pull request Mar 7, 2015
@chriddyp chriddyp mentioned this pull request Mar 9, 2015
@tdhock
Copy link
Contributor Author

tdhock commented Mar 9, 2015

so with the current code this ggplot http://docs.ggplot2.org/current/geom_rect.html

becomes this plotly https://plot.ly/398/~tdhock/

so I guess fill="tozerox" is not good enough when there are multiple rects that I want to draw in 1 trace. Alex said in an email that 'we could always add a fill mode like "toself" tailored for this' @chriddyp do you think I should wait until that is implemented?

or I can implement this right now by making each rect a separate trace, as in https://plot.ly/3722/~TestBot/

@jackparmer
Copy link
Contributor

haha, well, we tried.
why not do this with rectangle objects again instead of traces? the hover is also odd using traces with fill instead of an svg rectangle.

@alexcjohnson
Copy link
Collaborator

oooookay, it's possible to get this effect with the existing fill modes, but it isn't fun:
https://plot.ly/~alex/1023

You need to duplicate the starting corner of each one, then leave a (non-numeric) gap between rects, then when you're done retrace your steps past all the start / end points (including more gaps so the lines don't get drawn), and for some bizarro reason (that's probably a bug but its such a hack at this point I'm not sure I care) you have to duplicate the final point. 💩

If we made a fillmode "toself" tailored to making closed shapes it would:

  • let you just specify each corner once, no duplication at the end
  • make a new shape every time you leave a non-numeric gap, just by closing to the start of that shape, with no silly retracing at the end
  • do what you expect with line.shape='smooth' (try that on the plot above to see what I mean - not that you would want this for rects, but I could imagine other uses where you want to smoothly outline some region, like in a contour plot)
  • do something smarter with padding the axis ranges. I'm not sure whether it'd be padding on all sides or none, but it certainly wouldn't be padding on only the top and bottom, none on left and right, as you get with fill='tozeroy'
  • maybe even do something smart with hover info and/or displayed text. Not sure about this one, then we're getting into territory that should really be its own trace type (like mosaic plots)

Anyway, this is all a ways out, it's a bigger project than I can just bang out right now. Until then,if you can stomach my hack, that's going to be the highest performance for a large number of rects, then only make a new trace when you want a new color or some other feature like new dashes as in https://plot.ly/3722/~TestBot/. Maybe also set hoverinfo='none' unless you really want people to see the corner values.

@tdhock
Copy link
Contributor Author

tdhock commented Mar 10, 2015

@alexcjohnson thanks for the example plotly. It is nice to know your hack will "be the highest performance for a large number of rects" and I think it makes sense to "only make a new trace when you want a new color" so I will go ahead and implement that.

@tdhock
Copy link
Contributor Author

tdhock commented Mar 10, 2015

after some experimentation in the plotly GUI, I found that in some cases I don't have to re-trace my steps all the way back to the first trace, but instead just back to the first point of the second trace

https://plot.ly/~tdhock/402

however indeed in the general case (here with 2 rects) we need to trace back to the first rect and repeat the start position

https://plot.ly/406/~tdhock


test_that('rect color', {
info <- expect_traces(rect.color, 2, "rect-color")
## TODO: test for weird forward/backward NA pattern with 2 rects.
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Seems like conversion of geom_polygon and geom_rect is working fine now, but I would like to add a few more tests before merging with master.

cbind(x=xmax, y=ymin, others))
})
g$geom <- "polygon"
g
},
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

geom_rect is converted to a basic geom "polygon"


expect_identical(info[[1]]$showlegend, TRUE)
expect_identical(info[[2]]$showlegend, TRUE)
info <- expect_traces(gg, 1, "black")
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I updated the tests to check for the NA values and retracing the first points of each group.

@tdhock
Copy link
Contributor Author

tdhock commented Mar 16, 2015

IMO This code is ready to merge with master. Please code review @mkcor @chriddyp @cpsievert

expect_identical(tr$type, "scatter")
expect_identical(tr$mode, "lines")
for(xy in c("x", "y")){
expect_true(any(is.na(tr[[xy]])))
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just FYI, base does have anyNA() which is slightly faster

@cpsievert
Copy link
Collaborator

This looks good to me 👍

@tdhock tdhock mentioned this pull request Mar 18, 2015
tdhock pushed a commit that referenced this pull request Mar 18, 2015
@tdhock tdhock merged commit 9902be5 into master Mar 18, 2015
@tdhock tdhock deleted the toby-rect branch March 18, 2015 16:42
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants