Skip to content

[query/ggplot] Adds configurable legend format to point geom - #12244

Merged
danking merged 1 commit into
hail-is:mainfrom
iris-garden:ggplot/combined-legends
Sep 30, 2022
Merged

[query/ggplot] Adds configurable legend format to point geom#12244
danking merged 1 commit into
hail-is:mainfrom
iris-garden:ggplot/combined-legends

Conversation

@iris-garden

Copy link
Copy Markdown
Contributor

This change makes the labels for groups of points on scatterplots created through geom_point configurable by the user.

Consider the following sample 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, color=ht.even, shape=ht.threeven))
    + geom_point()
)
fig.show()

Before this change, this code generates the following legend for the plot:

Screen Shot 2022-09-29 at 12 29 27

After this change, the legend looks like this (× is now the default group name separator):

Screen Shot 2022-09-29 at 12 22 57

If legend_format is passed to geom_point, like so:

...
    + geom_point(legend_format="{color} ({shape})")
...

the following legend will be generated, replacing the names of the aesthetics within brackets in the format string with their values:

Screen Shot 2022-09-29 at 12 25 17

Notably, if the reverse ordering of group names ("{shape} ({color})") is used, the legend will look like this:

Screen Shot 2022-09-29 at 12 26 35

The order in which the groups in the legend appear can be adjusted, if desired, by changing the order of the aesthetics when they are passed into aes:

...
    ggplot(ht, aes(x=ht.idx, y=ht.squared, shape=ht.threeven, color=ht.even))
    + geom_point(legend_format="{shape} ({color})")
...

producing the following legend:

Screen Shot 2022-09-29 at 12 28 01

CHANGELOG: Added support for custom labels for per-group legends to `hail.ggplot.geom_point` via the `legend_format` keyword argument.
@iris-garden
iris-garden force-pushed the ggplot/combined-legends branch from cfb7cf3 to c5ff0ae Compare September 29, 2022 18:56
@danking
danking merged commit c3160d7 into hail-is:main Sep 30, 2022
@iris-garden
iris-garden deleted the ggplot/combined-legends branch October 26, 2022 20:02
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.

3 participants