From 2231efe03db4768c2a2eb7b39cf462aa1669938b Mon Sep 17 00:00:00 2001 From: Iris Rademacher Date: Wed, 24 May 2023 13:13:41 -0400 Subject: [PATCH] [query/ggplot] Shows legend group for single-value columns 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`: but generates one like this on this branch: --- hail/python/hail/ggplot/geoms.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/hail/python/hail/ggplot/geoms.py b/hail/python/hail/ggplot/geoms.py index a96f0d40906f..b0a79d7a59c7 100644 --- a/hail/python/hail/ggplot/geoms.py +++ b/hail/python/hail/ggplot/geoms.py @@ -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