-
Notifications
You must be signed in to change notification settings - Fork 634
RFC: fix traces subdivision by multiple properties #471
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
Conversation
Wow! At first glance this looks great and attacks a big problem 🍻 I'll need to a few days to mull this over. In the meantime, could you add some tests to ensure this new behavior works as intended? Also, don't worry about the current test failures... I need to find a way to skip those on external pull requests. |
} | ||
sub_trace | ||
}) | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@alyst I'm not quite sure what this if statement is doing. Should this say legendgroup
instead of legenditem
?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@cpsievert It tries to solve the problem of having an item in the legend for each trace (which is not what you want if e.g. you are doing something similar to geom_segment()
). The new property legenditem
specifies the name of the single element in the legend that all the traces in the same legenditem
-group would be referring to (unless they are further subdivided by something else).
To implement this, only the first trace of the group is shown in the legend, but all the other traces still share the same legendgroup
.
Although it utilizes legendgroup
mechanism, I thought it's better to call the property legenditem
, because from the user POV it doesn't do any grouping of legend elements, it just creates one item for the group of traces.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I should have mentioned this a long time ago in response to your comment:
Instead of creating a trace for each group and managing legend entries via legendgroup
, I'd prefer to take the same approach as ggplotly()
where group
doesn't create multiple traces, but groups are separated by NA
s. For a simple example, plot_ly(x = c(1:10), y = c(1:10), group = rep(1:2, each = 5))
should translate to plot_ly(x = c(1:5, NA, 6:10), y = c(1:5, NA, 6:10))
.
We use plotly:::group2NA()
to handle this ggplotly()
. Note that this line checks to see if we are drawing a polygon (in plotlyjs, a polygon is a scatter trace with mode of lines). In that case, we need to "retrace" the first point in each group to effectively close each polygon. There isn't any nice, high-level way to say "draw a polygon" using plot_ly()
, so for now, I think we could just slip in NA
s without worrying about retracing points. Does that make sense @alyst?
@cpsievert Oh, I wasn't aware about the NA trick. Definitely it makes things a lot easier! I'll try to update the PR to make use of it (actually, it would also be easier to implement (sub)trace grouping using |
What do you mean by subtraces? My example is really meant to show how the group argument should work (i.e. it should create just one trace with a gap).
I'm not sure I see the use case. Can you provide an example? In my example, since it should create just one trace, you could name it like this:
I don't think so. |
Ok, I see: the |
7c49ca8
to
14c99fd
Compare
@cpsievert Based on the comments, I've updated (redesigned) the PR. It passes all the tests except those where interaction with the plotly server is required.
|
edc77b0
to
e49b2b0
Compare
- separate the grouping of data into traces (this now done with the help of dplyr::group_by()) from traces creating and applying styles(brushes) to each resulting trace - replace `tracify()` by `generate_traces()`, replace `colorize/symbolize()` by `color/symbol_brush()` - import dplyr - update "group" tests
"group" property doesn't create any new trace, but if the trace mode is "lines", NA points are inserted at the "group" boundaries within the same trace. That is interpreted as the polyline break by plotlyjs.
legenditem allows to group multiply traces into one legend item
This is my version of the #418 fix:
indices
property to the trace to reference the indices ofthe data elements associated with the trace
tracify()
bysubdivide_traces()
that correctly subdividesthe current set of traces based on the given data property
Note that while the traces are assigned the correct visual properties, the PR does not fix #381 -- individual
group
traces are still being created and displayed in the legend.Comments, especially regarding the ways to fix #381 are most welcome.