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

stat_summary_hex not working properly with discrete z values #5353

Closed
rvalieris opened this issue Jul 17, 2023 · 2 comments
Closed

stat_summary_hex not working properly with discrete z values #5353

rvalieris opened this issue Jul 17, 2023 · 2 comments

Comments

@rvalieris
Copy link

rvalieris commented Jul 17, 2023

I found a problem with stat_summary_hex when z is a discrete variable:

p1 = ggplot(diamonds, aes(x=carat, y=depth)) + geom_hex()
p1 # ok, default stat

p2 = ggplot(diamonds, aes(x=carat, y=depth)) + stat_summary_hex(aes(z=carat),fun=length)
p2 # ok, same result as p1, carat is a continuous variable 

p3 = ggplot(diamonds, aes(x=carat, y=depth)) + stat_summary_hex(aes(z=cut),fun=length)
p3 # bug, cut is a discrete variable

p3

I expected p3 to be the same as p1 and p2 since length should return the same number regardless of which variable I pick for z, however it seems that the bins are being grouped by z, which was not intended.

Also stat_summary_2d have the same behavior, but its harder to see the overlapping bins.

this was tested with ggplot2 3.4.2

@teunbrand
Copy link
Collaborator

teunbrand commented Jul 17, 2023

I think this is one of those cases similar to the ones documented in ?aes_group_order where the defaults fail. In any case, it is true in (almost?) all geoms that using a discrete variable auto-implies grouping and I don't think it would make sense to apply a specific exception here. The plot can be fixed by using aes(group = 1) in either the main plot call or the stat_summary_hex().

@rvalieris
Copy link
Author

Thanks, that The 'group' aesthetic is by default set to the interaction of all discrete variables in the plot. was missing for me, I did tried to set group=NULL but didn't see any difference, group=1 solves the issue.

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

No branches or pull requests

2 participants