Skip to content

Commit

Permalink
[query/ggplot] Shows legend group for single-value columns
Browse files Browse the repository at this point in the history
CHANGELOG: `hail.ggplot2.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`:

<legend>

but generates one like this on this branch:

<legend>
  • Loading branch information
iris-garden committed Jun 5, 2023
1 parent 3edb8a7 commit 2231efe
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 2231efe

Please sign in to comment.