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

WIP: Aesthetics for positions #6100

Draft
wants to merge 12 commits into
base: main
Choose a base branch
from

Conversation

teunbrand
Copy link
Collaborator

This PR aims to fix #3026 and fix #5445.

Briefly, it adds the ability for position adjustments to declare their own aesthetics. It is currently only implemented for position_nudge().

On any layer where position = "nudge", one can use nudge_x and nudge_y either as a mapped aesthetic inside aes(), or static aesthetic passed through layer(params).

A few examples:

devtools::load_all("~/packages/ggplot2")
#> ℹ Loading ggplot2

df <- data.frame(x = 1:2, y = 1:2, z = c(0.5, 1))

ggplot(df, aes(x, y)) +
  geom_point(position = "nudge", nudge_x = 0.5)

ggplot(df, aes(x, y, nudge_y = z)) +
  geom_point(position = "nudge")

Created on 2024-09-12 with reprex v2.1.1

@clauswilke
Copy link
Member

clauswilke commented Sep 12, 2024

@teunbrand As you're working on this, I'd suggest you also look at the long history of issues with misplaced boxplots or violins when categories are missing. I realized long ago that the only way to truly fix this is to somehow have position adjustments be aware of aesthetics. Not sure whether your current work can address this, but conceptually it seems related.

See for example the long discussion here: #3022, and various other issues quoted therein.

Also, here is a reprex that highlights the issue. There is no way I am aware of that can properly format and place boxplots when categories are missing.

library(ggplot2)

# boxes for water polo, netball, and gymnastics have the wrong size
ggplot(ggridges::Aus_athletes, aes(height, sport, fill = sex)) +
  geom_boxplot()

# box for water polo is in the wrong location
ggplot(ggridges::Aus_athletes, aes(height, sport, fill = sex)) +
  geom_boxplot(position = position_dodge(preserve = "single"))

# boxes for water polo, netball, and gymnastics are in the wrong location
ggplot(ggridges::Aus_athletes, aes(height, sport, fill = sex)) +
  geom_boxplot(position = position_dodge2(preserve = "single"))

Created on 2024-09-12 with reprex v2.0.2

Let me know if you think I should open a new issue for this. I don't think we have one currently, but am not entirely certain.

@teunbrand
Copy link
Collaborator Author

Hi @clauswilke, I think that the issue you're highlight is fundamentally the same as #3345. I have pondered them, but haven't been able to come up with a good solution for them (yet). In any case, that issue will not be fixed by this PR.

@clauswilke
Copy link
Member

Yes, agreed, it's the same as #3345.

@teunbrand
Copy link
Collaborator Author

teunbrand commented Sep 13, 2024

On second thought, this PR might be able to fix it if we have something like an order aesthetic for position_dodge().
I'll play around with this for a bit.

@teunbrand
Copy link
Collaborator Author

I've added an order aesthetic to position_dodge() to fix #3022 and #3345.

As an example without order, consider the following case where boxes have partially arbitrary order within their x position:

devtools::load_all("~/packages/ggplot2")
#> ℹ Loading ggplot2

set.seed(0)
df <- data.frame(
  x = rep(rep(LETTERS[1:7], c(3, 2, 2, 2, 1, 1, 1)), each = 10),
  y = rnorm(120),
  group = rep(c("X", "Y", "Z")[c(1:3, 1:2, 1, 3, 2:3, 1:3)], each = 10)
)

ggplot(df, aes(x, y, fill = group)) +
  geom_boxplot(position = position_dodge(preserve = "single"))

Now with the order aesthetic, the boxes are always in the same order regardless of their x position.

last_plot() + aes(order = group)

Created on 2024-09-18 with reprex v2.1.1

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.

Feature Request: nudge_x and nudge_y as a true aesthetic Consistency of handling a wrong length of parameter
2 participants