Skip to content

Commit

Permalink
[query/ggplot] Shows legend group for single-value columns (#13113)
Browse files Browse the repository at this point in the history
CHANGELOG: `hail.ggplot.geom_point` now displays a legend group for a
column even when it has only one value in it.

Currently, if a column with only one value in it is passed to either the
`color` or the `shape` aesthetic for `geom_point`, the generated legend
will not include a group with a single entry for that aesthetic.
However, R's ggplot2 library in the same situation includes such a
legend group.

This change brings our ggplot implementation into line with R's ggplot2
in that regard.

For example, this code:

```python
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="good")
fig = (
    ggplot(ht, aes(x=ht.idx, y=ht.squared))
    + geom_point(aes(color=ht.even, shape=ht.threeven))
)
fig.show()
```

generates a plot legend like this on `main`:

<img width="90" alt="Screenshot 2023-05-24 at 13 19 49"
src="https://github.com/hail-is/hail/assets/84595986/f17ccbd4-3df7-4e3f-af32-a0b33adccff9">

but generates one like this on this branch:

<img width="101" alt="Screenshot 2023-05-24 at 13 19 16"
src="https://github.com/hail-is/hail/assets/84595986/6d020270-24c8-490c-a411-0bdb7baaa24c">
  • Loading branch information
iris-garden authored Jun 9, 2023
1 parent d647b6a commit bf814b6
Showing 1 changed file with 2 additions and 1 deletion.
3 changes: 2 additions & 1 deletion hail/python/hail/ggplot/geoms.py
Original file line number Diff line number Diff line change
Expand Up @@ -185,7 +185,8 @@ def apply_to_fig(self, grouped_data, fig_so_far: go.Figure, precomputed, facet_r
traces.append([fig_so_far, df, facet_row, facet_col, values, trace_categories])

non_empty_legend_groups = [
legend_group for legend_group in legends.values() if len(legend_group) > 1
legend_group for legend_group in legends.values()
if len(legend_group) > 1 or (len(legend_group) == 1 and list(legend_group.keys())[0] is not None)
]
dummy_legend = is_faceted or len(non_empty_legend_groups) >= 2

Expand Down

0 comments on commit bf814b6

Please sign in to comment.