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

[query/ggplot] Replaces cross-product legends with grouped ones #12254

Merged
merged 1 commit into from
Oct 5, 2022

Conversation

iris-garden
Copy link
Contributor

@iris-garden iris-garden commented Sep 30, 2022

Currently, hail.ggplot.geom_point generates cross-product-style legends when the shape and color aesthetics each have multiple groups. This change makes that behavior consistent with ggplot2's implementation, producing legends where the groups are factored out. Because the strategy used to accomplish this does not preserve the interactive features of the legends, such as toggling groups' visibility, this change also makes all plots generated via hail.ggplot.geom_point static.

For example, before this change, this code:

import hail as hl
from hail.ggplot import ggplot, aes, geom_point
ht = hl.utils.range_table(10)
ht = ht.annotate(squared=ht.idx ** 2)
ht = ht.annotate(even=hl.if_else(ht.idx % 2 == 0, "yes", "no"))
ht = ht.annotate(threeven=hl.if_else(ht.idx % 3 == 0, "good", "bad"))
fig = (
    ggplot(ht, aes(x=ht.idx, y=ht.squared))
    + geom_point(aes(color=ht.even, shape=ht.threeven))
)
fig.show()

Generates this legend:

Screen Shot 2022-09-29 at 12 22 57

After the change, this legend is generated:

Screen Shot 2022-10-03 at 13 56 01

Custom labels can be used by updating the code like so:

...
from hail.ggplot import ggplot, aes, geom_point, labs
...
fig = (
    ggplot(ht, aes(x=ht.idx, y=ht.squared))
    + geom_point(aes(color=ht.even, shape=ht.threeven))
    + labs(color="Even", shape="Threeven")
)
...

Generating this legend:

Screen Shot 2022-10-03 at 13 58 34

For more information on the current behavior, see #12244 and #12207.

@iris-garden iris-garden force-pushed the query/ggplot/legends branch 2 times, most recently from 27fe83e to 616873d Compare October 3, 2022 17:47
@iris-garden iris-garden changed the title wip ggplot non-cartesian-product legends [query/ggplot] Replaces cross-product legends with grouped ones Oct 3, 2022
@iris-garden iris-garden force-pushed the query/ggplot/legends branch 3 times, most recently from 58b56e2 to cd3da08 Compare October 3, 2022 19:23
@danking
Copy link
Contributor

danking commented Oct 3, 2022

Just wanted to say this is dope! Particularly considering this issue has been open since 2020.

@patrick-schultz
Copy link
Collaborator

Really nice work! This is a great change that I'm happy to merge now, but this suggests a number of follow-up improvements:

  • we should plan to abstract out the legend handling code to apply uniformly to all geoms
  • in the common case where an aesthetic is mapped to a field (as opposed to a compound expression), we should use the field name as the default legend group name. E.g. your second example with custom labels should be the default (but without capitalizing). This matches ggplot behavior.
  • if multiple aesthetics are mapped to the same value, they should only generate one legend group. E.g. geom_point(aes(color=ht.even, shape=ht.even)) should generate one group with title "even", with values "no" a blue circle and "yes" a red square.

hail/python/hail/ggplot/labels.py Outdated Show resolved Hide resolved
@patrick-schultz
Copy link
Collaborator

Sorry, could you also add an example to the docstring for labs? It isn't obvious that the keys need to be aesthetic names.

@danking
Copy link
Contributor

danking commented Oct 4, 2022

Another follow up PR: update the ggplot tutorial to include the use of cross-product legends!

@iris-garden iris-garden force-pushed the query/ggplot/legends branch from cdc75a3 to d06f813 Compare October 4, 2022 17:07
@iris-garden iris-garden force-pushed the query/ggplot/legends branch 2 times, most recently from f528b40 to 7e30ff0 Compare October 5, 2022 18:08
CHANGELOG: Replaced cross-product-style legends for data groups with factored ones (consistent with `ggplot2`'s implementation) for `hail.ggplot.geom_point`, and added support for custom legend group labels.
@iris-garden iris-garden force-pushed the query/ggplot/legends branch from 7e30ff0 to c59e244 Compare October 5, 2022 18:09
@danking danking merged commit f45e74f into hail-is:main Oct 5, 2022
@iris-garden iris-garden deleted the query/ggplot/legends branch October 26, 2022 20:02
@happydangoe
Copy link

Hey everyone! Thank you so much for working on this. As the requestor of many ggplot features -- including this one -- I am so thankful for all your effort in making this a great graphics language :)

