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

[ggplot] add shape aesthetic support to point geom #12207

Merged
merged 9 commits into from
Sep 23, 2022

Conversation

iris-garden
Copy link
Contributor

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

Currently, geom_point does not support specifying a point shape via an aesthetic. This change adds support for a shape aesthetic to geom_point. For example, using the following set of commands after this change has been applied produces the graph below. See the plotly documentation for a list of supported point shapes.

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

newplot


class ScaleShapeAuto(ScaleDiscrete):
def get_values(self, categories):
return [
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These are hardcoded here because there aren't equivalent methods to plotly.express.colors.(get|sample)_colorscale for the predefined list of shapes that plotly supports, and I couldn't find somewhere in the plotly API from which to directly import that list of constants.

Copy link
Collaborator

@patrick-schultz patrick-schultz left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice work! Thanks for taking this on!

I tried making a plot with only shape, not color. It should behave the same as color (in the discrete case), only using shapes instead of colors, but right now the legend uses "trace 0" etc. instead of the data values.

hail/python/hail/ggplot/scale.py Outdated Show resolved Hide resolved
hail/python/hail/ggplot/ggplot.py Show resolved Hide resolved
@iris-garden
Copy link
Contributor Author

I tried making a plot with only shape, not color. It should behave the same as color (in the discrete case), only using shapes instead of colors, but right now the legend uses "trace 0" etc. instead of the data values.

I was able to get the category names showing properly in the legend with just shapes by adding a shape_legend -> name mapping to GeomPoint.trace_args, but this caused the shape names to override the color names when using both; I added logic to Geom._add_aesthetics_to_trace_args to concatenate the names with one legend entry per pair of color and shape, which causes the shapes to stop working and a separate color to be assigned to each legend entry.

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()

newplot(1)

I'm having trouble tracking down why this is happening, so I'll ask for some help with that tomorrow.

@danking danking merged commit 767bdd6 into hail-is:main Sep 23, 2022
@iris-garden iris-garden deleted the ggplot/point-shapes branch September 23, 2022 20:19
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