iris-garden added a commit to iris-garden/hail that referenced this pull request Nov 23, 2022
CHANGELOG: Added support for `scales`, `nrow`, and `ncol` arguments, as
well as grouped legends, to `hail.ggplot.facet_wrap`.

This change brings the `facet_wrap` interface more in line with the `ggplot2`
implementation by defaulting to show axes (with tick marks) for all facets, as
well as providing `scales`, `nrow` and `ncol` arguments that the user can use
to specify whether the scales should be the same across facets and how many
rows or columns to use.

It also updates the faceting code to avoid duplicating legend entries when
[grouped legends](hail-is#12254) are used, and to
disable interactivity accordingly.
iris-garden added a commit to iris-garden/hail that referenced this pull request Nov 23, 2022
CHANGELOG: Added support for `scales`, `nrow`, and `ncol` arguments, as
well as grouped legends, to `hail.ggplot.facet_wrap`.

This change brings the `facet_wrap` interface more in line with
[the `ggplot2` implementation](https://ggplot2-book.org/facet.html#facet-wrap)
by defaulting to show axes (with tick marks) for all facets, as well as
providing `scales`, `nrow` and `ncol` arguments that the user can use to specify
whether the scales should be the same across facets and how many rows or columns
to use.

It also updates the faceting code to avoid duplicating legend entries when
[grouped legends](hail-is#12254) are used, and to
disable interactivity accordingly.
iris-garden added a commit to iris-garden/hail that referenced this pull request Nov 23, 2022
CHANGELOG: Added support for `scales`, `nrow`, and `ncol` arguments, as
well as grouped legends, to `hail.ggplot.facet_wrap`.

This change brings the `facet_wrap` interface more in line with
[the `ggplot2` implementation](https://ggplot2-book.org/facet.html#facet-wrap)
by defaulting to show axes (with tick marks) for all facets, as well as
providing `scales`, `nrow` and `ncol` arguments that the user can use to specify
whether the scales should be the same across facets and how many rows or columns
to use.

It also updates the faceting code to avoid duplicating legend entries when
[grouped legends](hail-is#12254) are used, and to
disable interactivity accordingly.
iris-garden added a commit to iris-garden/hail that referenced this pull request Nov 23, 2022
CHANGELOG: Added support for `scales`, `nrow`, and `ncol` arguments, as
well as grouped legends, to `hail.ggplot.facet_wrap`.

This change brings the `facet_wrap` interface more in line with
[the `ggplot2` implementation](https://ggplot2-book.org/facet.html#facet-wrap)
by defaulting to show axes (with tick marks) for all facets, as well as
providing `scales`, `nrow` and `ncol` arguments that the user can use to specify
whether the scales should be the same across facets and how many rows or columns
to use.

It also updates the faceting code to avoid duplicating legend entries when
[grouped legends](hail-is#12254) are used, and to
disable interactivity accordingly.
iris-garden added a commit to iris-garden/hail that referenced this pull request Nov 28, 2022
CHANGELOG: Added support for `scales`, `nrow`, and `ncol` arguments, as
well as grouped legends, to `hail.ggplot.facet_wrap`.

This change brings the `facet_wrap` interface more in line with
[the `ggplot2` implementation](https://ggplot2-book.org/facet.html#facet-wrap)
by defaulting to show axes (with tick marks) for all facets, as well as
providing `scales`, `nrow` and `ncol` arguments that the user can use to specify
whether the scales should be the same across facets and how many rows or columns
to use.

It also updates the faceting code to avoid duplicating legend entries when
[grouped legends](hail-is#12254) are used, and to
disable interactivity accordingly.
iris-garden added a commit to iris-garden/hail that referenced this pull request Nov 28, 2022
CHANGELOG: Added support for `scales`, `nrow`, and `ncol` arguments, as
well as grouped legends, to `hail.ggplot.facet_wrap`.

This change brings the `facet_wrap` interface more in line with
[the `ggplot2` implementation](https://ggplot2-book.org/facet.html#facet-wrap)
by defaulting to show axes (with tick marks) for all facets, as well as
providing `scales`, `nrow` and `ncol` arguments that the user can use to specify
whether the scales should be the same across facets and how many rows or columns
to use.

It also updates the faceting code to avoid duplicating legend entries when
[grouped legends](hail-is#12254) are used, and to
disable interactivity accordingly.
iris-garden added a commit to iris-garden/hail that referenced this pull request Nov 28, 2022
CHANGELOG: Added support for `scales`, `nrow`, and `ncol` arguments, as
well as grouped legends, to `hail.ggplot.facet_wrap`.

This change brings the `facet_wrap` interface more in line with
[the `ggplot2` implementation](https://ggplot2-book.org/facet.html#facet-wrap)
by defaulting to show axes (with tick marks) for all facets, as well as
providing `scales`, `nrow` and `ncol` arguments that the user can use to specify
whether the scales should be the same across facets and how many rows or columns
to use.

It also updates the faceting code to avoid duplicating legend entries when
[grouped legends](hail-is#12254) are used, and to
disable interactivity accordingly.
iris-garden added a commit to iris-garden/hail that referenced this pull request Nov 29, 2022
CHANGELOG: Added support for `scales`, `nrow`, and `ncol` arguments, as
well as grouped legends, to `hail.ggplot.facet_wrap`.

This change brings the `facet_wrap` interface more in line with
[the `ggplot2` implementation](https://ggplot2-book.org/facet.html#facet-wrap)
by defaulting to show axes (with tick marks) for all facets, as well as
providing `scales`, `nrow` and `ncol` arguments that the user can use to specify
whether the scales should be the same across facets and how many rows or columns
to use.

It also updates the faceting code to avoid duplicating legend entries when
[grouped legends](hail-is#12254) are used, and to
disable interactivity accordingly.
iris-garden added a commit to iris-garden/hail that referenced this pull request Nov 29, 2022
CHANGELOG: Added support for `scales`, `nrow`, and `ncol` arguments, as
well as grouped legends, to `hail.ggplot.facet_wrap`.

This change brings the `facet_wrap` interface more in line with
[the `ggplot2` implementation](https://ggplot2-book.org/facet.html#facet-wrap)
by defaulting to show axes (with tick marks) for all facets, as well as
providing `scales`, `nrow` and `ncol` arguments that the user can use to specify
whether the scales should be the same across facets and how many rows or columns
to use.

It also updates the faceting code to avoid duplicating legend entries when
[grouped legends](hail-is#12254) are used, and to
disable interactivity accordingly.
iris-garden added a commit to iris-garden/hail that referenced this pull request Nov 29, 2022
CHANGELOG: Added support for `scales`, `nrow`, and `ncol` arguments, as
well as grouped legends, to `hail.ggplot.facet_wrap`.

This change brings the `facet_wrap` interface more in line with
[the `ggplot2` implementation](https://ggplot2-book.org/facet.html#facet-wrap)
by defaulting to show axes (with tick marks) for all facets, as well as
providing `scales`, `nrow` and `ncol` arguments that the user can use to specify
whether the scales should be the same across facets and how many rows or columns
to use.

It also updates the faceting code to avoid duplicating legend entries when
[grouped legends](hail-is#12254) are used, and to
disable interactivity accordingly.
iris-garden added a commit to iris-garden/hail that referenced this pull request Nov 30, 2022
CHANGELOG: Added support for `scales`, `nrow`, and `ncol` arguments, as
well as grouped legends, to `hail.ggplot.facet_wrap`.

This change brings the `facet_wrap` interface more in line with
[the `ggplot2` implementation](https://ggplot2-book.org/facet.html#facet-wrap)
by defaulting to show axes (with tick marks) for all facets, as well as
providing `scales`, `nrow` and `ncol` arguments that the user can use to specify
whether the scales should be the same across facets and how many rows or columns
to use.

It also updates the faceting code to avoid duplicating legend entries when
[grouped legends](hail-is#12254) are used, and to
disable interactivity accordingly.
danking pushed a commit that referenced this pull request Dec 1, 2022
CHANGELOG: Added support for `scales`, `nrow`, and `ncol` arguments, as
well as grouped legends, to `hail.ggplot.facet_wrap`.

This change brings the `facet_wrap` interface more in line with
[the `ggplot2` implementation](https://ggplot2-book.org/facet.html#facet-wrap)
by defaulting to show axes (with tick marks) for all facets, as well as
providing `scales`, `nrow` and `ncol` arguments that the user can use to specify
whether the scales should be the same across facets and how many rows or columns
to use.

It also updates the faceting code to avoid duplicating legend entries when
[grouped legends](#12254) are used, and to
disable interactivity accordingly.
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.

4 participants